📄 planegame.java
字号:
///////////////////////////////////////////////////////////////////////////////////////
//这是一个飞行棋游戏,玩过吗?我们通过三个部分来实现它
//1。PlaneGame类,它是一个Applet 负责飞机构件的加载, 飞机图象的绘画, 以及一些常用构件的加载
//
//2. PlaneContainer 类,它是一个自定义的构件类,负责飞机构件的事件,飞机的监听者
//
//3。Plane类, 这个类用来将飞机当前在自定义的数组中的位置转换成图象上的坐标位置,
// 并且它还会处理飞机游戏中的逻辑以及数学上的运算。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.awt.event.*;
import java.applet.Applet;
import java.net.URL;
public class PlaneGame extends Applet implements PlaneListener,ActionListener //调入飞机的监听者接口,
{ //以及按钮的监听者接口
//========================define variable=======================
public int player=0,planeID=0; //定义玩家,以及玩家的飞机号
public int randomNum; //定义随机数,相当于游戏中的用来撒的股子
public String gameStatus=new String(); //定义游戏的状态
private Image im; //大地图,就是飞机游戏的棋盘
private Image imP[][]; //飞机图象,二维数组的第一维是玩家号,第二维是飞机的ID号
public int complete[]=new int[4]; //玩家的飞机到机的个数(到机:是这个飞机到达了终点)
public boolean addPlane=false; //因为玩家必须在撒了股子后才能走棋,这个函数是来控制
//玩家这项规则的
// ===================define map array====================
public int waitIn[][]=new int[4][5]; // 定义飞机停靠站组数
public String moveMap[][]=new String[52][2]; //飞机航道数组
public int toSky[][]=new int[4][6]; //飞机升天道数组
public Plane po[][]=new Plane[4][4]; //飞机对象数组
public ConXY[][] xy=new ConXY[4][5]; //ConXY类相当于一个结构体,它存有每架飞机的坐标
//=====================define container=====================
Button ranButton; //产生随机数的按钮
Button startButton; //改变游戏状态的按钮
TextField text; //输出随机数的文本框
public PlaneContainer pc[][]=new PlaneContainer[4][4]; //飞机构件,来自定义飞机构件类
//=======================the space of function================
public void init()
{
//初始化飞机停靠数组
for(int i=0;i<4;i++)
{for(int j=0;j<5;j++)
{
waitIn[i][j]=j;
}
}
System.out.println("1"); //嘿嘿,自定义跟踪
//初始化飞机的航道数组
for(int i=0;i<52;i+=4)
{
moveMap[i][0]=new String("green"); //the green planes
moveMap[i][1]=new String("no");
moveMap[i+1][0]=new String("red"); //the red plane
moveMap[i+1][1]=new String("no");
moveMap[i+2][0]=new String("yellow"); //the yellow plane
moveMap[i+2][1]=new String("no");
moveMap[i+3][0]=new String("blue"); //the blue plane
moveMap[i+3][1]=new String("no");
}
System.out.println("2");
//初始化飞机的升天格数组
for(int i=0;i<4;i++)
{for(int j=1;j<=6;j++)
{
toSky[i][j-1]=j;
}
}
System.out.println("3");
//初始化飞机的停靠位置,用ConXY来表示
xy[0][0]=new ConXY(120,640); //初始化为飞机的停靠站
xy[0][1]=new ConXY(120,560);
xy[0][2]=new ConXY(40,640);
xy[0][3]=new ConXY(40,560);
xy[0][4]=new ConXY(180,660);
xy[1][0]=new ConXY(120,240);
xy[1][1]=new ConXY(120,160);
xy[1][2]=new ConXY(40,240);
xy[1][3]=new ConXY(40,160);
xy[1][4]=new ConXY(20,180);
xy[2][0]=new ConXY(520,240);
xy[2][1]=new ConXY(520,160);
xy[2][2]=new ConXY(440,240);
xy[2][3]=new ConXY(440,160);
xy[2][4]=new ConXY(500,20);
xy[3][0]=new ConXY(520,640);
xy[3][1]=new ConXY(520,560);
xy[3][2]=new ConXY(440,640);
xy[3][3]=new ConXY(440,640);
xy[3][4]=new ConXY(660,500);
System.out.println("4");
//初始化飞机的计算方面的对象数组
for(int j=0;j<4;j++)
{
po[0][j]=new Plane("red",0,j,"wait");
po[1][j]=new Plane("yellow",1,j,"wait");
po[2][j]=new Plane("blue",2,j,"wait");
po[3][j]=new Plane("green",3,j,"wait");
}
System.out.println("5");
//初始化飞机构件对象数组,i,j是玩家数以及相应玩家的飞机ID
for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++)
{
pc[i][j]=new PlaneContainer(i,j);
}
}
System.out.println("6");
//初始化飞机游戏的状态以及飞机的图象
gameStatus="beforeGame";
imP=new Image[4][4];
//初始化随机按钮,改变游戏状态的按钮,输出随机数的文本框
ranButton=new Button("randomNum"); //随机函数按钮
text=new TextField(6); //随机数显示框
startButton=new Button("start game");
System.out.println("7");
//=============================加载布置构件==============================================
//布置所有的构件
setLayout(null); //定义Applet的布局管理是null,因为飞机的坐标都是程序给出的
//有更好的办法吗?计算坐标好复杂,而且不精确
text.setLocation(700,100); //设置文本框的位置
ranButton.setLocation(700,200);//设置随机按钮的位置
startButton.setLocation(900,200); //设置状态按钮的位置
//加载构件
add(text);
add(ranButton);
add(startButton);
System.out.println("8");
//加载按钮的监听者
startButton.addActionListener(this);
ranButton.addActionListener(this);
ranButton.setEnabled(false);
System.out.println("9");
//==========加载当前玩家飞机构件的监听者,就是这里初始化不了========
pc[player][0].addPlaneListener(this);
pc[player][1].addPlaneListener(this);
pc[player][2].addPlaneListener(this);
pc[player][3].addPlaneListener(this);
System.out.println("10");
//加载棋盘图象,飞机图象
loadImage();
//加载和布置飞机构件
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
{
add(pc[i][j]);
pc[i][j].setBounds(xy[i][j].x,xy[i][j].y,30,30);
}
}
}
private void loadImage() //加载图象方法
{
try{
URL ur=getClass().getResource("map.gif"); //加载图象作为资源(棋盘)
im=createImage((ImageProducer)ur.getContent());
}
catch(Exception e){
e.printStackTrace();
}
MediaTracker mt=new MediaTracker(this); //加载图象追踪者的实例
mt.addImage(im,0);
try{ //等待加载图象
mt.waitForID(0);
}
catch(Exception e){
e.printStackTrace();
}
//=================================加载飞机图象================================
try{
URL ur[]=new URL[4]; //同上
ur[0]=getClass().getResource("redplane.gif");
ur[1]=getClass().getResource("yellowplane.gif");
ur[2]=getClass().getResource("blueplane.gif");
ur[3]=getClass().getResource("greenplane.gif");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
imP[i][j]=createImage((ImageProducer)ur[i].getContent());
}
}
}
catch(Exception e){ e.printStackTrace(); }
MediaTracker mtP[]=new MediaTracker[4];
for(int i=0;i<4;i++)
{
mtP[i]=new MediaTracker(this);
for(int j=0;j<4;j++)
{
mtP[i].addImage(imP[i][j],i);
}
}
try{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
mtP[i].waitForID(i);
}
}
}
catch(Exception e){ e.printStackTrace(); }
}
//这个方法是为了让Applet重画是不会闪烁
public void update(Graphics g)
{
paint(g);
}
//绘制图象
public void paint(Graphics g)
{
if(gameStatus=="over")
{
g.drawString("player"+(player+1)+"is win!!!",300,300); //在游戏结束是提示玩家
stop();
}
g.drawImage(im,0,0,this);
if(gameStatus=="beforGame") //在游戏初始化阶段绘制所有玩家的飞机
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
g.drawImage(imP[i][j],xy[i][j].x,xy[i][j].y,this);
}
}
}
if(gameStatus=="inGame") //在进行游戏的状态是绘制飞机监听者处理的飞机
{
g.drawImage(imP[player][planeID],xy[player][planeID].x,xy[player][planeID].y,this);
}
}
//按钮的监听者处理方法
public void actionPerformed(ActionEvent event)
{
Button button=(Button)event.getSource();//得到按钮的事件源,就是玩家按了哪的按钮
if(button==startButton) //如果玩家按了startButton那么游戏进入了游戏状态
{
gameStatus="InGame";
}
if(button==ranButton) //如果玩家按了随机数按钮,那么。。。。
{
if(gameStatus=="InGame")
{
if(complete[player]==4) //如果玩家x的四架飞机都到了站,调用win(),游戏结束
{
winer();
}
showStatus("we are gaming"); //显示游戏当前的状态
double x=6*Math.random(); //产生随机数
randomNum=(int)(x+1);
text.setText(" "+randomNum); //在文本框中显示随机数
if(randomNum!=6) //如果随机数是否是6,如果是那么还是该玩家player走
{
if(player==4)
{
player=0;
}
player++;
}
}
}
}
public void stop() //如果有人赢了,就停止游戏
{
showStatus("GAME OVER");
}
//飞机构件监听者处理方法
public void planePerformed(PlaneEvent event)
{
Planeable pa=event.getPlaneable(); //得到事件源,激发事件的构件
Object planeO=(planeContainer)pa.getPlane(); //得到此飞机构件
int x,y; //x存储的是当前玩家数,y存储的是当前玩家要走的飞机
x=planeO.pp;
y=planeO.dd;
if(po[x][y].status=="wait") //如果飞机状态是停机就调用plane类的wait()方法计算飞机的坐标
{
po[x][y].wait(randomNum);
xy[x][y]=po[x][y].xy; //得到飞机的计算后的坐标
repaint(); //重画飞机
}
else if(po[x][y].status=="move"||po[x][y].status=="flyUp")
{
po[x][y].move(randomNum); //同上
xy[x][y]=po[x][y].xy;
repaint();
}
else if(po[x][y].status=="toSky")
{
po[x][y].move(randomNum); //同上
xy[x][y]=po[x][y].xy;
repaint();
}
else if(po[x][y].status=="complete")
{
winer(); //如果有玩家飞机到站了测试它是否有4个
}
}
public void winer() //判断玩家的是否赢了
{
if(complete[player]==4)
{
gameStatus=new String("over");
repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -