java實現(xiàn)flappy Bird小游戲
本文實例為大家分享了java實現(xiàn)flappy Bird游戲的具體代碼,供大家參考,具體內(nèi)容如下
整個游戲由3個類構(gòu)成。Bird類,Pipe類,stage類
第一步:首先寫一個Bird類
//鳥類
public class Bird {
private int flyHeight;//飛行高度
private int xpos;//距離y軸(窗口左邊緣)的位置,
public static int Up=1;//向上飛
public static int Down=-1;//向下飛
public Bird()
{
flyHeight=200;
xpos=30;
}
public void fly(int direction)
{
if(direction==Bird.Up)
flyHeight-=20;//每次向上飛20m
if(direction==Bird.Down)
flyHeight+=20;//每次向下飛20m
}
public int getFlyHeight()//獲得當(dāng)前飛行高度
{
return flyHeight;
}
public int getXpos()//獲得當(dāng)前鳥的水平位置
{
return xpos;
}
public Boolean hit(Pipe pipe[])//檢測是否碰到管道。只有在鳥經(jīng)過管道的過程才有可能相撞
{
for(int i=0;i<pipe.length;i++)//遍歷管道進行檢測,是否相撞
{
if(getXpos()+20>=pipe[i].getXpos()&&getXpos()<=pipe[i].getXpos()+20)//鳥經(jīng)過管道
if(flyHeight<pipe[i].getUpHeight()||flyHeight>pipe[i].getDownHeight())//鳥與管道相撞
return true;
}
return false;
}
}
第二步:寫一個Pipe類,Pipe類 里有3個成員,upHeight表示頂管道端的高度,downHeight表示底端管道段的高度,同樣要記錄管道的水平位置。
public class Pipe {
private int upHeight;//表示頂管道端的高度
private int downHeight;//表示底端管道段的高度
private int xpos;
public Pipe()
{
upHeight=0;
downHeight=0;
xpos=0;
}
public Pipe(int maxHeight,int xpos)//給管道一個最大總長度(maxHeight)=upHeight+downHeight。還有管道的水平位置
{
double num;
num=Math.random();
while(num==0)
num=Math.random();
upHeight=(int) (maxHeight*num);//隨機產(chǎn)生一個頂端管道段高度(<maxHeight)
downHeight=maxHeight-upHeight;//用總長度減去upHeight
this.xpos=xpos;
}
public void setXpos(int xpos)
{
this.xpos=xpos;
}
public int getXpos()
{
return xpos;
}
public int getUpHeight()
{
return upHeight;
}
public int getDownHeight()
{
return downHeight;
}
public String toSting()
{
return "("+upHeight+","+downHeight+")";
}
}
最后一步,寫好舞臺類,即操作游戲的類
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.Array;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class stage extends JPanel{
private Pipe pipe[];//管道數(shù)組
private Bird bird;//鳥
private int space;//上下管道之間的間隔
public JLabel scoreBoard;//計分面板
private int score;//計分
public stage()
{
space=150;//<span style="font-family: Arial, Helvetica, sans-serif;">上下管道之間的間隔為150</span>
score=0;
scoreBoard=new JLabel("得分:"+score);
pipe=new Pipe[5];//總共5跟根
for(int i=0;i<pipe.length;i++)
{
pipe[i]=new Pipe(400-space,i*130+110);//柱子每隔110m放一根
//System.out.println(pipe[i].toSting());
}
bird=new Bird();
}
public void play()
{
Timer timer=new Timer();//定時任務(wù),即使不操作也能動
timer.schedule(new TimerTask()
{
public void run()
{
if(bird.hit(pipe))//碰到,重置所有數(shù)據(jù)成員
{
//System.out.println("碰到了");
score=0;
scoreBoard.setText("得分:"+score);
pipe=new Pipe[10];
for(int x=0;x<pipe.length;x++)
pipe[x]=new Pipe(400-space,x*130+110);
bird=new Bird();
}
else{//沒碰到
//System.out.println("沒碰到");
bird.fly(Bird.Down);//鳥默認向下飛
for(int x=0;x<pipe.length;x++)//管道每次往前移動10m,造成鳥向右移動的效果
{
pipe[x].setXpos(pipe[x].getXpos()-10);
}
score=score+10;
scoreBoard.setText("得分:"+score);
}
repaint();
}
}, 0, 1000);//在不操作的情況下,每一秒飛一次
this.requestFocus();//獲取”輸入“焦點
this.addKeyListener(new KeyAdapter() {//加入鍵盤時間
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==38)
<span style="white-space:pre"> </span>action(Bird.Up);
else if(e.getKeyCode()==40)
action(Bird.Down);
});
}
public void action(int direction)//按下上下方向鍵后執(zhí)行的函數(shù)
{
if(bird.hit(pipe))
{
//System.out.println("碰到了");
score=0;
scoreBoard.setText("得分:"+score);
pipe=new Pipe[10];
for(int x=0;x<pipe.length;x++)
pipe[x]=new Pipe(400-space,x*130+110);
bird=new Bird();
}
else{
//System.out.println("沒碰到");
if(direction==Bird.Up)
{
bird.fly(Bird.Up);
}
else if(direction==Bird.Down)
{
bird.fly(Bird.Down);
}
for(int x=0;x<pipe.length;x++)//管道每次往前移動10m,造成鳥向右移動的效果
{
pipe[x].setXpos(pipe[x].getXpos()-10);
}
score=score+10;
scoreBoard.setText("得分:"+score);
}
repaint();
}
public void paint(Graphics g)
{
g.setColor(g.getColor());
g.fillRect(0, 0, getWidth(), getHeight());//用默認顏色清屏
g.setColor(Color.red);
g.fill3DRect(bird.getXpos(), bird.getFlyHeight(), 20, 20, true);//紅色畫鳥
g.setColor(Color.green);
for(int i=0;i<pipe.length;i++)//綠色畫管道
{
g.fill3DRect(pipe[i].getXpos(), 0, 20, pipe[i].getUpHeight(), true);
g.fill3DRect(pipe[i].getXpos(),pipe[i].getUpHeight()+space, 20, pipe[i].getDownHeight(), true);
}
if(pipe[0].getXpos()+20<=0)//如果第一根管道出界,就刪掉第一根管道,把后面的往前挪,再新創(chuàng)建一根管道
{
for(int i=1;i<pipe.length;i++)
pipe[i-1]=pipe[i];
pipe[pipe.length-1]=new Pipe(400-space,(pipe.length-1)*130+110);
//System.out.println(pipe[pipe.length-1].toSting());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf=new JFrame("flappyBird");
stage app=new stage();
jf.setLayout(null);
app.setBounds(0,20,600,400);
app.scoreBoard.setBounds(0, 0, 100, 20);
jf.add(app);
jf.add(app.scoreBoard);
jf.setSize(610, 460);
jf.setVisible(true);
app.play();//游戲開始
}
}
程序截圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳
這篇文章主要介紹了Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Feign遠程調(diào)用傳遞對象參數(shù)并返回自定義分頁數(shù)據(jù)的過程解析
這篇文章主要介紹了Feign遠程調(diào)用傳遞對象參數(shù)并返回自定義分頁數(shù)據(jù)的過程解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
聊聊springboot2.2.3升級到2.4.0單元測試的區(qū)別
這篇文章主要介紹了springboot 2.2.3 升級到 2.4.0單元測試的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

