大学课程设计报告-飞机大战

大学课程设计报告-飞机大战
大学课程设计报告-飞机大战

湖北大学本科课程设计

题目Java课程设计——飞机大战

姓名学号

专业年级

指导教师职称

2015年12月18日

----目录---- 一.项目介绍-------------------------------- 1二.概要设计

2.1资源需求------------------------------ 1

2.2游戏流程------------------------------ 1三.类设计

3.1游戏界面类---------------------------- 2

3.2飞行物类------------------------------ 2

3.3敌机类-------------------------------- 2

3.4蜜蜂类-------------------------------- 3

3.5玩家飞机类----------------------------- 3

3.6子弹类-------------------------------- 4四.编码分析

4.1游戏界面类---------------------------- 4

4.2飞行物类------------------------------ 11

4.3敌机类-------------------------------- 12

4.4蜜蜂类-------------------------------- 13

4.5玩家飞机类----------------------------- 13

4.6子弹类-------------------------------- 15五.游戏测试画面----------------------------- 16六.总结------------------------------------ 18

针对Java课程设计,我做了一个小游戏——飞机大战,游戏代码包含到本学期所学的所有知识点。

程序运行后,进入到开始画面,鼠标单击开始游戏。敌机自上向下移动,随机出现,玩家机随鼠标移动并发射子弹,消灭敌机可以获得分数,随机出现小蜜蜂,消灭后可获得奖励。

二.概要设计

2.1资源需求

此游戏需要导入图片:背景图片,开始界面,玩家飞机,敌机,小蜜蜂,子弹,暂停界面,结束界面。

2.2游戏流程

游戏界面:

ShootGame extends JPanel

static块:导入图片

main():创建窗口

重写paint():画图

action():鼠标事件

TimerTask重写run():游戏运行的活动飞行物类:

abstract FlyingObject

属性:x,y坐标,image,图片长宽

move():飞行物移动

outOfbound():飞行物出界

shootBy():子弹击中飞行物

敌机类:

Airplane extends FlyingObject

Int speed:移动速度

重写move()

重写outOfBound()

getScore():击中敌机后得分

Airplane():初始化敌机

Bee extends FlyingObject

Int xSpeed,ySpeed :移动速度

Int awardType:奖励类型(双倍活力或加命)

Bee():初始化蜜蜂

重写move()

重写outOfBound()

getType():获取奖励类型

玩家飞机类:

Player extends FlyingObject

Int life,doubleFire:生命,双倍火力

Player():初始化玩家

重写move():换图片,形成飞机的动态效果

重写outOfBound()

shoot():生成子弹

moveTo():玩家机移动

isHit():玩家碰撞到飞行物

setDoubleFire():设置双倍火力

addDoubleFire():奖励双倍火力

addLife():奖励生命

deleteLife():减命

getLife():获取生命数

Bullet extends FlyingObject

Int speed:移动速度

Bullet():初始化子弹

重写move()

重写outOfBound()

四.编码分析

(一)ShootGame类

此类继承JPanel类构建游戏窗口并控制游戏的运行类的成员变量:

public static final int WIDTH=400;//窗口宽

public static final int HEIGHT=600;//窗口高

//图片属性

public static BufferedImage airplane;

public static BufferedImage background;

public static BufferedImage bee;

public static BufferedImage bullet;

public static BufferedImage gameover;

public static BufferedImage player0;

public static BufferedImage player1;

public static BufferedImage pause;

public static BufferedImage start;

public static final int DOUBLE_FIRE=0;//双倍火力的属性为0

public static final int LIFE=1;//奖励生命的属性为1

public Player player=new Player();//创建玩家对象

private Bullet[] bullets={};//创建子弹对象(当前为空)

private FlyingObject[] flyings={};//创建飞行物对象(当前为空)

public static final int START=0;//状态:开始为0

public static final int RUNNING=1;//状态:运行为1

public static final int PAUSE=2;//状态:暂停为2

public static final int GAME_OVER=3;//状态:游戏结束为3

private int state=0;//当前状态

1.static块

静态块,在类加载时导入游戏所需的图片

static{

try {

airplane=ImageIO.read(ShootGame.class.getResource("airplane.png"));

background=ImageIO.read(ShootGame.class.getResource("background.pn g"));

bee=ImageIO.read(ShootGame.class.getResource("bee.png"));

bullet=ImageIO.read(ShootGame.class.getResource("bullet.png"));

gameover=ImageIO.read(ShootGame.class.getResource("gameover.png"));

pause=ImageIO.read(ShootGame.class.getResource("pause.png"));

start=ImageIO.read(ShootGame.class.getResource("start.png")); player0=ImageIO.read(ShootGame.class.getResource("player0.png")); player1=ImageIO.read(ShootGame.class.getResource("player1.png")); } catch (Exception e) {

e.printStackTrace();

}

}

2.main()

在main方法中创建窗口

public static void main(String[] args) {

JFrame frame=new JFrame("ShootGame");

ShootGame game=new ShootGame();

frame.add(game);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocation(400, 100);

frame.setAlwaysOnTop(true);

frame.setVisible(true);

frame.setSize(WIDTH, HEIGHT);

game.action();

}

3.paint()

//画图(g是画笔)

相关主题
相关文档
最新文档