编程语言
首页 > 编程语言> > Java课程设计-运用JavaFX的卡牌游戏

Java课程设计-运用JavaFX的卡牌游戏

作者:互联网

团队情况

前期调查

网上看到一个用扑克牌就能玩的桌游,基于此桌游规则实现游戏设计。

功能架构图

面向对象设计

包图

项目运行截图

登录

注册

游戏主界面

项目关键代码

util类

DbUtil:数据库连接

public class DbUtil {
	
	static String url="jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false";//数据库安装地址
	
	static String username="root";//数据库用户名
	
	static String password="123456";//数据库密码
	
	static String driver="com.mysql.cj.jdbc.Driver";//连接的驱动
	
	
	
	static {    //注册驱动
		try {
			Class.forName(driver);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Connection getConn() {  //连接数据库
		try {
			return DriverManager.getConnection(url,username,password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}

Subtraction:卡牌计算

public class Subtraction {
	static String two="2";
	static BigDecimal num4=new BigDecimal(two);
		
	public static String bloodSubtraction(String blood1,String cardNum,String cardDecor) {   //血量计算
		BigDecimal num1=new BigDecimal(blood1);
		BigDecimal num2=new BigDecimal(cardNum);
		if(cardDecor.equals("4")) {
			BigDecimal num=num2.multiply(num4);    //打出黑桃牌造成点数双倍的伤害
			BigDecimal result =num1.subtract(num);
			String s=result.toString();
			return s;
		}else {
			BigDecimal result =num1.subtract(num2);
			String s=result.toString();
			return s;
		}
		
	}
	
	public  static String surplusSubtraction(String surplus,String num) {   //剩余卡牌计算
		BigDecimal num1=new BigDecimal(surplus);
		BigDecimal num2=new BigDecimal(num);
		BigDecimal result =num1.subtract(num2);
		String s=result.toString();
		return s;
	}
}

model类

Card:卡牌信息

public class Card {
	private int number;
	private int decor;
	public static final String[] DECOR= {"1","2","3","4"};
	public static final String[] NUMBER= {"1","2","3","4","5","6","7","8","9","10"};
	
	
	public String toString() {
		return DECOR[decor]+NUMBER[number];	
	}
	
	public Card(int decor,int number) {
		this.number=number;
		this.decor=decor;
	}
	
	public int getNumber() {
		return number;
	}
	
	public void setNumber(int number) {
		this.number = number;
	}
	
	public int getDecor() {
		return decor;
	}
	
	public void setDecor(int decor) {
		this.decor = decor;
	}

}

Momentum:变量

public class Momentum {
	private StringProperty aggressivity;
	private StringProperty blood;
	private StringProperty cardSurplus;
	
	public Momentum(StringProperty aggressivity,StringProperty blood,StringProperty cardSurplus) {
		this.aggressivity=aggressivity;
		this.blood=blood;
		this.cardSurplus=cardSurplus;
	}
	
	public Momentum(String aggressivity,String blood,String cardSurplus) {
		this.aggressivity=new SimpleStringProperty(aggressivity);
		this.blood=new SimpleStringProperty(blood);
		this.cardSurplus=new SimpleStringProperty(cardSurplus);
	}

	public StringProperty getAggressivity() {
		return aggressivity;
	}
	
	public void setAggressivity(StringProperty aggressivity) {
		this.aggressivity = aggressivity;
	}
	
	public StringProperty getBlood() {
		return blood;
	}
	
	public void setBlood(StringProperty blood) {
		this.blood = blood;
	}

	public StringProperty getCardSurplus() {
		return cardSurplus;
	}

	public void setCardSurplus(StringProperty cardSurplus) {
		this.cardSurplus = cardSurplus;
	}
	
}

dao类

UserDao:用户信息与数据库交互

public class UserDao {
	
	public User findUser(String username,String password) {   //查找用户
		Connection connection=DbUtil.getConn();
		String sql="select * from user_account where username=?";
		User user =null;
		PreparedStatement ps=null;
		ResultSet rs =null;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, username);
			rs =ps.executeQuery();
			while(rs.next()) {
				user=new User();
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				ps.close();
				rs.close();
				connection.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}
		
	
	public boolean addUser(User user) {   //添加用户
		Connection conn =DbUtil.getConn();
		String sql="insert into user_account(username,password) values(?,?)";
		PreparedStatement ps=null;
		int result=0;
		try {
			ps= conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			result =ps.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result != -1;
	}
}

controller类

GameController:游戏界面控制(展示部分关键代码)

    public void changeUseMomentum(Momentum momentum) {   //修改用牌时变量
    	atk.textProperty().bind(momentum.getAggressivity());
    	blood.textProperty().bind(momentum.getBlood());
    	surplus.textProperty().bind(momentum.getCardSurplus());
    }
    
    public void changeDisappearMomentum(Momentum momentum) {   //修改弃牌时变量
    	surplus.textProperty().bind(momentum.getCardSurplus());
    }

    public void setMomentum() {   //初始化变量 
    	Momentum monentum= new Momentum(newatk,newblood,newsurplus);
    	changeUseMomentum(monentum);
    }

    public void getCards() {   //初始化牌组
		for(int i=0;i<4;i++) {
			for(int j=0;j<10;j++) {
				Card card=new Card(i,j);
				cards.add(card);
			}
		}
		Collections.shuffle(cards);
		showCards(cards);
    }
    
	public void showCards(List<Card> cards) {   //初始化手牌
		cardImages.add(cardImage1);
		cardImages.add(cardImage2);
		cardImages.add(cardImage3);
		cardImages.add(cardImage4);
		cardImages.add(cardImage5);
		cardImages.add(cardImage6);
		cardImages.add(cardImage7);
		cardImages.add(cardImage8);
		for (int i = 0; i < 8; i++) {
			cardImages.get(i).setImage(new Image("file:images/"+cards.get(0)+".png"));
			cards.remove(0);
		}
	}
public void calculationUseCard(ImageView cardImage) {   //修改用牌时点数计算
		@SuppressWarnings("deprecation")
		String s=cardImage.getImage().impl_getUrl();
		sendNum=Extract.extractNumbers(s);
		cardDecor=sendNum.substring(0, 1);
		cardNum=sendNum.substring(1);
		if(cardDecor.equals("1")) {
			clubSupplementCard(cardNum);
		}
		String result =Subtraction.bloodSubtraction(blood1,cardNum,cardDecor);
		String now=Subtraction.surplusSubtraction(surplus1, "1");
		Momentum monentum= new Momentum(atk1,result,now);
    	changeUseMomentum(monentum);
		blood1=result;
		surplus1=now;
    }
	
	public void calculationDisappearCard(ImageView cardImage) {   //修改弃牌时点数计算
		@SuppressWarnings("deprecation")
		String s=cardImage.getImage().impl_getUrl();
		sendNum=Extract.extractNumbers(s);
		cardDecor=sendNum.substring(0, 1);
		cardNum=sendNum.substring(1);
		String result =blood1;
		String now=Subtraction.surplusSubtraction(surplus1, "1");
		Momentum monentum= new Momentum(atk1,result,now);
    	changeDisappearMomentum(monentum);
		blood1=result;
		surplus1=now;
	}

改进及感想

感想:虽然项目看起来不难,但一个人做的话各个知识点都需要学习,有些时间不足,尝试不同于swing的JavaFX界面设计时花费蛮多时间,但是上手后能实现不错的效果,游戏功能只是基本实现,和原本的规则比起来还差很多,后续游戏逻辑不难但唯独就是时间不够了,日后有待完善空间。经过这次课设让我对面向对象编程有了更深刻的理解,同时也学到了很多新知识,虽然只是浅尝辄止,但相信对我日后的学习会有帮助,同时有感叹计算机技术的高深,一项一项技术组合起来就能实现非常棒的项目,期待之后的学习中能加深理解,以后利用更多的知识实现更多的的功能,创造更好的项目。
改进

标签:课程设计,Java,String,JavaFX,result,new,return,public,BigDecimal
来源: https://www.cnblogs.com/miao-witch/p/15790888.html