Java是现在比较流行的开发语言,很多软件应用都可以使用java开发出来,当然我们也可以使用java开发游戏,那Java如何写游戏脚本?下面来我们就来给大家讲解一下java写游戏脚本教程之java实现纸牌游戏。
1、实现要求(游戏规则)
有一副扑克54张(同现实接触的纸牌一致),玩家两名,洗牌后随机从任何一个玩家开始发牌,每人获得27张后停顿30秒,然后正式开始游戏,两名玩家逐个出牌(每张牌都是随机抽取),第一局随机决定谁先出,第二局开始,谁的牌大(按照大小规则)谁先出。如果玩家A大,那么将玩家B的牌拿到玩家A的纸牌中,30s后哪个玩家的牌多,谁就获胜!!
2、代码实现
实体类
扑克牌
public class Card { private String rank; private String color; public Card(String rank, String color) { this.rank = rank; this.color = color; } public Card() {} public String getRank() { return rank; } public void setRank(String rank) { this.rank = rank; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Card{" + "rank='" + rank + '\'' + ", color='" + color + '\'' + '}'; } }
玩家
public class Player { private String name; List < Card > handCards = new ArrayList < Card > (); public Player(String name, List < Card > handCards) { this.name = name; this.handCards = handCards; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List < Card > getHandCards() { return handCards; } public void setHandCards(Card card) { this.handCards = handCards; } }
创建扑克类
//创建一副扑克牌 public class Poke { //扑克集合 private List < Card > pokeList = new ArrayList < > (); //扑克牌的值 private int value; //创建一副扑克牌 public Poke() { String[] rank = { "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" }; String[] color = { "黑桃" , "红桃" , "方块" , "梅花" }; System.out.println("--------创建一副扑克牌--------"); for (int i = 0; i < rank.length; i++) { for (int j = 0; j < color.length; j++) { pokeList.add(new Card(rank[i], color[j])); } } pokeList.add(new Card("red_joke", "")); pokeList.add(new Card("black_joke", "")); //打乱顺序 Collections.shuffle(pokeList); } //拿到一张牌 public List < Card > getCard() { return pokeList; } //给扑克牌排大小 public int getValue(Card card) { String[] rank = { "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" }; String[] color = { "黑桃" , "红桃" , "方块" , "梅花" }; for (int i = 0; i < rank.length; i++) { if (card.getRank() .equals(rank[i])) { value = i * 10; } } for (int i = 0; i < color.length; i++) { if (card.getColor() .equals(color[i])) { value += i; } } return value; } }
主函数
public class Main { public static void main(String[] args) throws InterruptedException { //游戏加载界面 System.out.println("--------欢迎来到纸牌游戏--------"); System.out.println("------------游戏规则------------"); System.out.println("共54张牌,两名玩家随机得到27张," + "\n" + "停顿30s后,开启第一局随机一名" + "\n" + "玩家先出牌,第二局开启,谁的" + "\n" + "牌大谁先出,每一局中牌大的人" + "\n" + "赢得牌小的人的牌!30s后谁的" + "\n" + "牌多谁就获胜!!"); System.out.println("-------------------------------"); System.out.println("-------------------------------"); Thread.sleep(2000); //创建游戏角色 List < Card > p1HandCard = new ArrayList < > (); List < Card > p2HandCard = new ArrayList < > (); Player p1 = new Player("王姐", p1HandCard); System.out.println("p1玩家:" + p1.getName() + "加载成功!!"); Player p2 = new Player("三哥", p2HandCard); System.out.println("p2玩家:" + p2.getName() + "加载成功!!"); System.out.println("-------------------------------"); System.out.println("-------------------------------"); Thread.sleep(2000); //创建一副扑克牌 Poke poke = new Poke(); System.out.println("--------美女荷官正在洗牌--------"); System.out.println("洗牌完成!!"); System.out.println("-------------------------------"); System.out.println("-------------------------------"); Thread.sleep(2000); //发牌 System.out.println("--------美女荷官正在发牌--------"); int count = 0; for (int i = 0; i < 27; i++) { // Thread.sleep(500); for (int j = 0; j < 2; j++) { if (j == 0) { p1HandCard.add(poke.getCard() .get(count)); System.out.println(p1.getName() + "拿到" + poke.getCard() .get(count) .getColor() + poke.getCard() .get(count) .getRank()); } else { p2HandCard.add(poke.getCard() .get(count)); System.out.println(p2.getName() + "拿到" + poke.getCard() .get(count) .getColor() + poke.getCard() .get(count) .getRank()); } count++; } } System.out.println("发牌完成!!30s后开始游戏!!!"); System.out.println("-------------------------------"); System.out.println("-------------------------------"); for (int i = 30; i > 0; i--) { // Thread.sleep(1000); System.out.println(i); } //游戏开始,双方出牌 Long t1 = System.currentTimeMillis(); int value1; int value2; //第一局 int random1 = new Random() .nextInt(p1.getHandCards() .size()); int random2 = new Random() .nextInt(p2.getHandCards() .size()); value1 = poke.getValue(p1.getHandCards() .get(random1)); System.out.println(p1.getName() + "出了一张:" + p1.getHandCards() .get(random1) .getColor() + p1.getHandCards() .get(random1) .getRank()); value2 = poke.getValue(p2.getHandCards() .get(random2)); System.out.println(p2.getName() + "出了一张:" + p2.getHandCards() .get(random2) .getColor() + p2.getHandCards() .get(random2) .getRank()); if (value1 > value2) { p1HandCard.add(p2.getHandCards() .get(random2)); p2.getHandCards() .remove(random2); System.out.println(p1.getName() + "得到一张牌,现在共有:" + p1.getHandCards() .size() + "张"); } else { p2HandCard.add(p1.getHandCards() .get(random1)); p1.getHandCards() .remove(random1); System.out.println(p2.getName() + "得到一张牌,现在共有:" + p2.getHandCards() .size() + "张"); } while (true) { //设置30后游戏结束 Long t2 = System.currentTimeMillis(); if (t2 - t1 > 30 * 1000) { break; } else { Thread.sleep(1000); int random3 = new Random() .nextInt(p1.getHandCards() .size()); int random4 = new Random() .nextInt(p2.getHandCards() .size()); value1 = poke.getValue(p1.getHandCards() .get(random3)); System.out.println(p1.getName() + "出了一张:" + p1.getHandCards() .get(random3) .getColor() + p1.getHandCards() .get(random3) .getRank()); value2 = poke.getValue(p2.getHandCards() .get(random4)); System.out.println(p2.getName() + "出了一张:" + p2.getHandCards() .get(random4) .getColor() + p2.getHandCards() .get(random4) .getRank()); if (value1 > value2) { p1HandCard.add(p2.getHandCards() .get(random2)); p2.getHandCards() .remove(random2); System.out.println(p1.getName() + "得到一张牌,现在共有:" + p1.getHandCards() .size() + "张"); } else { p2HandCard.add(p1.getHandCards() .get(random1)); p1.getHandCards() .remove(random1); System.out.println(p2.getName() + "得到一张牌,现在共有:" + p2.getHandCards() .size() + "张"); } if (p1.getHandCards() .size() == 0) { System.out.println(p2.getName() + "获胜!!"); break; } if (p2.getHandCards() .size() == 0) { System.out.println(p1.getName() + "获胜!!"); break; } } } if (p1.getHandCards() .size() > p2.getHandCards() .size()) { System.out.println(p1.getName() + "获胜!!"); } else if (p1.getHandCards() .size() < p2.getHandCards() .size()) { System.out.println(p2.getName() + "获胜!!"); } else { System.out.println("平局!!皆大欢喜!!!"); } } }
项目优化后:
实体类
扑克牌
public class Card { private String rank; private String color; public Card(String rank, String color) { this.rank = rank; this.color = color; } public Card() {} public String getRank() { return rank; } public void setRank(String rank) { this.rank = rank; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Card{" + "rank='" + rank + '\'' + ", color='" + color + '\'' + '}'; } }
玩家
public class Player { private String name; List < Card > handCards = new ArrayList < Card > (); public Player(String name, List < Card > handCards) { this.name = name; this.handCards = handCards; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List < Card > getHandCards() { return handCards; } public void setHandCards(Card card) { this.handCards = handCards; } }
创建扑克牌类
//创建一副扑克牌 public class Poke { //扑克集合 private List < Card > pokeList = new ArrayList < > (); //扑克牌的值 private int value; //创建一副扑克牌 public Poke() { String[] rank = { "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" }; String[] color = { "黑桃" , "红桃" , "方块" , "梅花" }; System.out.println("--------创建一副扑克牌--------"); for (int i = 0; i < rank.length; i++) { for (int j = 0; j < color.length; j++) { pokeList.add(new Card(rank[i], color[j])); } } pokeList.add(new Card("red_joke", "")); pokeList.add(new Card("black_joke", "")); //打乱顺序 Collections.shuffle(pokeList); } //拿到一张牌 public List < Card > getCard() { return pokeList; } //给扑克牌排大小 public int getValue(Card card) { String[] rank = { "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" }; String[] color = { "黑桃" , "红桃" , "方块" , "梅花" }; for (int i = 0; i < rank.length; i++) { if (card.getRank() .equals(rank[i])) { value = i * 10; } } for (int i = 0; i < color.length; i++) { if (card.getColor() .equals(color[i])) { value += i; } } return value; } //发牌 public void DealCard(Poke poke, Player p1, Player p2, List < Card > p1HandCard, List < Card > p2HandCard) { System.out.println("--------美女荷官正在发牌--------"); int count = 0; for (int i = 0; i < 27; i++) { // Thread.sleep(500); for (int j = 0; j < 2; j++) { if (j == 0) { p1HandCard.add(poke.getCard() .get(count)); System.out.println(p1.getName() + "拿到" + poke.getCard() .get(count) .getColor() + poke.getCard() .get(count) .getRank()); } else { p2HandCard.add(poke.getCard() .get(count)); System.out.println(p2.getName() + "拿到" + poke.getCard() .get(count) .getColor() + poke.getCard() .get(count) .getRank()); } count++; } } System.out.println("发牌完成!!30s后开始游戏!!!"); System.out.println("-------------------------------"); System.out.println("-------------------------------"); for (int i = 30; i > 0; i--) { Thread.sleep(1000); System.out.println(i); } } }
主函数
public class Main { public static void main(String[] args) throws InterruptedException { //游戏加载界面 System.out.println("--------欢迎来到纸牌游戏--------"); System.out.println("------------游戏规则------------"); System.out.println("共54张牌,两名玩家随机得到27张," + "\n" + "停顿30s后,开启第一局随机一名" + "\n" + "玩家先出牌,第二局开启,谁的" + "\n" + "牌大谁先出,每一局中牌大的人" + "\n" + "赢得牌小的人的牌!30s后谁的" + "\n" + "牌多谁就获胜!!"); System.out.println("-------------------------------"); System.out.println("-------------------------------"); Thread.sleep(2000); //创建游戏角色 List < Card > p1HandCard = new ArrayList < > (); List < Card > p2HandCard = new ArrayList < > (); Player p1 = new Player("王姐", p1HandCard); System.out.println("p1玩家:" + p1.getName() + "加载成功!!"); Player p2 = new Player("三哥", p2HandCard); System.out.println("p2玩家:" + p2.getName() + "加载成功!!"); System.out.println("-------------------------------"); System.out.println("-------------------------------"); Thread.sleep(2000); //创建一副扑克牌 Poke poke = new Poke(); System.out.println("--------美女荷官正在洗牌--------"); System.out.println("洗牌完成!!"); System.out.println("-------------------------------"); System.out.println("-------------------------------"); Thread.sleep(2000); //发牌 poke.DealCard(poke, p1, p2, p1HandCard, p2HandCard); //游戏开始,双方出牌 new PlayGame(p1, p2, p1HandCard, p2HandCard, poke); } }
这样我们就可以利用java实现纸牌游戏了,其实在开发游戏的时候,我们一开始就需要有清晰的逻辑思维,这样在开发中就会减少出错的机会!最后大家如果想要了解更多java实例知识,敬请关注奇Q工具网。
推荐阅读: