📄 game.java
字号:
catch (Exception e)
{
e.printStackTrace();
}
repaint();
}
else if (gamestate==Utility.GAME_PAUSE)
{
synchronized (this)
{
try
{
wait();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
else if (gamestate==Utility.GAME_OVER)
{
break;
}
}
}
/**
* 移动背景地图
*/
public void moveBackground()
{
y += 1; //调整背景移动的速度
totalY++;
Utility.CURRENT_Y = totalY;
if (y>bgHeight)
{
y = 0;
}
}
/**
* 检查敌机是否出现
*/
public void checkEnemyVisible()
{
for (int i = 0; i<enemyPlanes.size(); i++)
{
Enemy e = (Enemy)enemyPlanes.elementAt(i);
int size = e.width;
if (e.startY<(Utility.GAME_HEIGHT+totalY+size)) //这里判断的标准是当整个飞机完全出现时,属性设置为真
{
e.setVisible(true);
}
else
{
return; //因为飞机在文件中是根据startY升序排好的,如过该飞机不出现,则后面的肯定不出现
}
}
}
/**
* 处理游戏的逻辑(飞机的移动,碰撞检测等)
*/
public void doGameLogic()
{
checkEnemyVisible();
//英雄飞机的移动
if (heroPlane.getVisible())
{
//获取用户输入,用于计算飞机的移动以及子弹的发射
getInput();
//英雄飞机的边界检查
heroPlane.checkEdge();
if (heroPlane.getUnbeatable())
{
unbeatTime++;
if (unbeatTime>=100)
{
heroPlane.setUnbeatable(false);
unbeatTime = 0;
}
}
}
else
{
//如果爆炸已经结束
if (planeBlast.size()==0)
{
//如果heroNumner小于零,则游戏结束
if (heroNumber<=0)
{
gamestate = Utility.GAME_OVER;
return;
}
else
{
newPlaneTime++;
if(newPlaneTime>=40)
{
heroPlane = new Plane();
newPlaneTime = 0;
}
}
}
}
//奖品移动
for (int i = 0; i<award.size(); i++)
{
Award a = (Award)award.elementAt(i);
a.move();
a.checkEdge();
//英雄是否吃到奖品
if (heroPlane.isHit(a.x, a.y, a.width, a.height))
{
//吃到奖品的声音
heroPlane.setAward(a.kind);
a.setVisible(false);
new PlaySound("Audio/eat.wav");
}
if (!a.getVisible())
{
award.remove(i);
}
}
//英雄飞机爆炸效果的计算
for (int i = 0; i<planeBlast.size(); i++)
{
Blast b = (Blast)planeBlast.elementAt(i);
if (b.index==0)
{
new PlaySound("Audio/hit.wav");
}
b.next();
if (b.getIsOver())
{
planeBlast.remove(i);
}
}
//敌机爆炸效果的计算
for (int i = 0; i<blast.size(); i++)
{
Blast b = (Blast)blast.elementAt(i);
if (b.index==0)
{
new PlaySound("Audio/hit.wav");
}
b.next();
if (b.getIsOver())
{
blast.remove(i);
}
}
//敌机的子弹移动以及判断敌机子弹是否和英雄飞机相撞
for (int i = 0; i<enemyBullets.size(); i++)
{
Bullet b1 = (Bullet)enemyBullets.get(i);
b1.move();
b1.checkEdge();
if (heroPlane.getVisible() && heroPlane.isHit(b1.x+b1.width/2, b1.y+b1.height/2, b1.width/2)) //敌机子弹图片的大小为8*8
{
if (!heroPlane.getUnbeatable())
{
heroPlane.desLife(b1.power);
if (heroPlane.getLife()<=0)
{
heroPlane.setVisible(false);
planeBlast.addElement(new Blast(heroPlane.x,heroPlane.y, heroPlane.width, heroPlane.height,8));
heroNumber--;
}
}
b1.setVisible(false);
}
if (!b1.getVisible())
{
enemyBullets.remove(i);
}
}
//敌机移动并判断是否发射子弹,敌机是否和英雄飞机相撞
for (int i = 0; i<enemyPlanes.size(); i++)
{
Enemy e1 = (Enemy)enemyPlanes.get(i);
if (e1.getVisible())
{
e1.move();
e1.checkEdge();
if (heroPlane.getVisible() && heroPlane.isHit(e1.x, e1.y, e1.width, e1.height))
{
if (!heroPlane.getUnbeatable())
{
planeBlast.addElement(new Blast(heroPlane.x,heroPlane.y, heroPlane.width, heroPlane.height,8));
heroPlane.setLife(0);
heroPlane.setVisible(false);
heroNumber--;
}
blast.addElement(new Blast(e1.x, e1.y, e1.width, e1.height,8));
e1.setVisible(false);
e1.setDead(true);
//奖品
Award a = e1.getAward();
if (a!=null)
{
award.add(a);
}
}
else if (e1.fire())
{
Object[] b = e1.getBullets();
for (int k = 0; k<b.length; k++)
{
enemyBullets.addElement((Bullet)b[k]);
}
}
}
if (e1.getDead())
{
enemyPlanes.remove(i);
}
}
//英雄子弹的移动
for (int i = 0; i<heroBullets.size(); i++)
{
Bullet b1 = (Bullet)heroBullets.get(i);
b1.move();
b1.checkEdge();
//英雄子弹和敌机的碰撞检测
for (int j = 0; j<enemyPlanes.size(); j++)
{
Enemy emy = (Enemy)enemyPlanes.get(j);
if (emy.getVisible())
{
if (emy.isHit(b1.x+b1.width/2, b1.y+b1.height/2, b1.width/2)) //英雄子弹的大小是16*16
{
//增加分数 将子弹和敌机分别设为不可显示 添加爆炸效果
b1.setVisible(false);
heroPlane.addScore(emy.life);
emy.decLife(b1.power);
if (emy.getLife()<=0)
{
emy.setVisible(false);
emy.setDead(true);
blast.addElement(new Blast(emy.x, emy.y, emy.width,emy.height, 8)); //爆炸
Award a = emy.getAward(); //奖品
if (a!=null)
{
award.add(a);
}
}
break;
}
}
}
if (!b1.getVisible())
{
heroBullets.remove(i);
}
}
}
/**
* 计算游戏的FPS
*/
public void caculateFPS()
{
long endTime = System.currentTimeMillis();
fps++;
if ((endTime-startTime)>1000)
{
startTime = endTime;
fpsStr = "FPS: "+String.valueOf(fps); //通过字符串记录相对恒定的FPS
fps = 0;
}
}
/**
* 获取用户的输入信息,支持多键
*/
public void getInput()
{
if (keys[KeyEvent.VK_A])
{
heroPlane.move(Utility.MOVE_LEFT);
}
if (keys[KeyEvent.VK_D])
{
heroPlane.move(Utility.MOVE_RIGHT);
}
if (keys[KeyEvent.VK_S])
{
heroPlane.move(Utility.MOVE_DOWN);
}
if (keys[KeyEvent.VK_W])
{
heroPlane.move(Utility.MOVE_UP);
}
if (keys[KeyEvent.VK_K])
{
Object[] b = heroPlane.getBullets();
for (int i = 0; i<b.length; i++)
{
heroBullets.add((Bullet)b[i]);
}
new PlaySound("Audio/fire.wav");
}
}
/**
* 设置按键的状态
* @param key 按键的虚拟键值
* @param value 为真表示该键正被按下,为假则没有按
*/
public void setKeyState(int key, boolean value)
{
keys[key] = value;
}
/**
* 键盘事件的处理
*/
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key==KeyEvent.VK_ESCAPE)
{
System.exit(0);
return;
}
if (gamestate==Utility.GAME_START) //相应4种按键,可进入3中状态
{
//菜单按键的逻辑控制
if (key==KeyEvent.VK_DOWN)
{
menuIndex++;
if (menuIndex==menuIndexDes.length)
{
menuIndex = 0;
}
}
else if (key==KeyEvent.VK_UP)
{
menuIndex--;
if (menuIndex==-1)
{
menuIndex = menuIndexDes.length-1;
}
}
else if (key==KeyEvent.VK_ENTER)
{
if (menuIndex==Utility.MENU_START)
{
gamestate = Utility.GAME_RUNNING;
bgThread.start(); //启动线程
}
else if (menuIndex==Utility.MENU_HELP)
{
gamestate = Utility.KEY_HELP;
}
else if (menuIndex==Utility.MENU_EXIT)
{
System.exit(0);
}
}
repaint();
}
else if (gamestate==Utility.KEY_HELP)
{
if (key==KeyEvent.VK_BACK_SPACE)
{
gamestate = Utility.GAME_START;
menuIndex = Utility.MENU_START;
}
repaint();
}
else if (gamestate==Utility.GAME_RUNNING)
{
if (key==KeyEvent.VK_P)
{
gamestate = Utility.GAME_PAUSE;
}
else
{
setKeyState(key, true);
}
}
else if (gamestate==Utility.GAME_PAUSE)
{
if (key==KeyEvent.VK_R)
{
try
{
synchronized (this)
{
gamestate = Utility.GAME_RUNNING;
notify();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
setKeyState(key, false);
}
public void keyTyped(KeyEvent e)
{}
public void mouseEntered(MouseEvent e)
{
requestFocus();
}
public void mouseExited(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -