这是一个基于java开发的战机小游戏【游戏说明】↑↓←→:控制方向,可实现8个方向
Q: 开火
W: 大决
F2:复活
注:游戏一段时间后可以看到“礼品状”物体飞过,“吃”掉它可以增加大决数量
package cn.edu.ahu.RapidSurvial;import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.event.KeyEvent;public class Fighter {public static final int FWIDTH = 70;//战机宽度public static final int FHEIGHT = 10;//战机高度public static final int FXSPEED = 4;//战机在x方向上的速度public static final int FYSPEED = 4;//战机在y方向上的速度int x;//战机在x方向上的位置int y;//战机在y方向上的位置int w;//战机的宽度int h;//战机的高度Direction dir;//方向RapidSurvialManager rsm;//持有RapidSurvialManager的引用private boolean isUp = false;//键盘↑ 是否被按下,初始化为falseprivate boolean isDown = false;//键盘↓ 是否被按下,初始化为falseprivate boolean isRight = false;//键盘→ 是否被按下,初始化为falseprivate boolean isLeft = false;//键盘← 是否被按下,初始化为falseenum Direction {LTR, RTL}; //两个方向,LTR:从左向右,RTL:从右向左boolean isEnemy;//区分敌我的量,是敌人:true,否则为falseboolean isLive = true;//判读是否存活,活着:true,否则为falseprivate int lifeValue = 10;//我方战机的生命值int speed;//产生一个速度值BloodBar bb = new BloodBar();//可视的血量static int isRelive = 0;//是否复活private int superStarCounts = 1;//初始大决数private static Toolkit tk = Toolkit.getDefaultToolkit();private static Image[] fighterImage = null;static {fighterImage = new Image[] {tk.getImage(Fighter.class.getClassLoader().getResource("images/EnemysFighter.png")),tk.getImage(Fighter.class.getClassLoader().getResource("images/MyFighter_LTR.png"))};}//构造函数Fighter(int x , int y,boolean isEnemy) {this.x = x;this.y = y;this.w = FWIDTH;this.h = FHEIGHT;this.isEnemy = isEnemy;}//构造函数Fighter(int x, int y, boolean isEnemy, RapidSurvialManager rsm) {this(x, y, isEnemy);this.rsm = rsm;this.dir = Direction.LTR;}//构造函数Fighter(int x, int y,boolean isEnemy, RapidSurvialManager rsm, Direction dir, int speed) {this(x, y, isEnemy, rsm);this.dir = dir;this.speed = speed;}//设置lifeValue值public void setLifeValue(int lifeValue) {this.lifeValue = lifeValue;}//得到lifeValue值public int getLifeValue() {return lifeValue;}//设置superStarCounts值public void setSuperStarCounts(int superStarCounts) {this.superStarCounts = superStarCounts;}//得到superStarCounts值public int getSuperStarCounts() {return superStarCounts;}//用此方画出战机public void draw(Graphics g) {if(!isLive) {if(isEnemy) {rsm.enemys.remove(this);}return;}if(isEnemy) {g.drawImage(fighterImage[0], x, y, null);go();} else {g.drawImage(fighterImage[1], x, y, null);setPostion();bb.draw(g);}}//让敌军动起来的方法public void go() {switch(dir) {case LTR:x = speed;break;case RTL:x -= speed;break;}}//对按键被按下经行处理public void keyPressed(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_UP:isUp = true;break;case KeyEvent.VK_RIGHT:isRight = true;break;case KeyEvent.VK_DOWN:isDown = true;break;case KeyEvent.VK_LEFT:isLeft = true;break;}setPostion();}//对按键被释放经行处理public void keyReleased(KeyEvent e) {int key = e.getKeyCode();switch(key) {case KeyEvent.VK_UP:isUp = false;break;case KeyEvent.VK_RIGHT:isRight = false;break;case KeyEvent.VK_DOWN:isDown = false;break;case KeyEvent.VK_LEFT:isLeft = false;break;case KeyEvent.VK_Q:fire();break;case KeyEvent.VK_F2:if(!isLive) {isLive = true;lifeValue = 10;isRelive ;}break;case KeyEvent.VK_W:if(getSuperStarCounts() == 0) {return;}if(!isLive) {return;}setSuperStarCounts(getSuperStarCounts() - 1);superFire();break;}}//根据按键的组合确定下一次的位置private void setPostion() {if(isUp && !isRight && !isDown && !isLeft) {y -= FYSPEED;}if(!isUp && isRight && !isDown && !isLeft) {x = FXSPEED;}if(!isUp && !isRight && isDown && !isLeft) {y = FYSPEED;}if(!isUp && !isRight && !isDown && isLeft) {x -= FXSPEED;}if(isUp && isRight && !isDown && !isLeft) {x = FXSPEED;y -= FYSPEED;}if(isUp && !isRight && !isDown && isLeft) {y -= FYSPEED;x -= FXSPEED;}if(!isUp && isRight && isDown && !isLeft) {x = FXSPEED;y = FYSPEED;}if(!isUp && !isRight && isDown && isLeft) {x -= FXSPEED;y = FYSPEED;}//对战机的出界处理if(x <= 0) {x = 0;}if(y < 45) {y = 45;}if(x Fighter.FWIDTH > RapidSurvialManager.MAINWIDTH) {x = RapidSurvialManager.MAINWIDTH - Fighter.FWIDTH;}if(y Fighter.FHEIGHT 52> RapidSurvialManager.MAINHEIGHT) {y = RapidSurvialManager.MAINHEIGHT - Fighter.FHEIGHT - 52;}}//战机的开火处理public Bomb fire() {if(!isLive) {return null;}int x, y;if(!isEnemy) {x = this.x Fighter.FWIDTH;y = this.y Fighter.FHEIGHT / 2 - Bomb.BHEIGHT / 2 22;} else {x = this.x;y = this.y Fighter.FHEIGHT / 2 - Bomb.BHEIGHT / 2 22;}Bomb b = new Bomb(x, y, rsm, dir, isEnemy);rsm.bombs.add(b);return b; }//放大决的方法public SuperLine superFire() {if(!isLive) {return null;}SuperLine s = new SuperLine(x, rsm, dir);rsm.superLines.add(s);return s;}//得到自己的大小,用于碰撞检测public Rectangle getRect() {return new Rectangle(x, y, w, h*4);}//血块类private class BloodBar {public void draw(Graphics g) {Color c = g.getColor();if(lifeValue <= 5) {g.setColor(Color.RED);g.drawRect(x 20, y-20, w * 2 / 3, 4);int w = FWIDTH * lifeValue / 10;g.fillRect(x 20, y-20, w * 2 / 3, 4);g.drawString("" lifeValue, x, y-13);} else {g.setColor(Color.GREEN);g.drawRect(x 20, y-20, w * 2 / 3, 4);int w = FWIDTH * lifeValue / 10;g.fillRect(x 20, y-20, w * 2 / 3, 4);g.drawString("" lifeValue, x, y-13);g.setColor(c);}}}//吃SuperStar的方法public boolean eat(SuperStar ss) {if(this.isLive && ss.isLive&& this.getRect().intersects(ss.getRect())) {ss.isLive = false;setSuperStarCounts(getSuperStarCounts() 1);return true;}return false;}public int getScore() {return Bomb.sid - isRelive * 50;}}
评论