这是flappy bird的代码。所有这些都在一个类中,我不知道这是不是一个问题,我该如何解决它?鼠标侦听器的实现可能是错误的。
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Flappy extends JFrame implements MouseListener {
public static Image loadImage (String name) //Loads image from file
{
BufferedImage img = null;
try
{
img = ImageIO.read (new File (name));
}
catch (IOException e)
{
}
return img;
}
/**
*
*/
private static final long serialVersionUID = 1L;
Image back,ground, bird, upper, lower;
int y=200,vy=1;//speed and acceleration respectively
int xPipe1=600,yPipe1=(int)(Math.random()*300)+250;//random numbers for pipes
int xPipe2=900,yPipe2=(int)(Math.random()*300)+250;
int xPipe3=1200,yPipe3=(int)(Math.random()*300)+250;
public Flappy ()
{
this.setTitle("Flappy Bird");//the jframe
this.setSize(400,700);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addMouseListener(this);
//Here is the problem, adding the mouse listener to the jframe
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
new Flappy();
}
public void paint (Graphics g) {
//loop that draws the images
Graphics2D g2d = (Graphics2D) g;
ImageIcon i=new ImageIcon("image/background.jpg");
back=i.getImage();
ImageIcon j=new ImageIcon("image/ground.png");
ground=j.getImage();
ImageIcon k=new ImageIcon("image/wingup.png");
bird=k.getImage();
ImageIcon l=new ImageIcon("image/pipe-up.png");
lower=l.getImage();
ImageIcon m=new ImageIcon("image/pipe-down.png");
upper=m.getImage();
for (int backPos=0;backPos>-1200;backPos--)
{
try{
Thread.sleep(30);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
g2d.drawImage(back, backPos, 0, null);
g2d.drawImage(lower,xPipe1,yPipe1,null);
g2d.drawImage(upper,xPipe1,yPipe1-600,null);
xPipe1-=5;
g2d.drawImage(lower,xPipe2,yPipe2,null);
g2d.drawImage(upper,xPipe2,yPipe2-600,null);
xPipe2-=5;
g2d.drawImage(lower,xPipe3,yPipe3,null);
g2d.drawImage(upper,xPipe3,yPipe3-600,null);
xPipe3-=5;
if(xPipe1==-80)
{
xPipe1=800;
yPipe1=(int)(Math.random()*300)+250;
}
if(xPipe2==-80)
{
xPipe2=800;
yPipe2=(int)(Math.random()*300)+250;
}
if(xPipe3==-80)
{
xPipe3=800;
yPipe3=(int)(Math.random()*300)+250;
}
g2d.drawImage(ground,0,129,null);
g2d.drawImage(bird,150,y,null);
if (y<590)
{
vy+=3;
y+=vy;
}
else
{
y=590;
}
if(backPos==-1100)
{
backPos=0;
}
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
vy=-100;//The bird is supposed to flap here, but there is no input
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}发布于 2015-01-08 10:57:44
尝试一下:在构造函数中定义东西时,创建一个新的mouseListener对象,而不是为整个类实现一个mouseListener。例如:
public flappy(){
//define stuff here
this.addMouseListener(new MouseListener(){
public void onClick(MouseEvent e){
//action goes here
}
//you will get an error to include other voids. Just click on "add unimplemented methods".
});
}https://stackoverflow.com/questions/27831917
复制相似问题