資源簡介
用戶打開此游戲后,窗口會有一個球移動,每次落到下面的時候,下面的板子必須接住。當接住一次后,球的速度越來越快。難度越來越大。如果接不住球,則游戲結(jié)束。

代碼片段和文件信息
import?java.awt.Color;
import?java.awt.Graphics;
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseMotionListener;
import?java.util.Timer;
import?java.util.TimerTask;
import?java.util.zip.Inflater;
import?javax.swing.Jframe;
import?javax.swing.JOptionPane;
import?javax.swing.JPanel;
/**
?*?
?*?@author?xieyuan
?*?1.窗口??面板??窗口位置固定
?*?2.在窗口上畫一個球
?*?3.讓小球自己動
?*?4.對小球的運動方向進行判斷,并進行方向的處理,實現(xiàn)小球的反彈效果
?*?5.添加擋板??實現(xiàn)跟隨鼠標來控制擋板移動
?*?6.判斷小球接觸到擋板,或者接觸到邊緣的時候的處理
?*/
public?class?Game?extends?Jframe?implements?MouseMotionListener{
//設(shè)置窗口的寬、高
private?int?fw=800;
private?int?fh=600;
//設(shè)置球的位置
private?int?bx=0;
private?int?by=0;
private?int?b2r=100;
private?String?direction=“right_down“;//初始方向是右下
//定時器:在指定時間間隔內(nèi),反復(fù)觸發(fā)指定窗口的事件
private?Timer?timer=new?Timer();
//設(shè)置擋板??back?plate
private?int?block_x=200;
private?int?block_y=580;
private?int?block_w=150;
private?int?block_h=20;
//設(shè)置球的速度、分數(shù)
private?int?speed=1;
private?int?score=0;
private?Inner?inner=null;
public?Game()?{
//窗口置頂,永遠在前
this.setAlwaysOnTop(true);
//禁用此frame邊框修飾
this.setUndecorated(true);
//初始化容器
this.getContentPane().setBackground(Color.BLACK);
this.setSize(fw?fh);
//添加面板
inner=new?Inner();
this.add(inner);
//窗口位置:居中
this.setLocationRelativeTo(null);
//設(shè)置窗口能否改變大小
this.setResizable(false);
this.addMouseMotionListener(this);
this.setVisible(true);
}
//面板容器類
class?Inner?extends?JPanel?{
public?Inner()?{
//控件:設(shè)置不透明
this.setOpaque(false);
//定時器的方法
timer.schedule(new?TimerTask(){
@Override
public?void?run()?{
/*
?*?通過坐標定時改變實現(xiàn)小球的移動
?*/
if(“right_down“.equalsIgnoreCase(direction))?{//右下
bx+=speed;
by+=speed;
}
if(“right_up“.equalsIgnoreCase(direction))?{//右上
bx+=speed;
by-=speed;
}
if(“l(fā)eft_up“.equalsIgnoreCase(direction))?{//左上
bx-=speed;
by-=speed;
}
if(“l(fā)eft_down“.equalsIgnoreCase(direction))?{//左下
bx-=speed;
by+=speed;
}
/*
?*?判斷小球什么時候進行方向的改變
?*/
//游戲結(jié)束
if(by+b2r>=fh)?{
JOptionPane.showMessageDialog(inner?“GAME?OVER!!!“?“提示信息“?JOptionPane.DEFAULT_OPTION);
Runtime.getRuntime().exit(0);//運行結(jié)束
}
//接觸到頂部
if(by<=0)?{
if(“l(fā)eft_up“.equalsIgnoreCase(direction))?{//左上
direction=“l(fā)eft_down“;
}else{//右上
direction=“right_down“;
}
}
//接觸到左邊
if(bx<=0)?{
if(“l(fā)eft_down“.equalsIgnoreCase(direction))?{//左下
direction=“right_down“;
}else{//左上
direction=“right_up“;
}
}
//接觸到右邊
if(bx+b2r>=fw)?{
if(“right_up“.equalsIgnoreCase(direction))?{//右上
direction=“l(fā)eft_up“;
}else{//右下
direction=“l(fā)eft_down“;
}
}
//接觸到底部、擋板
if(bx+b2r/2>=block_x&&bx+b2r/2<=block_x+block_w&&by+b2r>=block_y)?{
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-07-31?16:29??彈球游戲\
?????文件?????????301??2018-07-31?15:39??彈球游戲\.classpath
?????文件?????????388??2018-07-31?15:39??彈球游戲\.project
?????目錄???????????0??2018-07-31?19:10??彈球游戲\.settings\
?????文件?????????598??2018-07-31?15:39??彈球游戲\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2018-11-18?07:04??彈球游戲\bin\
?????文件????????2379??2018-11-18?07:04??彈球游戲\bin\Game$Inner$1.class
?????文件????????1563??2018-11-18?07:04??彈球游戲\bin\Game$Inner.class
?????文件????????3469??2018-11-18?07:04??彈球游戲\bin\Game.class
?????文件????????4137??2018-11-18?07:04??彈球游戲\bin\JFraStart.class
?????文件?????????406??2018-11-18?07:04??彈球游戲\bin\StartAction.class
?????目錄???????????0??2018-07-31?19:12??彈球游戲\src\
?????文件????????4211??2018-07-31?16:26??彈球游戲\src\Game.java
?????文件????????3465??2018-07-31?19:55??彈球游戲\src\StartAction.java
- 上一篇:swing gif 所需jar包
- 下一篇:FiddlerCertMaker.exe
評論
共有 條評論