大家好,欢迎来到IT知识分享网。
本文实例为大家分享了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 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();
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();
}
}
}
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/12773.html