1
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Zhang extends JFrame implements Runnable, KeyListener
{

    Container con = getContentPane();
    Thread t = new Thread(this);
    int Hogwartsx = 10, Hogwartsy= 10, Snitchx = 200, Snitchy = 200, Loopx = 50, Loopy =       300, Loop2x = 120, Loop2y = 10,
    Loopx2 = 190, Loopy2 = 300, Loop2x2 = 260, Loop2y2 = 10,Loopx3 = 320, 
    Loopy3 = 300, Loop2x3 = 380, Loop2y3 = 10,
    SnitchxVel = 10, SnitchyVel = 10;
    Image Loop;
    Image Loop2;
    Image Snitch;
    Image Hogwarts;
    public Zhang()
    {
        addKeyListener(this);
        Hogwarts =     Toolkit.getDefaultToolkit().getImage(getClass().getResource("Hogwarts.gif"));
        Hogwarts = Hogwarts.getScaledInstance(500, 500, 1);
        Loop2 =   Toolkit.getDefaultToolkit().getImage(getClass().getResource("Loop2.gif"));
        Loop2 = Loop2.getScaledInstance(200, 200, 1);
        Loop = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Loop.gif"));
        Loop = Loop.getScaledInstance(200, 200, 1);
        Snitch = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Snitch.gif"));
        Snitch = Snitch.getScaledInstance(150, 150, 1);
        con.setLayout(new FlowLayout());
        t.start();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void run()
    {
        try{
            while(true)
            {
                t.sleep(100);
                repaint();
                Snitchy += SnitchyVel;
                if (Snitchy > 500)
                {
                    System.exit(0);
                }
            }
        }
        catch(Exception e){}
    }

    public void update(Graphics g)
    {
        paint(g);
    } 

    public void paint(Graphics gr)
    {
        Image i=createImage(getSize().width, getSize().height);
        Graphics2D g2 = (Graphics2D)i.getGraphics();

        g2.drawImage(Hogwarts, Hogwartsx, Hogwartsy, this); 
        g2.drawImage(Loop2, Loop2x, Loop2y, this); 
        g2.drawImage(Loop,Loopx, Loopy, this);
        g2.drawImage(Loop2, Loop2x2, Loop2y2, this); 
        g2.drawImage(Loop,Loopx2, Loopy2, this);
        g2.drawImage(Loop2, Loop2x3, Loop2y3, this); 
        g2.drawImage(Loop,Loopx3, Loopy3, this);
        g2.drawImage(Snitch,Snitchx,Snitchy, this);       
        g2.dispose();
        gr.drawImage(i, 0, 0, this);
    }

    public static void main(String[] args)
    {
        Zhang frame = new Zhang();
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public void keyReleased (KeyEvent k) 
    {

    }

    public void keyPressed (KeyEvent k) 
    {
        if( k.getKeyCode() == 38)
    {
        for (SnitchyVel = 10; SnitchyVel>= 10; SnitchyVel++)
        {
            Snitchy-=SnitchyVel;
            for (SnitchyVel = 0; SnitchyVel<=10; SnitchyVel--)
            {
                Snitchy+=SnitchyVel;
            }
        }
    }
    }

    public void keyTyped (KeyEvent k) 
    {}

}

So in my Programming class, we're trying to program Flappy Bird or a version of it. In my case, I'm doing it with a Harry Potter theme. My snitch is suppose to decelerate up when I press the up arrow on my keyboard until it reaches a velocity of 0, which will cause it to stop moving. Once the velocity hits 0, the snitch is suppose to accelerate as it falls until it reaches the pre-declared velocity of 10. Can someone explain to me how to accelerate and decelerate velocity?

Raul Guiu
  • 2,374
  • 22
  • 37
Sarah
  • 25
  • 7
  • 2
    They teach Flappy Bird cloning in programming classes?! – Blue Ice Feb 26 '14 at 23:13
  • well, I'm only in a high school programming class and our class wanted to program a game so Flappy Bird it was. – Sarah Feb 26 '14 at 23:17
  • Neat. It's interesting that you are learning something that you can relate to. – Blue Ice Feb 26 '14 at 23:18
  • haha yeah! It makes the teacher's life a lot easier to not have to explain a game to everyone. So, um, do you know the answer to my question? – Sarah Feb 26 '14 at 23:23
  • you have no indicator if you are accelerating or not. You should move the logic for the keypress into the run method. Use your keypress up or down to switch a flag from true or false. If true accelerate if false decelerate. Then in your run method check to see if you need to add or remove velocity stopping if at 0 or 10 depending on the flag. – Sorceri Feb 27 '14 at 00:15
  • @BlueIce I think that's a great project for a programming class. You can learn about rendering, interaction, object orientation, threading, game physics, ... and in the end, you may have nice game (which may be a great motivation). – Marco13 Mar 19 '14 at 10:30

1 Answers1

0

I don't know this game, only heard about it.

There are several issues with your code. The classics:

  • Don't extend JFrame
  • Implies: Never override the update or paint method of JFrame
  • Create the GUI on the Event Dispatch Thread
  • Don't let your top-level classes implement these interfaces
  • Use appropriate variable names
  • Avoid magic constants (Loop2x3 = 380, Loop2y3 = 10 etc...)

The specific ones:

  • The speed of the animation should depend on the time, and not on the frame rate
  • You should think about how you are going to model the animation. If you want to perform computations with positions, velocities and accelerations, then you should have a representation of these. Particularly, you should not try to store these in int values. For a simple 2D game, a Point2D may be a good choice.

A very, very simple approach:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Point2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class CrappyBird
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.getContentPane().setLayout(new BorderLayout());

        CrappyBirdGame crappyBirdGame = new CrappyBirdGame();
        CrappyBirdPanel crappyBirdPanel = new CrappyBirdPanel(crappyBirdGame);
        crappyBirdGame.setComponent(crappyBirdPanel);
        f.getContentPane().add(crappyBirdPanel, BorderLayout.CENTER);
        crappyBirdGame.start();

        f.setSize(800, 800);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

class CrappyBirdGame
{
    private Point2D position = new Point2D.Double(0, 500);
    private Point2D velocity = new Point2D.Double(20, 0);
    private Point2D acceleration = new Point2D.Double(0, -9.81);

    private JComponent component;

    void setComponent(JComponent component)
    {
        this.component = component;
    }

    void start()
    {
        Thread thread = new Thread(new Runnable()
        {
            @Override
            public void run() 
            {
                long oldNS = System.nanoTime();
                while (true)
                {
                    long newNS = System.nanoTime();
                    performAnimation(newNS - oldNS);
                    oldNS = newNS;
                    component.repaint();
                    try
                    {
                        Thread.sleep(20);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }

                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }


    private void performAnimation(long ns)
    {
        double delta = (ns / 1e9) * 15;

        double newVx = velocity.getX() + delta * acceleration.getX();
        double newVy = velocity.getY() + delta * acceleration.getY();
        velocity.setLocation(newVx, newVy);

        double newPx = position.getX() + delta * velocity.getX();
        double newPy = position.getY() + delta * velocity.getY();
        if (newPy < 0)
        {
            newPy = 0;
            velocity.setLocation(velocity.getX(), 0);
        }
        position.setLocation(newPx, newPy);


    }

    public Point2D getPosition()
    {
        return new Point2D.Double(position.getX(),  position.getY());
    }

    public void flap()
    {
        double newVx = velocity.getX();
        double newVy = velocity.getY() + 50;
        newVy = Math.min(30, newVy);
        velocity.setLocation(newVx, newVy);
    }

}


class CrappyBirdPanel extends JPanel
{
    private final CrappyBirdGame crappyBirdGame;

    CrappyBirdPanel(final CrappyBirdGame crappyBirdGame)
    {
        this.crappyBirdGame = crappyBirdGame;
        setFocusable(true);
        requestFocusInWindow();
        KeyListener keyListener = new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                if( e.getKeyCode() == KeyEvent.VK_UP)
                {
                    crappyBirdGame.flap();
                }
            }
        };
        addKeyListener(keyListener);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Point2D p = crappyBirdGame.getPosition();
        int x = (int)p.getX() % getWidth();
        int y = getHeight() - (int)p.getY();

        g.setColor(Color.BLUE);
        g.fillOval(x-15, y-15, 30, 30);
    }
}
Marco13
  • 53,703
  • 9
  • 80
  • 159