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?