这篇文章主要知识点是关于java、Flappy、Bird、的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书
本文实例为大家分享了java实现Flappy Bird游戏的具体代码,供大家参考,具体内容如下
/* 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(); } } }
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农之家。
以上就是本次给大家分享的关于java的全部知识点内容总结,大家还可以在下方相关文章里找到相关文章进一步学习,感谢大家的阅读和支持。
Copyright 2018-2020 www.xz577.com 码农之家
版权投诉 / 书籍推广 / 赞助:520161757@qq.com
Java实现Flappy Bird游戏源码
本文实例为大家分享了Java实现Flappy Bird游戏的具体代码,供大家参考,具体内容如下 1.首先在mainActivity.xml中放置一个View,ID为viewDraw 2.开始编程,程序中自定义一个View类的子类,与viewDraw关联,程序除了放置如一个view控件,没有其他控件,程序上面的所有图片都是通过控制canvas画图实现 3.游戏是依据flappybird的游戏过程重新写的算法,功能与原版游戏相似,可能有些地方不足,完全是学习练习编程而已,做的不好见谅 4.原图片大小为384*512,在展示图片时将图片进行了放大,尽可能满足800*1280的全屏展示,如果你使用三星Note10,这个程序可以直接复制粘贴运行,否则,可能会遇到图片画错位置的问题,请适当调整 5.程序游戏中使用到的图片最后,只需要按照图片上的名字命名该图片,并导入到程序的图片资源中,就能顺利运行本程序 代码: public class……
java实现flappy Bird小游戏
本文实例为大家分享了java实现flappy Bird游戏的具体代码,供大家参考,具体内容如下 整个游戏由3个类构成。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()//获得当前飞行高度 { return flyHeight; } public int getXpos()//获得当前鸟的水平位置 { return xpos; } public Boolean hit(Pipe pipe[])//检测是否碰到管道。只有在鸟经过管道的过程才有可能相撞 { for(int i=0;ipipe.length;i++)//遍历管道进行检测,是否相撞 { if(getXpos()+20=pipe[i].getXpos()getXpos()=pipe[i].getXpos()+20)/……