0

在此处输入图像描述

我四处寻找这本书的答案。我知道任何试图阅读这本书的人都有同样的感受。这本书叫《为邪恶天才编程电子游戏》 有人读过这本书吗?我在项目 10:激进赛车-汽车。一切都正确编译,但由于某种原因,我的汽车没有出现在 JFrame 上的正确位置。它们应该出现在两条白线下方。我很肯定代码与书中的代码完全相同,但是这本书是错误的。我已经尝试过更改原点的高度部分,但无论我做什么,它都不会让步。我不能附上图片,因为我没有至少 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();
}

}
4

1 回答 1

3

假设您将这些Rectangle对象直接绘制到屏幕上,我们必须假设表达式“ HEIGHT/2”和“ HEIGHT/2 + HEIGHT/10”输出相等,并且非零但很小。如果 HEIGHT 是int,并且该值大于 2 且小于 10,就会出现这种情况。大概该值至少需要几百个,这些框才能显示在屏幕中间。检查HEIGHT(只使用 aSystem.out.println()就可以了)的值并确保它是窗口的实际高度。

编辑

现在我们看到了代码的其余部分:每个调用的第二个参数fill3DRect()是错误的。它应该是对象的y成员Rectangle,这是一个很大的、可变的数字,但您传递的width是一个小的固定数字。将两个调用更改为如下所示,您将回到正轨:

g.fill3DRect(p1.x, p1.y, p1.width, p1.height, true);
于 2012-05-19T00:52:24.420 回答