j_flychess.java
来自「用java开发的飞行棋游戏 包含用户手册和技术文档」· Java 代码 · 共 1,810 行 · 第 1/4 页
JAVA
1,810 行
Image[] map1=new Image[4],
map2=new Image[4],
map3=new Image[4],
map4=new Image[4],
map5=new Image[4];
int lenth=50,lenthpoint=225,i,j,player=0,id=0,p;
int x=100,y=60;//棋盘初始坐标
int randomNum;
String colors[]={"红","蓝","绿","黄"};
String gameStatus;
Button ranButton,startButton,stopButton;
Button [][] chess={ {new Button("红"),new Button("红"),new Button("红"),new Button("红")},
{new Button("蓝"),new Button("蓝"),new Button("蓝"),new Button("蓝")},
{new Button("绿"),new Button("绿"),new Button("绿"),new Button("绿")},
{new Button("黄"),new Button("黄"),new Button("黄"),new Button("黄")}};
TextField text1,text2,text3;
//////////////////////////////////代表棋子状态的变量/////////////////////////////////////////////////////
int winner[]=new int[4]; //代表每个玩家共有几个棋子到站
int planeTimes[][]=new int[4][4]; //代表每个棋子的状态,0为等待起飞,1为自由活动,-1为到达终点
int playerTimes[]=new int[4]; //代表每个玩家有几个棋子处于活动状态
///////////////////////////////////坐标变量//////////////////////////////////////////////////////////
XY[][] planeXY=new XY[4][4],startXY=new XY[4][4]; //存储每个飞机的坐标
XY[] airportXY=new XY[4]; //飞机的起点
XY winXY=new XY(x+4*lenth,y+5*lenth); //到达终点后的坐标
J_Map2 map=new J_Map2();
public void init()
{
startButton=new Button("开始游戏");
ranButton=new Button("扔色子");
text1=new TextField(3);
text2=new TextField(30);
text3=new TextField(30);
add(startButton);
add(ranButton);
add(text1);
add(text2);
add(text3);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
add(chess[i][j]);
ranButton.addActionListener(this);
startButton.addActionListener(this);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
chess[i][j].addActionListener(this);
ranButton.setEnabled(false);
for(i=0;i<4;i++)
chess[0][i].setBackground(Color.red);
for(i=0;i<4;i++)
chess[1][i].setBackground(Color.blue);
for(i=0;i<4;i++)
chess[2][i].setBackground(Color.green);
for(i=0;i<4;i++)
chess[3][i].setBackground(Color.yellow);
map1[0]=getImage(getCodeBase(),"red.png");
map1[1]=getImage(getCodeBase(),"blue.png");
map1[2]=getImage(getCodeBase(),"green.png");
map1[3]=getImage(getCodeBase(),"yellow.png");
map2[0]=getImage(getCodeBase(),"redpoint.png");
map2[1]=getImage(getCodeBase(),"bluepoint.png");
map2[2]=getImage(getCodeBase(),"greenpoint.png");
map2[3]=getImage(getCodeBase(),"yellowpoint.png");
map3[0]=getImage(getCodeBase(),"redstart.png");
map3[1]=getImage(getCodeBase(),"bluestart.png");
map3[2]=getImage(getCodeBase(),"greenstart.png");
map3[3]=getImage(getCodeBase(),"yellowstart.png");
map4[0]=getImage(getCodeBase(),"redlpoint.png");
map4[1]=getImage(getCodeBase(),"bluelpoint.png");
map4[2]=getImage(getCodeBase(),"greenlpoint.png");
map4[3]=getImage(getCodeBase(),"yellowlpoint.png");
map5[0]=getImage(getCodeBase(),"red1.png");
map5[1]=getImage(getCodeBase(),"blue1.png");
map5[2]=getImage(getCodeBase(),"green1.png");
map5[3]=getImage(getCodeBase(),"yellow1.png");
startXY[0][0]=new XY(x+5*lenth,y+3*lenth);
startXY[0][1]=new XY(x+6*lenth,y+3*lenth);
startXY[0][2]=new XY(x+5*lenth,y+4*lenth);
startXY[0][3]=new XY(x+6*lenth,y+4*lenth);
startXY[1][0]=new XY(x+5*lenth,y+6*lenth);
startXY[1][1]=new XY(x+6*lenth,y+6*lenth);
startXY[1][2]=new XY(x+5*lenth,y+7*lenth);
startXY[1][3]=new XY(x+6*lenth,y+7*lenth);
startXY[2][0]=new XY(x+2*lenth,y+6*lenth);
startXY[2][1]=new XY(x+3*lenth,y+6*lenth);
startXY[2][2]=new XY(x+2*lenth,y+7*lenth);
startXY[2][3]=new XY(x+3*lenth,y+7*lenth);
startXY[3][0]=new XY(x+2*lenth,y+3*lenth);
startXY[3][1]=new XY(x+3*lenth,y+3*lenth);
startXY[3][2]=new XY(x+2*lenth,y+4*lenth);
startXY[3][3]=new XY(x+3*lenth,y+4*lenth);
airportXY[0]=new XY(x+9*lenth,y);
airportXY[1]=new XY(x+9*lenth,y+10*lenth);
airportXY[2]=new XY(x-lenth,y+10*lenth);
airportXY[3]=new XY(x-lenth,y);
}
public void paint(Graphics g)//绘制棋盘
{
for(j=0;j<4;j++)
g.drawImage(map1[j],x+j*lenth,y,lenth,lenth,this);
for(j=0;j<4;j++)
g.drawImage(map1[j],x+(j+4)*lenth,y,lenth,lenth,this);
g.drawImage(map1[0],x+8*lenth,y,lenth,lenth,this);
g.drawImage(map1[1],x+9*lenth,y+lenth,lenth,lenth,this);
g.drawImage(map1[2],x+9*lenth,y+2*lenth,lenth,lenth,this);
g.drawImage(map1[3],x+9*lenth,y+3*lenth,lenth,lenth,this);
g.drawImage(map1[0],x+9*lenth,y+4*lenth,lenth,lenth,this);
g.drawImage(map1[1],x+9*lenth,y+5*lenth,lenth,lenth,this);
g.drawImage(map1[2],x+9*lenth,y+6*lenth,lenth,lenth,this);
g.drawImage(map1[3],x+9*lenth,y+7*lenth,lenth,lenth,this);
g.drawImage(map1[0],x+9*lenth,y+8*lenth,lenth,lenth,this);
g.drawImage(map1[1],x+9*lenth,y+9*lenth,lenth,lenth,this);
g.drawImage(map1[2],x+8*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[3],x+7*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[0],x+6*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[1],x+5*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[2],x+4*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[3],x+3*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[0],x+2*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[1],x+1*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[2],x,y+10*lenth,lenth,lenth,this);
g.drawImage(map1[3],x-lenth,y+9*lenth,lenth,lenth,this);
g.drawImage(map1[0],x-lenth,y+8*lenth,lenth,lenth,this);
g.drawImage(map1[1],x-lenth,y+7*lenth,lenth,lenth,this);
g.drawImage(map1[2],x-lenth,y+6*lenth,lenth,lenth,this);
g.drawImage(map1[3],x-lenth,y+5*lenth,lenth,lenth,this);
g.drawImage(map1[0],x-lenth,y+4*lenth,lenth,lenth,this);
g.drawImage(map1[1],x-lenth,y+3*lenth,lenth,lenth,this);
g.drawImage(map1[2],x-lenth,y+2*lenth,lenth,lenth,this);
g.drawImage(map1[3],x-lenth,y+1*lenth,lenth,lenth,this);
g.drawImage(map2[0],x+4*lenth,y+1*lenth,lenth,lenthpoint,this);
g.drawImage(map2[1],x+225,y+5*lenth,lenthpoint,lenth,this);
g.drawImage(map2[2],x+4*lenth,y+275,lenth,lenthpoint,this);
g.drawImage(map2[3],x,y+5*lenth,lenthpoint,lenth,this);
g.drawImage(map4[0],x+0*lenth,y+8*lenth,9*lenth,lenth,this);
g.drawImage(map4[1],x+1*lenth,y+1*lenth,lenth,9*lenth,this);
g.drawImage(map4[2],x+0*lenth,y+2*lenth,9*lenth,lenth,this);
g.drawImage(map4[3],x+7*lenth,y+1*lenth,lenth,9*lenth,this);
g.drawImage(map3[0],x+5*lenth,y+3*lenth,2*lenth,2*lenth,this);
g.drawImage(map3[1],x+5*lenth,y+6*lenth,2*lenth,2*lenth,this);
g.drawImage(map3[2],x+2*lenth,y+6*lenth,2*lenth,2*lenth,this);
g.drawImage(map3[3],x+2*lenth,y+3*lenth,2*lenth,2*lenth,this);
g.drawImage(map3[0],x+9*lenth,y+0*lenth,lenth,lenth,this);
g.drawImage(map3[1],x+9*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map3[2],x-1*lenth,y+10*lenth,lenth,lenth,this);
g.drawImage(map3[3],x-1*lenth,y+0*lenth,lenth,lenth,this);
for(i=0;i<3;i++)
g.drawImage(map1[0],x+4*lenth,y+(i+1)*lenth,lenth,lenth,this);
for(i=0;i<3;i++)
g.drawImage(map1[3],x+i*lenth,y+5*lenth,lenth,lenth,this);
for(i=0;i<3;i++)
g.drawImage(map1[1],x+(6+i)*lenth,y+5*lenth,lenth,lenth,this);
for(i=0;i<3;i++)
g.drawImage(map1[2],x+4*lenth,y+(7+i)*lenth,lenth,lenth,this);
}
public void actionPerformed(ActionEvent e)
{
int next=player+1;//表示下一个玩家
if(next>3)
next=0;
Button buttonx=(Button)e.getSource();
if(buttonx==startButton) //玩家按了startButton,进入游戏状态
{
gameStatus="InGame";
startgame();
}
else if(buttonx==ranButton) //玩家按了随机数按钮
{
ranButton.setEnabled(false);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
chess[i][j].setEnabled(true);
if(planeTimes[i][j]==-1)
chess[i][j].setEnabled(false);
}
if(gameStatus=="InGame")
{
double c=6*Math.random();
randomNum=(int)(c+1);
text1.setText(" "+randomNum); //产生随机数
if(playerTimes[player]==0&&randomNum!=6)
{
text3.setText("没扔到6,不能起飞");
text2.setText("该"+colors[next]+"方扔色子");
ranButton.setEnabled(true);
player++;
if(player>3)
player=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
chess[i][j].setEnabled(false);
}
else
if(randomNum==6)
{
text2.setText(colors[player]+"方,恭喜你,可以起飞了!");
text3.setText(" ");
}
else
{
text2.setText("该"+colors[player]+"方走棋");
text3.setText(" ");
}
}
}
else //玩家按了棋子
{
if(gameStatus=="InGame")
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(buttonx==chess[i][j])
{
id=j;
p=i;
}
}//寻找所按的棋子
if(p==player)//所按棋子属于当前玩家
{
if(planeTimes[player][id]==0)
{
if(randomNum==6)
{
text2.setText("起飞了!");
ranButton.setEnabled(true);
planeXY[player][id].x1=airportXY[player].x1;
planeXY[player][id].y1=airportXY[player].y1;
playerTimes[player]++;
planeTimes[player][id]=1;
chess[player][id].setLocation(airportXY[player].x1,airportXY[player].y1);
player++;
if(player>3)
player=0;
text2.setText("该"+colors[player]+"方扔色子");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
chess[i][j].setEnabled(false);
ranButton.setEnabled(true);
}
}
else
{
text3.setText("不能走这一个棋子");
}
}
else
{
calculate(player,id,randomNum);//计算当前棋子的坐标
if(gameStatus!="GameOver")
{
for(i=0;i<4;i++)
{
if(i==player) continue;
if(planeXY[player][id].x1==airportXY[i].x1&&planeXY[player][id].y1==airportXY[i].y1)//该坐标是其他玩家机场
{
text3.setText("误入他人机场,回到起点");
planeXY[player][id].x1=airportXY[player].x1;
planeXY[player][id].y1=airportXY[player].y1;
}
else
{
for(j=0;j<4;j++)
{
if(planeXY[player][id].x1==planeXY[i][j].x1&&planeXY[player][id].y1==planeXY[i][j].y1
&&planeXY[i][j].x1!=winXY.x1&&planeXY[i][j].y1!=winXY.y1&&i!=player)//该坐标上已有其他玩家的棋子
{
text3.setText("撞机事件!"+colors[i]+"方,重新起飞吧");
playerTimes[i]--;
planeTimes[i][j]=0;
planeXY[i][j].x1=startXY[i][j].x1;
planeXY[i][j].y1=startXY[i][j].y1;
chess[i][j].setLocation(planeXY[i][j].x1,planeXY[i][j].y1);
}
}
}
}
chess[player][id].setLocation(planeXY[player][id].x1,planeXY[player][id].y1);
player++;
if(player>3)
player=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
chess[i][j].setEnabled(false);
ranButton.setEnabled(true);
text2.setText("该"+colors[player]+"方扔色子");
}
}
}
else
{
text2.setText("该"+colors[player]+"方走棋");}
}
}
}
public void startgame()//开始游戏,设置按钮位置
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
chess[i][j].setSize(lenth,lenth);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
planeXY[i][j]=new XY(startXY[i][j].x1,startXY[i][j].y1);
chess[i][j].setLocation(planeXY[i][j].x1,planeXY[i][j].y1);
}
startButton.setEnabled(false);
ranButton.setEnabled(true);
text2.setText("开始游戏啦!该红方扔色子");
}
public void win() //当前棋子到达终点
{
text3.setText(colors[player]+"方棋子进站啦!恭喜!");
playerTimes[player]--;
planeTimes[player][id]=-1;
planeXY[player][id].x1=winXY.x1;
planeXY[player][id].y1=winXY.y1;
chess[player][id].setEnabled(false);
winner[player]++;
if(winner[player]==4)
gameover(player);
}
public void gameover(int playernum)//四颗棋子都进站,游戏结束
{
text2.setText(colors[playernum]+"方获胜!");
text3.setText("GameOver");
gameStatus="GameOver";
startButton.setEnabled(false);
ranButton.setEnabled(false);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
chess[i][j].setEnabled(false);
}
public void calculate(int playerNum,int idNum,int radNum)//当前棋子坐标计算方法,复杂,可以忽略
{
if(playerNum==0)
{
if(planeXY[player][idNum].x1==x+9*lenth&&(planeXY[player][idNum].y1+radNum*lenth)<=y+10*lenth)
{
planeXY[player][idNum].y1+=radNum*lenth;
if(planeXY[player][idNum].y1==y+4*lenth)
{
planeXY[player][idNum].y1=y+8*lenth;
}
else
if(planeXY[player][idNum].y1==y+8*lenth)
{
planeXY[player][idNum].x1=x-lenth;
}
}//0号玩家右边竖
else
if(planeXY[player][idNum].x1==x+9*lenth&&(planeXY[player][idNum].y1+radNum*lenth)>y+10*lenth)
{
planeXY[player][idNum].x1=x+9*lenth-(planeXY[player][idNum].y1+(radNum-10)*lenth-y);
planeXY[player][idNum].y1=y+10*lenth;
if(planeXY[player][idNum].x1==x+6*lenth)
{
planeXY[player][idNum].x1=x+2*lenth;
}
}//0号右边竖转角
else
if(planeXY[player][idNum].y1==y+10*lenth&&planeXY[player][idNum].x1-radNum*lenth>=x-lenth)
{
planeXY[player][idNum].x1-=radNum*lenth;
if(planeXY[player][idNum].x1==x+6*lenth)
{
planeXY[player][idNum].x1=x+2*lenth;
}
else
if(planeXY[player][idNum].x1==x+2*lenth)
{
planeXY[player][idNum].x1=x-lenth;
planeXY[player][idNum].y1=y+8*lenth;
}
}//0号下面横
else
if(planeXY[player][idNum].y1==y+10*lenth&&planeXY[player][idNum].x1-radNum*lenth<x-lenth)
{
planeXY[player][idNum].y1=y+10*lenth+planeXY[player][idNum].x1-radNum*lenth-x+lenth;
planeXY[player][idNum].x1=x-lenth;
if(planeXY[player][idNum].y1==y+8*lenth)
{
planeXY[player][idNum].y1=y+4*lenth;
}
}//0号左下转角
else
if(planeXY[player][idNum].x1==x-lenth&&planeXY[player][idNum].y1-radNum*lenth>=y)
{
planeXY[player][idNum].y1-=radNum*lenth;
if(planeXY[player][idNum].y1==y+8*lenth)
{
planeXY[player][idNum].y1=y+4*lenth;
}
else
if(planeXY[player][idNum].y1==y+4*lenth)
{
planeXY[player][idNum].y1=y;
planeXY[player][idNum].x1=x;
}
}//0号左边竖
else
if(planeXY[player][idNum].x1==x-lenth&&(planeXY[player][idNum].y1-radNum*lenth)<=y)
{
planeXY[player][idNum].x1=x-lenth+y-(planeXY[player][idNum].y1-radNum*lenth);
planeXY[player][idNum].y1=y;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?