我有一个矩形来代表我的鸟,我使用了我的鸟和管道的教程,但我知道 Rectangle 类有 x、y、宽度和高度作为其参数。我不太确定如何使用图像复制它。我的主要问题是找到一种用图像替换鸟“矩形”而不影响代码的方法。 所有与鸟相关的代码都是矩形:
public class FlappyBird implements ActionListener,MouseListener, KeyListener {
public static FlappyBird flappybird;
public static final int WIDTH = 800, HEIGHT = 800;
//object to call methods from Renderer
public static FlappyBirdUI1 render;
public static Rectangle bird;
public static int in, bmotion,scores;
public boolean gameOver;
public boolean started;
public ArrayList<Rectangle> pipes;
public static GameOverTest Gover= new GameOverTest();
JFrame jframe = new JFrame();
public FlappyBird() {
JPanel jpanel= new JPanel();
Timer timer = new Timer(20, this);// used to ensure it repaints throught a schedule timer
render = new FlappyBirdUI1();
jframe.add(render);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
//jframe.setSize(WIDTH, HEIGHT);
jframe.setSize(new java.awt.Dimension(WIDTH, HEIGHT));
jframe.setLocationRelativeTo(null);
jframe.addMouseListener(this);
jframe.setVisible(true);
//using a rectangle as the "bird"
//REPLACE WITH IMAGE OF BIRD
bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
pipes = new ArrayList<Rectangle>();
addpipe(true);
addpipe(true);
addpipe(true);
addpipe(true);
// gsettings.setBounds(0, HEIGHT, 30, 25);
timer.start();
}
public void jump(){
if (gameOver){
bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
pipes.clear();
bmotion=0;
scores=0;
addpipe(true);
addpipe(true);
addpipe(true);
addpipe(true);
gameOver=false;
}
if(!started){
started=true;
}else if(!gameOver){
if(bmotion>0){
bmotion=0;
}
bmotion-=10;
}
}
`
这是我的代码的其余部分,用于了解它的使用频率并显示对 x、y、宽度和高度的需求。
@Override
public void actionPerformed(ActionEvent e) {
in++;
//speed of repaint / moving forwrd
int speed = 10;
if (started == true) {
for (int i = 0; i < pipes.size(); i++) {
Rectangle pipe = pipes.get(i);
pipe.x -= speed;
}
if (in % 2 == 0 && bmotion < 15) {
bmotion += 2;
}
for (int i = 0; i < pipes.size() - 1; i++) {
Rectangle pipe = pipes.get(i);
//x+width is the amnt of the screen he pipe takes
if (pipe.x + pipe.width < 0) {
pipes.remove(pipe);
if (pipe.y == 0) {
addpipe(false);
}
}
}
bird.y += bmotion;
//colon is like a while there are more objects in the Rectangle pipe arraylist etc
for (Rectangle pipe : pipes) {
if(pipe.y==0&& bird.x+ bird.width /2 > pipe.x+pipe.width /2-10 && bird.x+bird.width/2 < pipe.x+pipe.width/2+10 )
{
scores++;
}
if (pipe.intersects(bird)) {
gameOver = true;
bird.x= pipe.x-bird.width;
Gover.show();
started=false;
}
}
//if you go out of the screen
//120 is the white part
if (bird.y > HEIGHT - 120 || bird.y < 0) {
gameOver = true;
}
if(bird.y+bmotion> HEIGHT-120){
bird.y=HEIGHT-120-bird.height;
}
}
render.repaint();
//
}
void repaint(Graphics g) {
g.setColor(Color.cyan);
g.fillRect(0, 0, WIDTH, HEIGHT);
//replace with clouds
g.setColor(Color.white);
g.fillRect(0, HEIGHT - 200, WIDTH, 200);
//the specific coordinates of the bird must be filled
g.setColor(Color.pink);
g.fillRect(bird.x, bird.y, bird.width, bird.height);
g.setColor(Color.white);
g.setFont(new Font("Arial",12,100));
if(!started && !gameOver){
g.drawString("Click to start", 75, HEIGHT/2-80);
}
if(gameOver){
g.drawString("Game Over!", 75, HEIGHT/2-80);
}
for (Rectangle pipe : pipes) {
paintPipe(g, pipe);
}
if(!gameOver&& started){
//String score= ""+scores;
g.drawString(String.valueOf(scores), WIDTH/2 -25, 100);
}
}