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)
{}
}
所以在我的编程课上,我们正在尝试编写 Flappy Bird 或它的一个版本。就我而言,我正在使用哈利波特主题。当我按下键盘上的向上箭头直到速度为 0 时,我的飞贼应该会减速,这将导致它停止移动。一旦速度达到 0,飞贼就会在下降时加速,直到达到预先声明的速度 10。有人可以向我解释如何加速和减速吗?