Question

Here is my code:

import java.applet.AudioClip;
import java.awt.Color; 
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import acm.graphics.GLabel;
import acm.program.GraphicsProgram;
import javax.imageio.ImageIO;
import acm.util.MediaTools;

public class FlappyBird extends GraphicsProgram {
    public Background background; //background image
    public UpTube uptube; //one of the pipes
    public DownTube downtube; //other pipe
    public Bird bird;
    //image for the bird
    public static final int APPLICATION_WIDTH = 882;
    public static final int APPLICATION_HEIGHT = 772;

    public void run(){
        addKeyListeners();
        background = new Background();
        add(background);
        uptube = new UpTube();
        add(uptube);
        downtube = new DownTube();
        add(downtube);
        bird = new Bird();
        add(bird);

    public void jump(){
        for(int i =0;i<5;i++){
            bird.move(3,-7);
            pause(100);
        }
        for(int i =0;i<15;i++){
            bird.move(5, -4);
            pause(100);
        }
        for(int i =0;i<15;i++){
            bird.move(7,0);
            pause(100);
        }
        for(int i =0;i<15;i++){
            bird.move(5,7);
            pause(100);
        }
        for(int i =0;i<15;i++){
            bird.move(3,-7);
            pause(100);
        }
    }

    public void keyPressed(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_SPACE){
                    jump();

However, when I run this and I press the Space Bar, it doesn't show the bird's individual movements, it just teleports the bird to the end location after the pause(100) is over for each for statement. How do I make it so that it updates the bird's location each time I move it?

Was it helpful?

Solution

I dont know about the API you are using but I have made many graphics utilities, games and programs in java and there are some basic principles you must know; the one you seem to be having an 'issue' with is you assume the rendering is done one another thread (happening while this code is running) or will completely redraw whenever the bird moves - this is not the case in most graphics renderers, instead they simply redraw after all processing of every frame.
So, what you will need to do is either get rendering on another thread so the for loop can run while the rendering is happening at a different rate, or, implement more state-machine like code where it knows what it should do each frame (e.g move the bird every 100ms, 15 times).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top