欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现Flappy Bird游戏源代码

程序员文章站 2024-03-02 21:11:40
本文实例为大家分享了java实现flappy bird游戏的具体代码,供大家参考,具体内容如下 /* 2017/7/23 */ import java...

本文实例为大家分享了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实现Flappy Bird游戏源代码

java实现Flappy Bird游戏源代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。