x战机java_java战机游戏源码(含设计报告)

【实例简介】这是一个基于java开发的战机小游戏

【游戏说明】

↑↓←→:控制方向,可实现8个方向

Q: 开火

W: 大决

F2:复活

注:游戏一段时间后可以看到“礼品状”物体飞过,“吃”掉它可以增加大决数量

【实例截图】

78153fbf628644db5e020b0bb7d53c0b.png

2c508a6261f84629155ccfe4c26c2dbe.png

【核心代码】

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;//键盘↑ 是否被按下,初始化为false

private boolean isDown = false;//键盘↓ 是否被按下,初始化为false

private boolean isRight = false;//键盘→ 是否被按下,初始化为false

private boolean isLeft = false;//键盘← 是否被按下,初始化为false

enum Direction {LTR, RTL}; //两个方向,LTR:从左向右,RTL:从右向左

boolean isEnemy;//区分敌我的量,是敌人:true,否则为false

boolean isLive = true;//判读是否存活,活着:true,否则为false

private 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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值