Java弹球游戏实验报告chen.docx
《Java弹球游戏实验报告chen.docx》由会员分享,可在线阅读,更多相关《Java弹球游戏实验报告chen.docx(15页珍藏版)》请在冰豆网上搜索。
Java弹球游戏实验报告chen
Java弹球游戏实验报告—chen
题目弹球小游戏
姓名方成
学号20
专业java
指导教师陈华恩
2013年12月30
一、实验目的...........................................................................................................................3二、需求分析...........................................................................................................................3三、实验任务...........................................................................................................................3
1、设计.............................................................................................................................3
2、程序要求:
.................................................................................................................3
3、选作题:
.....................................................................................................................3四、开发工具与平台...............................................................................................................3五、设计思路...........................................................................................................................4
1、界面设计.....................................................................................................................4
2、逻辑设计.....................................................................................................................4
3、程序测试.....................................................................................................................4六、实验总结...........................................................................................................................5七、程序代码...........................................................................................................................5八、参考文献.........................................................................................................................11
1.《疯狂java讲义》...................................................................................................12
2.《算法导论》.............................................................................................................12
3.《java编程思想》....................................................................................................12
一、实验目的
1、熟练掌握java面向对象编程。
2、掌握Swing图形用户界面编程以及事件处理等,掌握java绘图技术。
3、掌握timer类的灵活使用
4、培养独立查找资料,并解决问题的能力。
二、需求分析
经典的碰撞球是一个的古老游戏,目的是在训练人的反应能力。
只有通过把所有的砖块消除完,才能顺利的完成任务。
游戏要求如下:
1、实现球速度的随机性
2、实现球碰撞到边缘或者砖块自动反弹
3、实现游戏可以随时暂停
4、实现游戏结束后能重新开始游戏
三、实验任务
1、设计
设计并编程实现弹球程序:
用户能通过菜单或者按钮新增一小球,该小球将从随机的位置出现,并具有随机颜色,随机速度以及随机的运动方向,小球沿初始方向匀速运动,当碰到窗口边缘时,小球将依据受力原理改变运动方向(可简化考虑,受力只改变小球的运动方向,小球仍按照初始速度匀速运动,且不考虑小球之间的碰撞)。
2、程序要求:
(1)具备相应界面,并通过事件编程,实现相应的菜单或者按钮功能。
(2)使用timer,在程序窗口区域绘制小球,并以线程控制小球的移动,实现
动画效果。
3、选作题:
(1)实现奖励机制及关卡机制
四、开发工具与平台
JDK1.6+eclipse
五、设计思路
1、界面设计
首先建立窗口类继承自JFrame。
该界面为主界面。
在加上按钮—开始与暂停
在加入JPanel面板,用于图形界面.
、逻辑设计2
程序用过响应timer事件,按钮事件及键盘事件来管理程序的运行。
程序开始
窗口运行,等待响应用户消息
游戏开始,小球开始移退出动
移动速度在每次反弹后
发生变化。
如果小球碰倒移动球拍来使小边界或者砖块,小球方向球不至于掉到球发生变化拍下面
若某个小球掉到球拍下
面,游戏结束。
按开始按
钮则重新开始游戏
3、程序测试
六、实验总结
1、这次实验综合运用了GUI编程,使我对多线程编程有了一定的了解,对java的多线程机制有了一点理解,更加熟练的运用swing组件设计界面,响应消息,接口的实现等等。
2、通过课程设计的编程和对编程过程中遇到的问题的思考,切实的提高了我的编程能力,通过这次课程设计,我巩固所了学过的知识,能用JAVA熟练地运用其中的一些,进一步掌握了各个指令的功能是转向。
七、程序代码
packagecom.fangcheng.ball.game;
importjava.awt.Font;
importjava.awt.event.*;
importjavax.swing.*;
importjava.util.ArrayList;importjava.util.Random;
publicclassBallGameextendsJFrameimplementsActionListener,KeyListener{
privateJPanelp;
privateJButtonstart;
privateJButtonstop;
privateJLabelball;
privateJLabelqiupai;
用来放置背景图片的JLabelprivateJLabelbg;//
privateJLabelwin;
privateJLabelover;
privateintx1=50;
privateintx2=15;
privateinty1=325;
privateinty2=340;//球与球拍的初始位置
privateTimertime;//周期性触发球的移动事件的timer对象
privatebooleanisRight=false;//球是否向右移动
privatebooleanisDow=false;//球是否为下降
privatebooleanisOver=false;//是否游戏结束
privatebooleanisYi=false;//球拍是否能移动
privateintxsudu=3;
privateintysudu=3;//球的x,y速度,初始值为3
privateRandomran=newRandom();//用于随机产生球的x,y速度
privateArrayListremoveBrickList;//被移除的砖块的list
privateArrayListbrickList;//游戏时砖块的list
privateFontfont;
/**
*构造器,参数为窗口标题
*@params
*/
publicBallGame(Strings){
super(s);
gameShow();
}
/**
*用于游戏窗口绘制的方法
*/
publicvoidgameShow(){
//初始化组件
ImageIconimg1=new
ImageIcon(BallGame.class.getResource("/images/ball.png"));
ImageIconimg2=new
ImageIcon(this.getClass().getResource("/images/pa.png"));
ImageIconimg3=new
ImageIcon(this.getClass().getResource("/images/kuang.png"));
ImageIconimg4=new
ImageIcon(this.getClass().getResource("/images/win.gif"));
ImageIconimg5=new
ImageIcon(this.getClass().getResource("/images/over.png"));
ImageIconimg6=new
ImageIcon(this.getClass().getResource("/images/brick.gif"));
ImageIconimg7=new
ImageIcon(this.getClass().getResource("/images/brick1.png"));
ImageIconimg8=new
ImageIcon(this.getClass().getResource("/images/brick2.png"));
font=newFont("微软雅黑",Font.PLAIN,12);
ball=newJLabel(img1);
qiupai=newJLabel(img2);
bg=newJLabel(img3);
win=newJLabel(img4);
over=newJLabel(img5);
p=newJPanel();
start=newJButton("开始");
stop=newJButton("暂停");
start.setFont(font);
stop.setFont(font);
time=newTimer(10,this);
brickList=newArrayList();
removeBrickList=newArrayList();
//初始化砖块
for(inti=0;i<27;i++){
JLabeltemp;
if(i%6==0)//用来给砖块加载不同的图片
temp=newJLabel(img6);
elseif(i%6==1)
temp=newJLabel(img7);
elseif(i%6==2)
temp=newJLabel(img8);
elseif(i%6==3)
temp=newJLabel(img7);
elseif(i%6==4)
temp=newJLabel(img8);
else
temp=newJLabel(img6);
brickList.add(temp);
}
//将砖块添加至面板
for(inti=0;iif(i<9){
brickList.get(i).setBounds(25*i+10,15,20,20);
}elseif(i<18){
brickList.get(i).setBounds(25*(i-9)+10,40,20,20);
}elseif(i<27){
brickList.get(i).setBounds(25*(i-18)+10,65,20,20);
}
p.add(brickList.get(i));
}
//置空布局管理器
this.setLayout(null);
p.setLayout(null);
//绝对位置布局组件
p.setBounds(0,0,240,500);
start.setBounds(50,400,60,30);
stop.setBounds(140,400,60,30);
this.setBounds(450,100,246,465);
ball.setBounds(x1,y1,img1.getIconWidth(),img1.getIconHeight());
qiupai.setBounds(x2,y2,img2.getIconWidth(),img2.getIconHeight());
bg.setBounds(0,0,img3.getIconWidth(),img3.getIconHeight());
over.setBounds(10,30,img4.getIconWidth(),img4.getIconHeight());
win.setBounds(10,30,img5.getIconWidth(),img5.getIconHeight());
//先将胜利与结束标签设为不可见
over.setVisible(false);
win.setVisible(false);
//添加监视器
start.addActionListener(this);
stop.addActionListener(this);
qiupai.addKeyListener(this);
//添加键盘监视器(为了防止焦点丢失造成的问题,将所有组件都添加键盘监视器)
p.addKeyListener(this);
start.addKeyListener(this);
stop.addKeyListener(this);
this.addKeyListener(this);
//加载组件
p.add(ball);
p.add(qiupai);
p.add(over);
p.add(win);
p.add(bg);
this.add(start);
this.add(stop);
this.add(p);
this.setResizable(false);
this.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
this.setVisible(true);
}
/**
*主程序入口
*@paramargs
*/
publicstaticvoidmain(String[]args){
newBallGame("碰撞球");
}
/**
*发生操作的监视器
*/
publicvoidactionPerformed(ActionEvente){
if(e.getSource().equals(start)){
if(isOver){
x1=qiupai.getX()+30;y1=qiupai.getY()-15;//当游戏结束时按开始按钮,即二次开始游戏时将球的位置至于球拍上
isOver=false;
over.setVisible(false);
win.setVisible(false);
//遍历remove列表,将上次游戏移除的砖块再次绘制到面板(添加至bricklist)
for(inti=0;iremoveBrickList.get(i).setVisible(true);
brickList.add(removeBrickList.get(i));
}
}
isYi=true;
time.start();
}elseif(e.getSource().equals(stop)){
if(!
isOver){
time.stop();//只有当游戏没有结束时,才能暂停
}
}else{
//遍历砖块list
for(inti=0;i//如果小球与砖块发生重叠,将砖块从list里移除
if(ball.getX()>=brickList.get(i).getX()&&ball.getX()<=brickList.get(i).getX()+20&&ball.getY()<=brickList.get(i).getY()+20){
//将需要移除的砖块保存到被移除的砖块的removeBrickList
removeBrickList.add(brickList.get(i));
brickList.get(i).setVisible(false);
brickList.remove(i);
isDow=true;//与砖块碰撞后改变y轴方向
}
}
//如果list空,则说明游戏胜利
if(brickList.size()==0){
win.setVisible(true);
time.stop();
isOver=true;
}
if(y1<340){//如果球的y轴超过该数值,则表明球已经掉到球拍下面,即游戏结束
if(isDow){
if(isRight){
x1+=xsudu;
y1+=ysudu;
}else{
x1-=xsudu;
y1+=ysudu;
}
}else{
if(isRight){
y1-=ysudu;
x1+=xsudu;
}else{
x1-=xsudu;
y1-=ysudu;//绘制球的运动轨迹
}
}
if(x1>210)//超出范围改变左右方向
isRight=false;
//如果小球与球拍发生重叠,小球反弹。
球的x,y速度也跟着改变。
速度由随机数决定
if(y1>320&&qiupai.getX()ball.getX()){
while(true){
if((xsudu=ran.nextInt(3))!
=0)//死循环的作用是为了防止
出现速度为0的情况
break;
}
while(true){
if((ysudu=ran.nextInt(3))!
=0)
break;
}
isDow=false;//改变上下方向
}
if(x1<=10)
isRight=true;//超出范围改变左右方向
if(y1<=10)
isDow=true;//改变上下方向
ball.setLocation(x1,y1);//设置球的位置
}else{
//当游戏结束时,提醒用户
over.setVisible(true);
isYi=false;
isOver=true;
time.stop();
}
}
}
/**
*键盘事件监视器
*/
publicvoidkeyPressed(KeyEvente){
if(isYi){
if(e.getKeyCode()==KeyEvent.VK_LEFT){
if(x2>10)
x2-=10;
}elseif(e.getKeyCode()==KeyEvent.VK_RIGHT){//按下lift,
right时左右移动动球拍,偏移量10
if(x2<160)
x2+=10;
}
qiupai.setLocation(x2,y2);//设置球拍最新位置
}
}
publicvoidkeyReleased(KeyEvente){}
publicvoidkeyTyped(KeyEvente){}}
八、参考文献
1.《疯狂java讲义》
2.《算法导论》
3.《java编程思想》