

免费预览已结束,剩余25页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java程序设计课程设计报告院别: 计算机学院 班级: 计科-1303bj 学号: 姓名: 指导教师: 日期: 2015/6/21 太空大战游戏1.课程设计的目的java语言是当今流行的网络编程语言,它具有面向对象、跨平台、分布应用等特点。面向对象的开发方法是当今世界最流行的开发方法,它不仅具有更贴近自然的语义,而且有利于软件的维护和继承。 为了进一步巩固课堂上所学到的知识,深刻把握java语言的重要概念及其面向对象的特性,锻炼学生熟练的应用面向对象的思想和设计方法解决实际问题的能力,开设了java程序设计课程设计。 通过此次课程设计,巩固所学java语言基本知识,增进java语言编辑基本功,掌握jcreator开发工具的运用,拓宽常用类库的应用。通过该教学环节与手段,把所学课程及相关知识加以融会贯通,全面掌握java语言的编程思想及面向对象程序设计的方法,为今后从事实际工作打下坚实的基础。怀着这样目的,通过在课本上学到的方法、网络上搜集的资料以及和同学的交流,慢慢做完及完善这个小游戏。2.主要程序类1). bullet(子弹类)package plane;import java.awt.color;import java.awt.graphics;import java.awt.rectangle;import util.constant;/* * 子弹类 * author lyp *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);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 */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.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, 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 plane.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 grtrect()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 =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;/* * 构造器 * 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.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 extends 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 miao=(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();bao=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_hight/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;case 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.setcolor(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) /system.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). 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;import .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) 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 myframe 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 windowclosing(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(t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 现代化信息技术在学院建设中的应用
- 舞蹈艺术探索
- 肺脓肿的考试试题及答案
- 2025劳动合同与就业协议劳动合同与就业协议的区别
- 2025年生活垃圾焚烧发电项目申请报告模范
- 基层中医药面试题及答案
- 2025房产租赁合同「标准版」
- 2025至2030年中国扇面螺丝行业投资前景及策略咨询报告
- 点胶机试题及答案
- 日语会考的试题及答案
- 2025国家开放大学《员工劳动关系管理》形考任务1234答案
- 土木工程力学(本)-001-国开机考复习资料
- 【MOOC】保险学概论-中央财经大学 中国大学慕课MOOC答案
- 2024届清华大学强基计划数学学科笔试试题(附答案)
- 2023年6月福建高考生物真题(含答案)
- 科技咨询师培训课件一(1)
- 汽车维修技术论文两篇
- 心理学基础试卷A
- 中国民航管理干部学院新生报名登记表
- 四川德昌县经济发展中存在的问题与对策研(改)
- 道路工程习题第十三章 道路工程图
评论
0/150
提交评论