




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、java程序设计课程设计报告院别: 计算机学院 班级: 计科-1303bj 学号: 姓名: 指导教师: 日期: 2015/6/21 太空大战游戏1.课程设计的目的java语言是当今流行的网络编程语言,它具有面向对象、跨平台、分布应用等特点。面向对象的开发方法是当今世界最流行的开发方法,它不仅具有更贴近自然的语义,而且有利于软件的维护和继承。 为了进一步巩固课堂上所学到的知识,深刻把握java语言的重要概念及其面向对象的特性,锻炼学生熟练的应用面向对象的思想和设计方法解决实际问题的能力,开设了java程序设计课程设计。 通过此次课程设计,巩固所学java语言基本知识,增进java语言编辑基本功,
2、掌握jcreator开发工具的运用,拓宽常用类库的应用。通过该教学环节与手段,把所学课程及相关知识加以融会贯通,全面掌握java语言的编程思想及面向对象程序设计的方法,为今后从事实际工作打下坚实的基础。怀着这样目的,通过在课本上学到的方法、网络上搜集的资料以及和同学的交流,慢慢做完及完善这个小游戏。2.主要程序类1). bullet(子弹类)package plane;import java.awt.color;import java.awt.graphics;import java.awt.rectangle;import util.constant;/* * 子弹类 * author ly
3、p *15/6/18 */public class bullet extends gameobjectdouble degree;public bullet()degree=math.random()*math.pi*2;x=constant.game_width/2;y=constant.game_hight/2;width=10;height=10;speed=3;public void draw(graphics g)color c=g.getcolor();g.setcolor(color.yellow);g.filloval(int)x,(int) y, width, height)
4、;x +=speed*math.cos(degree);y +=speed*math.sin(degree);if(yconstant.game_hight-height)degree=-degree;if(xconstant.game_width-width|x0)degree=math.pi-degree;g.setcolor(c);2). explode(爆炸类)package plane;import java.awt.graphics;import java.awt.image;import util.gameutil;/* * 爆炸类 * author lyp *15/6/18 *
5、/public class explode double x,y;static image imgs=new image16;static for(int i=0;i16;i+)imgsi=gameutil.getimage(images/copy/e+(i+1)+.gif);imgsi.getwidth(null);int count;public void draw(graphics g)if(count=15)g.drawimage(imgscount, (int)x, (int)y, null);count+;public explode(double x,double y)this.
6、x=x;this.y=y;3).游戏环境(gameobject)package plane;import java.awt.image;import java.awt.rectangle;public class gameobject image img;double x,y;double speed=11;int width,height;/判断矩形public rectangle grtrect()return new rectangle(int)x,(int)y,width,height);public gameobject(image img, double x, double y,
7、double speed, int width,int height) super();this.img = img;this.x = x;this.y = y;this.speed = speed;this.width = width;this.height = height;public gameobject()4). plane1(飞机类)package plane;import java.awt.graphics;import java.awt.image;import java.awt.rectangle;import java.awt.event.keyevent;import p
8、lane.planegameframe.keymonitor;import util.gameutil;/* * 飞机类 * author lyp *15/6/18 */public class plane1 extends gameobjectboolean left,right,up,down;private boolean live=true;int width,height;public void draw(graphics g)if(live)g.drawimage(img,(int)x, (int)y,null);move();/判断矩形public rectangle grtre
9、ct()return new rectangle(int)x,(int)y,width,height);/飞机移动方法public void move()if(left)x -=speed;if(right)x +=speed;if(up)y -=speed;if(down)y +=speed;/方向上的加减public void adddirection(keyevent e)switch (e.getkeycode() case 37:left =true;break;case 38:up =true;break;case 39:right =true;break;case 40:down
10、 =true;break;default:break;public void reducedirection(keyevent e)switch (e.getkeycode() case 37:left =false;break;case 38:up =false;break;case 39:right =false;break;case 40:down =false;break;default:break;public boolean islive() return live;public void setlive(boolean live) this.live = live;/* * 构造
11、器 * param imgpath * param x * param y */public plane1(string imgpath, double x, double y) this.img = gameutil.getimage(imgpath);this.width=img.getwidth(null);this.height=img.getheight(null);this.x = x;this.y = y;public plane1()5).飞机游戏窗口(planegameframe)package plane;import java.awt.color;import java.
12、awt.font;import java.awt.graphics;import java.awt.image;import java.awt.event.keyadapter;import java.awt.event.keyevent;import java.util.arraylist;import java.util.date;import util.constant;import util.gameutil;import util.myframe;/* * 飞机游戏窗口 * author lyp *15/6/19 */public class planegameframe exten
13、ds myframeimage bg=gameutil.getimage(image/bg.jpg);plane1 pl=new plane1(image/plane.png, 550, 550);arraylist bulletlist=new arraylist();date starttime;date endtime;date reporttime;explode bao;public void paint(graphics g)g.drawimage(bg, 0, 0, null);pl.draw(g);if(bao=null)reporttime=new date();int mi
14、ao=(int)(reporttime.gettime()-starttime.gettime()/1000);gameover(g, 时间:+miao+s, 20, 700, 50,color.white);for(int i=0;ibulletlist.size();i+)bullet b=(bullet)bulletlist.get(i);b.draw(g);/检测飞机的碰撞boolean boom=b.grtrect().intersects(pl.grtrect();if(boom)pl.setlive(false);if(bao=null)endtime=new date();ba
15、o=new explode(pl.x, pl.y);bao.draw(g);if(!pl.islive()int period=(int)(endtime.gettime()-starttime.gettime()/1000);gameover(g, 时间:+period+s,30, constant.game_width/2-60,constant.game_hight/2+50, color.white);switch (period/10) case 0:case 1:gameover(g, 菜鸟, 60,constant.game_width/2-60,constant.game_hi
16、ght/2,color.white);break;case 2:gameover(g, 还行, 60,constant.game_width/2-60,constant.game_hight/2,color.white);break;case 3:gameover(g, 不错, 60,constant.game_width/2-60,constant.game_hight/2,color.blue);break;case 4:gameover(g, 很好, 60,constant.game_width/2-100,constant.game_hight/2,color.red);break;c
17、ase 5:gameover(g, 太棒了, 60,constant.game_width/2-60,constant.game_hight/2,color.red);break;default:gameover(g, 高手, 60,constant.game_width/2-60,constant.game_hight/2,color.red);break;/if(pl.islive()public void gameover(graphics g,string str,int size,int x,int y,color color)color c=g.getcolor();g.setco
18、lor(color);font f=new font(宋体, font.bold, size);g.setfont(f);g.drawstring(str,x, y);g.setcolor(c);/内部类,可以方便使用外部类的普通属性class keymonitor extends keyadapteroverridepublic void keypressed(keyevent e) /system.out.println(按下+e.getkeycode();pl.adddirection(e);overridepublic void keyreleased(keyevent e) /sys
19、tem.out.println(释放+e.getkeycode();pl.reducedirection(e);public void lunchframe()super.lunchframe();/增加键盘的监听addkeylistener(new keymonitor();/生成子弹for(int i=0;i=50;i+)bullet b=new bullet();bulletlist.add(b);starttime=new date();public static void main(string args) new planegameframe().lunchframe();6).
20、constant(游戏中常量类)package util;/* * 游戏项目中常量类 * author lyp *15/6/19*/public class constant public static final int game_width=800;public static final int game_hight=600;7). gameutil(游戏开发中常用的工具类(如图片加载))package util;import java.awt.image;import java.awt.image.bufferedimage;import java.io.ioexception;impo
21、rt .url;import javax.imageio.imageio;/* * 游戏开发中常用的工具类(如图片加载) * author lyp *15/6/20 */public class gameutil private gameutil() /工具类构造器一般私有public static image getimage(string path)url u=gameutil.class.getclassloader().getresource(path);bufferedimage bf=null;try bf=imageio.read(u); catch (ioexception e
22、) e.printstacktrace();return bf;8). myframe(共同的类的父类(如窗口加载))package util;/* * 共同的类的父类(如窗口加载) * 15/4/19 */import java.awt.color;import java.awt.font;import java.awt.frame;import java.awt.graphics;import java.awt.image;import java.awt.event.windowadapter;import java.awt.event.windowevent;public class m
23、yframe extends frameprivate image ibuffer; /为解决闪烁而设定的两个私有变量,private graphics gbuffer; /* * 加载窗口 */public void lunchframe()setsize(constant.game_width,constant.game_hight);setlocation(100,100);setvisible(true);new paintthread().start();/重画窗口addwindowlistener(new windowadapter() overridepublic void wi
24、ndowclosing(windowevent e) system.exit(0););/* * 重新定义updata,解决闪烁,双缓冲 */public void update(graphics scr) if(ibuffer=null) ibuffer=createimage(this.getsize().width,this.getsize().height); gbuffer=ibuffer.getgraphics(); gbuffer.setcolor(getbackground(); gbuffer.fillrect(0,0,this.getsize().width,this.getsize().height); paint(gbuffer); scr.drawimage(ibuffer,0,0,this);/* * 定义一个重画窗口的线程类,内部类 * author sl * */class paintthread extends threadpublic void run ()while(tr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高速公路智能交通系统2025年智能交通系统与智慧交通应用报告
- 基于5G商用深化2025年边缘计算行业应用案例分析报告
- 烧烤场地外租赁合同协议
- 消防验收咨询费合同范本
- 闲置水泥仓收购合同范本
- 猫咪寄养健康协议书模板
- 铸造承包合同协议书范本
- 长期合作的物流合同范本
- 项目部采购护栏合同范本
- 生物质燃料采购合同协议
- 汽车散热器安装施工方案与技术措施
- 山西省烟草专卖局(公司)笔试试题2024
- 江苏扬州经济技术开发区区属国有企业招聘笔试真题2024
- 华为emt轮岗管理制度
- Alport综合征基因诊断
- 搜身带离技术课件
- 光伏电站设备移交生产验收标准
- DB31/ 890-2015公共游泳场所卫生管理规范
- 无人机打药合同协议书
- 学习解读《水利水电建设工程验收规程》SLT223-2025课件
- 2025山西国晋物业服务限公司及下属企业招聘(34人)易考易错模拟试题(共500题)试卷后附参考答案
评论
0/150
提交评论