大学课程设计飞机大战文档格式.docx
- 文档编号:13299500
- 上传时间:2022-10-09
- 格式:DOCX
- 页数:29
- 大小:122.71KB
大学课程设计飞机大战文档格式.docx
《大学课程设计飞机大战文档格式.docx》由会员分享,可在线阅读,更多相关《大学课程设计飞机大战文档格式.docx(29页珍藏版)》请在冰豆网上搜索。
4.4蜜蜂类--------------------------------13
4.5玩家飞机类-----------------------------13
4.6子弹类--------------------------------15
5.游戏测试画面-----------------------------16
6.总结------------------------------------18
一.项目介绍
针对Java课程设计,我做了一个小游戏——飞机大战,游戏代码包含到本学期所学的所有知识点。
程序运行后,进入到开始画面,鼠标单击开始游戏。
敌机自上向下移动,随机出现,玩家机随鼠标移动并发射子弹,消灭敌机可以获得分数,随机出现小蜜蜂,消灭后可获得奖励。
二.概要设计
2.1资源需求
此游戏需要导入图片:
背景图片,开始界面,玩家飞机,敌机,小蜜蜂,子弹,暂停界面,结束界面。
2.2游戏流程
单击鼠标
鼠标移出
单击鼠标
玩家死亡
三.程序结构
游戏界面:
ShootGameextendsJPanel
static块:
导入图片
main():
创建窗口
重写paint():
画图
action():
鼠标事件
TimerTask重写run():
游戏运行的活动
飞行物类:
abstractFlyingObject
属性:
x,y坐标,image,图片长宽
move():
飞行物移动
outOfbound():
飞行物出界
shootBy():
子弹击中飞行物
敌机类:
AirplaneextendsFlyingObject
Intspeed:
移动速度
重写move()
重写outOfBound()
getScore():
击中敌机后得分
Airplane():
初始化敌机
蜜蜂类:
BeeextendsFlyingObject
IntxSpeed,ySpeed:
IntawardType:
奖励类型(双倍活力或加命)
Bee():
初始化蜜蜂
getType():
获取奖励类型
玩家飞机类:
PlayerextendsFlyingObject
Intlife,doubleFire:
生命,双倍火力
Player():
初始化玩家
重写move():
换图片,形成飞机的动态效果
shoot():
生成子弹
moveTo():
玩家机移动
isHit():
玩家碰撞到飞行物
setDoubleFire():
设置双倍火力
addDoubleFire():
奖励双倍火力
addLife():
奖励生命
deleteLife():
减命
getLife():
获取生命数
子弹类:
BulletextendsFlyingObject
Bullet():
初始化子弹
四.编码分析
(1)ShootGame类
此类继承JPanel类构建游戏窗口并控制游戏的运行
类的成员变量:
publicstaticfinalintWIDTH=400;
//窗口宽
publicstaticfinalintHEIGHT=600;
//窗口高
//图片属性
publicstaticBufferedImageairplane;
publicstaticBufferedImagebackground;
publicstaticBufferedImagebee;
publicstaticBufferedImagebullet;
publicstaticBufferedImagegameover;
publicstaticBufferedImageplayer0;
publicstaticBufferedImageplayer1;
publicstaticBufferedImagepause;
publicstaticBufferedImagestart;
publicstaticfinalintDOUBLE_FIRE=0;
//双倍火力的属性为0
publicstaticfinalintLIFE=1;
//奖励生命的属性为1
publicPlayerplayer=newPlayer();
//创建玩家对象
privateBullet[]bullets={};
//创建子弹对象(当前为空)
privateFlyingObject[]flyings={};
//创建飞行物对象(当前为空)
publicstaticfinalintSTART=0;
//状态:
开始为0
publicstaticfinalintRUNNING=1;
//状态:
运行为1
publicstaticfinalintPAUSE=2;
暂停为2
publicstaticfinalintGAME_OVER=3;
游戏结束为3
privateintstate=0;
//当前状态
1.static块
静态块,在类加载时导入游戏所需的图片
static{
try{
airplane=ImageIO.read(ShootGame.class.getResource("
airplane.png"
));
background=ImageIO.read(ShootGame.class.getResource("
background.png"
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(Exceptione){
e.printStackTrace();
}
2.main()
在main方法中创建窗口
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame("
ShootGame"
);
ShootGamegame=newShootGame();
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是画笔)
publicvoidpaint(Graphicsg){
g.drawImage(background,0,0,null);
paintPlayer(g);
//画玩家飞机
paintFlyings(g);
//画飞行物
paintBullets(g);
//画子弹
paintScore(g);
//画分数
paintState(g);
//画游戏状态
//画每一个子弹
privatevoidpaintBullets(Graphicsg){
for(inti=0;
i<
bullets.length;
i++){
Bulletb=bullets[i];
g.drawImage(b.image,b.x,b.y,null);
//画飞行物(敌机,蜜蜂)
privatevoidpaintFlyings(Graphicsg){
for(inti=0;
i<
flyings.length;
i++){
FlyingObjectflying=flyings[i];
g.drawImage(flying.image,flying.x,flying.y,null);
//画玩家
privatevoidpaintPlayer(Graphicsg){
g.drawImage(player.image,player.x,player.y,null);
publicvoidpaintScore(Graphicsg){
g.setColor(Color.RED);
//设置画笔颜色为红
g.setFont(newFont(Font.SANS_SERIF,Font.BOLD,20));
//设置字体,加粗,字号
g.drawString("
Score:
"
+score,10,25);
Life:
+player.getLife(),10,45);
//画状态
publicvoidpaintState(Graphicsg){
switch(state){
caseSTART:
g.drawImage(start,0,0,null);
break
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 大学 课程设计 飞机 大战