java----实现简单的21点纸牌游戏
- 游戏设计思路:
电脑和玩家的⽐拼:谁⼿中扑克牌的“点数和”⾸先达到21点为获胜⽅,或者在双⽅都叫停的情况下谁的“点数和”越接近21点且不⼤于21点为获胜⽅,凡是⼤于21点或者“点数和:⼩于对⽅的”点数和“的⼀⽅为输。
⼤⼩王的点数为0;
J,Q,K的点数为10;
其余的点数都与⾃⼰所表⽰的数字⼀样;
设计⽅法:
1.定义⼀个类:设置每⼀张扑克牌所具有的属性,将其点数,花⾊等等设置好;
2.定义⼀个类:设置⼀副扑克牌所具有的属性,⽐如洗牌,加载图⽚等;
3.定义⼀个游戏界⾯类:将整个游戏界⾯显⽰出来,其中包括⼀些游戏逻辑函数,计算得分,绘图等等
操作;
4.定义⼀个游戏开始的⼊⼝类,加载游戏界⾯和⼀些基本的界⾯属性按键,设计游戏整体界⾯的格局;
说多了都是泪(类),直接上代码整!
Cards类:⼀张扑克牌的属性设置,⽐如点数,花⾊等;
package cords_21_game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import javax.swing.JPanel;
public class Cards {//每⼀张扑克牌的对象属性和功能
//创建每张牌的属性
private String color;//花⾊
private String value;//⾯值
private Image cardImage;//每张牌的图像
public int count;//每张牌的点数
private int x = 0;
private int y = 0;//定义好每张扑克牌的位置坐标
private int cardWidth = 80;
private int cardHeight = 100;
private Image imagebackground = DefaultToolkit().getImage("F:/Image/21_cards/background.png");//这是我的图⽚存储路径
public Cards(String color, String value, Image cardImage)//正常牌初始化
{
this.value = value;
this.cardImage = cardImage;
if(value == "J" || value == "Q" || value == "K")
{
}
else if(value == "A")
{
}
else
{
}
}
public Cards(String value, Image cardImage)//⼤⼩王初始化
{
this.value = value;//-----分为⼤王⼩王
this.value = value;//-----分为⼤王⼩王
this.cardImage = cardImage;
}
public void setPos(int x, int y)
{
this.x = x;
this.y = y;
}
简单的java游戏代码public void drawCards(Graphics g, JPanel i)//在⾯板i上⾯画扑克牌对象
{
g.drawImage(cardImage, x, y, cardWidth, cardHeight, (ImageObserver) i);
}
public void drawBackgroundCards(Graphics g, JPanel i)
{
g.drawImage(imagebackground, x, y, cardWidth, cardHeight, (ImageObserver) i); }
public String getColor()//得到当前对象的花⾊
{
return color;
}
public String getValue()//得到当前对象的⾯值-----“A,2,3,4,5,6。。。”
{
return value;
}
public int getCount()//得到当前对象的点数
{
return count;
}
public void print()
{
System.out.print("花⾊"+color);
System.out.print(" ⾯值"+value);
System.out.println(" 点数"+count);
}
}
Poker类:⼀副扑克牌的⼀些基本属性,⽐如牌的图⽚,牌的获取,洗牌等操作;
package cords_21_game;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Random;
//import java.util.Random;
public class Poker {
static final int COUNT = 54;
static String color[] = {"红桃", "⿊桃", "⽅⽚", "梅花"};
static String value[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
private Image cardsImage[] = new Image[COUNT];
static Cards cards[] = new Cards[COUNT];
public void getcardsImage()//获取图⽚信息-----在创建对象以后
{
for(int i = 0; i < color.length; i++)//花⾊
{
for(int j = 0; j < value.length; j++)//⾯值
for(int j = 0; j < value.length; j++)//⾯值
{
cardsImage[i*value.length + j] = DefaultToolkit().getImage("F:/Image/21_cards\\"+(i+1)+"-"+(j+1)+".png"); }
}
cardsImage[COUNT-2] = DefaultToolkit().getImage("F:/Image/21_cards/5-0.png");//⼩王
cardsImage[COUNT-1] = DefaultToolkit().getImage("F:/Image/21_cards/5-1.png");//⼤王
}
public Poker()
{
for(int i = 0; i < color.length; i++)
{
for(int j = 0; j < value.length; j++)
{
cards[i*13+j] = new Cards(color[i], value[j], cardsImage[i*13+j]);
}
}
cards[COUNT-2] = new Cards("⼩王", cardsImage[COUNT-2]);
cards[COUNT-1] = new Cards("⼤王", cardsImage[COUNT-1]);
}
public void WashCards()
{
Random random = new Random();
for(int i = 0; i < cards.length; i++)
{
int p = Int(cards.length);
Cards temp = cards[i];
cards[i] = cards[p];
cards[p] = temp;
}
}
public Cards[] getCard()//获取⼀副扑克对象
{
return Poker.cards;
}
public Cards getOneCard(int n)//获取⼀张扑克牌对象
{
return Poker.cards[n-1];
}
public void show()//显⽰所有的牌对象color 和 value信息
{
for(int i = 0; i < COUNT; i++)
{
cards[i].print();
}
System.out.println();
}
}
PorkerPanel类:游戏界⾯类,真正的游戏显⽰界⾯设计,也包含了游戏的计算得分,逻辑判断等;
package cords_21_game;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class PokerPanel extends JPanel{
public class PokerPanel extends JPanel{
Poker poker = new Poker();//创建⼀个扑克牌对象
int myScore = 0;
int computerScore = 0;//创建玩家得分和电脑得分初始化为0
ArrayList myCardsList = new ArrayList();
ArrayList computerCardsList = new ArrayList();//创建两个得分机制数组,⼀个存放玩家得分,⼀个存放电脑得分
boolean mylose = false;
boolean computerlose = false;//创建两个变量判断玩家电脑谁输谁赢
static int n = 1;//n⽤来表⽰当前发的牌的ID
public boolean ComputerContinue = true;
public PokerPanel()//构造函数
{
poker.WashCards();//先洗牌
poker.show();//后台显⽰⼀下牌
this.setVisible(true);
repaint();
}
public void paint(Graphics g)
{
//super.paint(g);
g.clearRect(0, 0, Width(), Height());//清屏
g.drawString("电脑牌", 400, 100);
for(int i = 0; i < computerCardsList.size(); i++)
{
Cards card = (Cards) (i);
card.setPos(i*50, 100);//设置扑克牌图像的宽度为50
card.print();
if(computerlose == true || mylose == true)
{
card.drawCards(g, this);
}
else
{
card.drawBackgroundCards(g, this);
}
}
g.drawString("玩家牌", 400, 300);
for(int i = 0; i < myCardsList.size(); i++)
{
Cards card = (Cards) (i);
card.setPos(i*50, 300);
card.print();
card.drawCards(g, this);
}
System.out.println();
}
public void myPlayer()//给玩家发牌
{
myCardsList.OneCard(n));//获取⼀张扑克牌
computeMyScore();//计算玩家得分
repaint();
IsOrNoWashCards();
if(myScore > 21)
{
mylose = true;
// repaint();
JOptionPane.showMessageDialog(null, "玩家输!电脑赢!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "提⽰", JOptionPane.OK_OPTIO repaint();
}
}
public void computeMyScore()
{
{
myScore = 0;
for(int i = 0; i < myCardsList.size(); i++)
{
Cards card = (Cards) (i);
myScore += unt;
}
}
public void computerPlayer()
{
computerCardsList.OneCard(n));
computeComputerScore();
repaint();
IsOrNoWashCards();
if(computerScore > 21)
{
computerlose = true;
ComputerContinue = false;
// repaint();
JOptionPane.showMessageDialog(null, "玩家赢,电脑输!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "提⽰", JOptionPane.OK_OPTIO repaint();
}
else if(computerScore > 18)//电脑停牌不叫
{
ComputerContinue = false;
JOptionPane.showMessageDialog(null, "电脑停牌不叫!", "提⽰", JOptionPane.OK_OPTION);
}
}
public void computeComputerScore()
{
computerScore = 0;
for(int i = 0; i < computerCardsList.size(); i++)
{
Cards card = (Cards) (i);
computerScore += unt;
}
}
public void ComputeAllScore()
{
thisputeMyScore();
thisputeComputerScore();
if(myScore > computerScore)
{
computerlose = true;
JOptionPane.showMessageDialog(null, "玩家赢了,电脑输了!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "赢了", JOptionPane.OK_O }
else if(myScore < computerScore)
{
JOptionPane.showMessageDialog(null, "电脑赢了,玩家输了!\n电脑得分:"+computerScore+"\n玩家得分:"+myScore, "输了", JOptionPane.OK_O mylose = true;
}
else
{
JOptionPane.showMessageDialog(null, "平局!\n电脑得分:"+computerScore+"\n玩家得分:"+myS
core, "平局", JOptionPane.OK_OPTION);
mylose = true;
}
repaint();
}
public void ReGame()
{
myScore = 0;
computerScore = 0;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论