java實(shí)現(xiàn)Flappy Bird游戲源代碼
本文實(shí)例為大家分享了java實(shí)現(xiàn)Flappy Bird游戲的具體代碼,供大家參考,具體內(nèi)容如下
/*
2017/7/23
*/
import java.awt.Graphics;
//import java.util.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;
import java.awt.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.*;
import javax.swing.JPanel;
class Renderer extends JPanel
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
FlappyBird.flappyBird.repaint(g);
}
}
public class FlappyBird implements ActionListener, MouseListener, KeyListener
{
public static FlappyBird flappyBird;
public final int WIDTH = 900, HEIGHT = 800;
public Renderer renderer;
public Rectangle bird;
public ArrayList<Rectangle> columns;
public int ticks, yMotion, score;
public boolean gameOver, started;
public Random rand;
public FlappyBird()
{
JFrame jframe = new JFrame();
Timer timer = new Timer(20,this);
renderer = new Renderer();
rand = new Random();
jframe.add(renderer);
jframe.setTitle("Flappy Bird");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(WIDTH,HEIGHT);
jframe.addMouseListener(this);
jframe.addKeyListener(this);
jframe.setResizable(false);
jframe.setVisible(true);
bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
columns = new ArrayList<Rectangle>();
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
timer.start();
}
public void addColumn(boolean start)
{
int space = 300;
int width = 100;
int height = 50 + rand.nextInt(300);
if(start)
{
columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height));
columns.add(new Rectangle(WIDTH + width + (columns.size()-1)*300, 0, width, HEIGHT - height - space));
}
else
{
columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height));
columns.add(new Rectangle(columns.get(columns.size() - 1).x , 0, width, HEIGHT - height - space));
}
}
public void paintColumn(Graphics g, Rectangle column)
{
g.setColor(Color.green.darker());
g.fillRect(column.x, column.y, column.width, column.height);
}
public void jump()
{
if (gameOver)
{
bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
columns.clear();
yMotion = 0;
score = 0;
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
gameOver = false;
}
if(!started)
{
started = true;
}
else if(!gameOver)
{
if(yMotion > 0)
{
yMotion = 0;
}
yMotion -= 10;
}
}
public void actionPerformed(ActionEvent e)
{
int speed = 10;
ticks++;
if(started )
{
for( int i = 0; i < columns.size(); i++)
{
Rectangle column = columns.get(i);
column.x -= speed;
}
if(ticks % 2 ==0 && yMotion < 15)
{
yMotion += 2;
}
for (int i = 0; i < columns.size(); i++)
{
Rectangle column = columns.get(i);
if (column.x + column.width < 0)
{
columns.remove(column);
if(column.y ==0)
{
addColumn(false);
}
}
}
bird.y += yMotion;
for(Rectangle column : columns)
{
if(bird.x + bird.width / 2 > column.x + column.width / 2 - 5
&& bird.x + bird.width / 2 < column.x + column.width / 2 + 5
&& column.y == 0)
{
score++;
}
if(column.intersects(bird))
{
gameOver = true;
if(bird.x <= column.x)
{
bird.x = column.x - bird.width;
}
else
{
if(column.y != 0)
{
bird.y = column.y - bird.height;
}
else if(bird.y < column.height)
{
bird.y = column.height;
}
}
}
}
if(bird.y > HEIGHT - 120 || bird.y < 0 )
{
gameOver = true;
}
if(bird.y + yMotion >= HEIGHT -120)//(gameOver)
{
bird.y = HEIGHT -120 - bird.height;
}
}
renderer.repaint();
}
public void repaint(Graphics g)
{
//System.out.println("hello");
g.setColor(Color.cyan);
g.fillRect(0,0,WIDTH,HEIGHT);
g.setColor(Color.orange);
g.fillRect(0, HEIGHT - 120, WIDTH, 150);
g.setColor(Color.green);
g.fillRect(0, HEIGHT - 120, WIDTH, 20);
g.setColor(Color.red);
g.fillRect(bird.x, bird.y, bird.width, bird.height);
for ( Rectangle column : columns )
{
paintColumn(g,column);
}
g.setColor(Color.white);
g.setFont(new Font("Arial",1,70));
if(!started)
{
g.drawString("Click to start!",90,HEIGHT / 2-50);
}
if(gameOver)
{
g.drawString("Game Over! You suck!",40,HEIGHT / 2-50);
}
if(!gameOver && started)
{
g.drawString(String.valueOf(score), WIDTH / 2, 100);
}
}
public static void main(String[]args)
{
flappyBird = new FlappyBird();
}
public void mouseClicked(MouseEvent e)
{
jump();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void keyPressed(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
jump();
}
}
}
效果圖:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言實(shí)現(xiàn)flappy bird游戲
- Qt實(shí)現(xiàn)Flappy Bird游戲
- python實(shí)現(xiàn)flappy bird游戲
- Java實(shí)現(xiàn)Flappy Bird游戲源碼
- java實(shí)現(xiàn)flappy Bird小游戲
- C語(yǔ)言實(shí)現(xiàn)Flappy Bird小游戲
- C語(yǔ)言簡(jiǎn)易版flappy bird小游戲
- 純JavaScript 實(shí)現(xiàn)flappy bird小游戲?qū)嵗a
- C++版本簡(jiǎn)易Flappy bird
- Unity實(shí)現(xiàn)Flappy Bird游戲開發(fā)實(shí)戰(zhàn)
相關(guān)文章
spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法
最近寫了關(guān)于圖片上傳至本地文件夾或服務(wù)器,上傳路徑到數(shù)據(jù)庫(kù),并在上傳時(shí)預(yù)覽圖片。本文通過實(shí)例代碼給大家分享spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法,需要的朋友參考下2018-12-12
Java一維數(shù)組和二維數(shù)組元素默認(rèn)初始化值的判斷方式
這篇文章主要介紹了Java一維數(shù)組和二維數(shù)組元素默認(rèn)初始化值的判斷方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇上
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國(guó)內(nèi)又以Mybatis用的多,基于mybatis上的增強(qiáng)框架,又有mybatis plus和TK mybatis等。今天我們介紹一個(gè)新的mybatis增強(qiáng)框架 fluent mybatis2021-10-10
java自定義任務(wù)類定時(shí)執(zhí)行任務(wù)示例 callable和future接口使用方法
Callable是類似于Runnable的接口,實(shí)現(xiàn)Callable接口的類和實(shí)現(xiàn)Runnable的類都是可被其它線程執(zhí)行的任務(wù)2014-01-01
Java中實(shí)體類為什么要實(shí)現(xiàn)Serializable序列化的作用
這篇文章主要介紹了Java中實(shí)體類為什么要實(shí)現(xiàn)Serializable序列化的作用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

