为邪恶的天才编程视频游戏:项目10:激进的赛车 - 汽车

时间:2012-05-19 00:40:22

标签: java video

enter image description here

我看了一遍这本书的答案。而且我知道其他试图阅读本书的人也有同感。它被称为“为邪恶的天才编程视频游戏”有没有人读过这本书?我在项目10:Radical Racing-The Cars。一切都正确编译但由于某种原因,我的汽车没有出现在JFrame的正确位置。它们应该出现在两条白线下面。我很肯定代码和书中的代码完全相同,但这本书是错误的。我已经尝试改变原点的HEIGHT部分,但无论我做什么都不会让步。我无法附加图片,因为我没有至少10个代表。这是处理汽车位置的代码。

public class TheCars extends JFrame
{
final int WIDTH = 900; int HEIGHT = 650;

double p1Speed = .5, p2Speed = .5;


Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2, WIDTH/30,WIDTH/30);


Rectangle p2 = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),(HEIGHT/2)+    
(HEIGHT/10),WIDTH/30,WIDTH/30);
//the constructor
public TheCars()
{
    //the following code creates the JFrame
    super("Radical Racing");
    setSize(WIDTH,HEIGHT);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    //start the inner class (which works on it's own because it is a thread)
    Move1 m1 = new Move1();
    Move2 m2 = new Move2();
    m1.start();
    m2.start();

  }
  //this will draw the cars and the racetrack
  public void paint(Graphics g)
 {
     super.paint(g);

  //set the color to blue for p1
    g.setColor(Color.BLUE);
    //now draw the actual player
    g.fill3DRect(p1.x,p1.width,p1.width,p1.height,true);

    //set the color to red for p2
    g.setColor(Color.red);
    //now draw the actual player
      g.fill3DRect(p2.x,p2.width,p2.width,p2.height,true);

}
private class Move1 extends Thread
{
    public void run()
            //This should all be in an infinite loop so that the process repeats.
    {
        while(true)
        {
        //now put in the try block. This will let 
        //the program exit if there is an error
        try
        {
            //first refresh the screen
            repaint();
            //increase speed a bit
            if(p1Speed<=5)
                p1Speed+=.2;
            p1.y-=p1Speed;

            //this delays the refresh rate
            Thread.sleep(75);
        }
        catch(Exception e)
        {
            //if there is an exception (an error), exit the loop
            break;

        }

        }
    }
}

private class Move2 extends Thread
{
    public void run()
    {
    //this should all be in an infinite loop so the process repeats
    while(true)
    {
    //now put the code in a "try" block.
        //this will let the program exit if there is an error
        try
        {
            //first refresh the screen
            repaint();
            //increase the speed a bit
                    if(p2Speed<=5)
                        p2Speed+=.2;
                    p2.y-=p2Speed;

                    //this delays the refresh rate
                    Thread.sleep(75);
        }
        catch(Exception e)
        {
            //if there is an exception (an error), exitthe loop
            break;
        }
    }
    }

 }
public static void main(String[]args)
{
    new TheCars();
}

}

1 个答案:

答案 0 :(得分:3)

假设您正在将这些Rectangle对象直接绘制到屏幕上,我们必须假设表达式“HEIGHT/2”和“HEIGHT/2 + HEIGHT/10”的结果相同,并且非零但是小。如果HEIGHT是int,并且该值大于2且小于10,那就是这种情况。据推测,这个值至少应该是几百,这些盒子出现在中间的屏幕。检查HEIGHT的值(只需使用System.out.println()即可),并确保它是窗口的实际高度。

修改

现在我们看到了其余代码:每次调用fill3DRect()的第二个参数都是错误的。它应该是y对象的Rectangle成员,这是一个很大的,不同的数字,但是你传递的是width成员,这是一个小的固定数字。将这两个调用更改为这样,您就会回到正轨:

g.fill3DRect(p1.x, p1.y, p1.width, p1.height, true);