我正在为学校做项目,我选择重新创建Duck Hunt。它到目前为止相当顺利,除了一个问题,飞出的鸭子移动速度太快,我设置如下,x和y显然是鸭子的x和y坐标&# 39;飞行,我有一个循环,做y--。但就像我说的那样,速度太快了,我不能在图形类中使用双精度数,只能使用int。这是我写的代码。
(第一篇文章,我认为我正确发布了代码,抱歉,如果我没有这样做)
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.TextField;
import java.awt.MouseInfo;
public class duck extends Applet implements ActionListener, MouseListener{
boolean start= true;
int x;
int y=400;
int bx=350;
int by= 285;
int counter=1;
int c=2;// (int) (Math.random()*4)+1;
boolean dog=true;
boolean dog3=false;
boolean bird=false;
public void init(){
}
public void paint(Graphics g){
//BackGround
this.resize (612,574);
g.drawImage(getImage(getCodeBase(), "background.png"), 0, 0, this);
g.drawString("" +c, 10, 300);
//Crosshair Follow
g.drawOval (MouseInfo.getPointerInfo().getLocation().x-24, MouseInfo.getPointerInfo().getLocation().y-63, 50, 50);
g.drawLine (MouseInfo.getPointerInfo().getLocation().x-24, MouseInfo.getPointerInfo().getLocation().y-37, MouseInfo.getPointerInfo().getLocation().x+24, MouseInfo.getPointerInfo().getLocation().y-37);
g.drawLine (MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y-64, MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y-15);
g.drawString ("" +MouseInfo.getPointerInfo().getLocation().x +" " +MouseInfo.getPointerInfo().getLocation().y, 100, 100);
//this.repaint();
//Dog
if (dog==true)
g.drawImage(getImage(getCodeBase(), "dog.gif"), x, y, this);
x++;
this.repaint();
if (x>200){
dog=false;
dog3=true;
}
if (y<285){
dog3=false;
//this.repaint();
}
if (dog3==true){
y=y-1;
bird=true;
}
//Bird
if (bird==true ){
//Start Path 1 (Right Then Left at y 100)
if (c==1){
g.drawImage(getImage(getCodeBase(), "birdleft.gif"), bx, by, 75, 75, this);
bx++;
by--;
counter++;
if (by>200){
bx=bx-3;
by--;
}
}
//End Path 1
//Start Path 2
if (c==2){
g.drawImage(getImage(getCodeBase(), "birdleft.gif"), bx-100, by, 75, 75, this);
bx--;
by--;
counter++;
if (by<200){
bx++;
by--;
}
}
//End Path 2
} //End Bird True
} //End Graphics
public void mouseClicked(MouseEvent me) {
//this.repaint();
}
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}public void mouseExited(MouseEvent me) {}
public void actionPerformed(ActionEvent e){
}
}
答案 0 :(得分:1)
不要通过在绘画方法中调用repaint()
来制作动画,因为你无法控制动画。而是使用Swing Timer,它甚至可以在AWT程序中工作,或使用你自己的后台线程Timer,你可以设置粗略的帧速率,从而减慢你的动画。
修改强>
您的代码的其他问题:
if (something == true)
或if (someOtherThing == false)
,因为这很容易出错。而只需使用if (something)
和if (!someOtherThing)
。