我在java中制作FlappyBirds,现在一切都很好用。但我遇到了一个问题。
我有一个名为' status
'的整数变量。当status
为0
时,游戏会运行,如果status
为1
,则窗口会显示" game over, press s to begin
"。我的KeyListener
适用于游戏。
但是在我输掉游戏后status
变为1
,当我再次按下s
按钮时,我必须按住按钮一段时间。它没有迅速响应。在开始期间,status
为1
。当我在此期间按s
时,它很快就会出现,但当我丢失并且状态再次变为1
时,就会出现问题。
package flappybirds;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FlappyBirds extends JPanel implements ActionListener,KeyListener {
public int WIDTH=800,HEIGHT=800,x=WIDTH/2-10,y=HEIGHT/2-10 , gravity=0, ticks=0,status=1;
public static FlappyBirds flappy;
public boolean jump;
public boolean gameOver=true ;
public ArrayList<Rectangle>walls;
public Rectangle bird;
public Random generator;
public FlappyBirds(){
Timer t=new Timer(10,this);
JFrame window=new JFrame("Flappy Birds");
window.setSize(WIDTH ,HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(this);
window.setVisible(true);
window.addKeyListener(this);
walls=new ArrayList<Rectangle>();
generator=new Random();
t.start();
}
public static void main(String[] args) {
flappy=new FlappyBirds();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
bird=new Rectangle(x,y,15,15);
g.setColor(new Color(0,200,255));
g.fillRect(0,0,WIDTH,HEIGHT);
g.setColor(Color.ORANGE);
g.fillRect(0, HEIGHT-100, WIDTH,100);
g.setColor(Color.GREEN);
g.fillRect(0,HEIGHT-110,WIDTH,10);
g.setColor(Color.red);
g.fillRect(bird.x,bird.y,bird.width,bird.height);
g.drawString(Integer.toString(status),200,200);
g.setColor(new Color(200,200,40));
if(status==0){
for(Rectangle rect:walls){
paintWall(rect,g);
}
}
if(status==1){
g.setFont(new Font("Arial",50,50));
g.setColor(Color.RED);
g.drawString("Press 'S' to begin",400,400);
}
g.setColor(Color.RED);
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
if(status==0){
addWall(true);
addWall(true);
ticks ++;
if(jump){
if(ticks%8==0){
gravity=-10;
}
}
else if(ticks%8==0 && gravity<15 && jump==false){
gravity+=2;
}
if(ticks==1000){
ticks=0;
}
y += gravity;
for(Rectangle rect:walls){
rect.x -=10;
}
for(int i=0;i<walls.size();i++){
Rectangle r=walls.get(i);
if(r.x+r.width<0){
addWall(false);
}
}
for(Rectangle column:walls){
if(column.intersects(bird) || bird.y>=HEIGHT-120 || bird.y<=3 ){
gameOver=true;
status=1;
if(!jump){
if(y>=HEIGHT-120){
y=HEIGHT-120;
}
if(column.intersects(bird)){
y=HEIGHT/2-10;
}
if(bird.y<=3){
y=HEIGHT/2-10;
}
}else if(jump){
y=HEIGHT/2-10;
}
}else{
gameOver=false;
}
}
collide();
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
int c=e.getKeyCode();
if(status==0){
if(c==KeyEvent.VK_SPACE){
jump=true;
}
}
if(status==1){
if(c==KeyEvent.VK_S){
status=0;
}
}
}
public void collide(){
if(status==0){
if(gameOver){
walls.clear();
addWall(true);
addWall(true);
gameOver=false;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
jump=false;
}
public void paintWall(Rectangle rect,Graphics g){
g.fillRect(rect.x, rect.y, rect.width, rect.height);
}
public void paintSpace(Rectangle rect,Graphics g){
g.setColor(new Color(0,200,225));
g.fillRect(rect.x,rect.y,rect.width,rect.height);
}
public void addWall(boolean oldwall){
int width=100;
final int height=350;
int yloc=100+generator.nextInt(150);
int space=100;
if(oldwall){
if( generator.nextBoolean()){
walls.add(new Rectangle(WIDTH+width+ (walls.size()*200),HEIGHT- height-yloc,width,height));
walls.add(new Rectangle(WIDTH+width+(walls.size()-1)*200,- yloc, width,height- space));
}else{
walls.add(new Rectangle(WIDTH+width+ (walls.size()*200),HEIGHT-height+yloc,width,height));
walls.add(new Rectangle(WIDTH+width+ (walls.size()-1)*200,+yloc, width,height- space));
}
}else{
if( generator.nextBoolean()){
walls.add(new Rectangle(walls.get(walls.size()-1).x+600,HEIGHT-height- yloc,width,height+space));
walls.add(new Rectangle(walls.get(walls.size()-2).x+600,- yloc,width,height-space));
}else{
walls.add(new Rectangle(walls.get(walls.size()-1).x+600,HEIGHT- height+yloc,width,height+space));
walls.add(new Rectangle(walls.get(walls.size()-2).x+600,yloc,width,height-space));
}
}
}
}
答案 0 :(得分:0)
延迟的原因当然是计时器:
计时器持续运行,每10ms启动一次事件(即调用actionPerformed
)。 即使在游戏结束后它正在运行,因此ActionEvent事件会在事件队列中累积,而您按下的'S'会受到很大的延迟,直到轮到它被处理为止。
我建议你:
init
的私有方法,并在那里初始化x
,y
,gravity
,ticks
,walls
和当然,呼叫timer.start()
。init
之后,在构造函数末尾和keyPressed
内调用status=0
。