2012-05-19 2 views
0

enter image description here프로그래밍 사악한 천재를위한 비디오 게임 : 프로젝트 10 : 과격한 레이싱 자동차

Iv'e이 책에 대한 답변 모든 것을 보았다. 그리고 나는이 책을 읽으려고하는 다른 사람이 같은 방식으로 느낀다는 것을 압니다. "이블 천재를위한 비디오 게임 프로그래밍"이 책을 읽은 사람이 있습니까? 저는 프로젝트 10에 있습니다 : 래디컬 레이싱 - 자동차. 모든 것은 올바르게 컴파일되지만 어떤 이유로 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(); 
} 

} 
+3

이 질문은 향후 방문자를 돕기는 어려울 수 있습니다. 작은 지리적 영역, 특정 순간에만 관련이 있거나 인터넷의 전 세계 시청자에게 일반적으로 적용 할 수없는 매우 좁은 상황에서만 관련이 있습니다. 도움이 필요하면 책이없는 사람들도 도움을 줄 수 있도록 자세한 정보를 게시해야합니다. –

+1

HEIGHT 및 WIDTH의 값은 무엇입니까? –

+3

더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. 지금 내가 추측 할 수있는 최선은 '코드가 잘못되었습니다'입니다. –

답변

3

화면에 직접 그 Rectangle 오브젝트를 칠하고 가정 할 때, 우리는 표현은 "HEIGHT/2"와 "HEIGHT/2 + HEIGHT/10"이 나오고 있다고 가정 할 필요가 동일하고, 제로가 아닌하지만 작은. HEIGHT가 int이고 값이 2보다 크고 10보다 작은 경우에 해당합니다. 아마도이 값이 화면 가운데에 나타나려면 이백 개가되어야합니다. HEIGHT의 값을 확인하고 (정확히 System.out.println()을 사용하면됩니다) 창 실제 높이를 확인하십시오.

EDIT 이제 우리는 코드의 나머지 볼 것을

: fill3DRect() 각 호출에 두 번째 인수는 잘못된 것입니다. Rectangle 개체의 y 구성원이어야하며 크기는 다양하지만 작은 고정 숫자 인 width 구성원을 전달하고 있습니다. 다음과 같이 통화를 변경하면 궤도에 다시 돌아옵니다.

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

나는 여전히 코드를 이해하려고 노력하고 있지만 수백 개의 숫자로 값을 변경하려고 시도했다. 움직이지 않아. –

+0

@MartinMarino - 더 많은 코드를 보여 주셔서 감사합니다. 내 대답을 편집하여 올바른 해결책을 포함 시켰습니다. 제 답변을 "수락 됨"으로 표시하십시오. –

+0

우누 (WOOOOOW). 나는 실수했다고 믿을 수 없다. 내가 그걸 알아들을 지 모르겠다. 도움을 많이 주셔서 감사합니다. 코드가 지금과 같은 방식으로 작동하고 있다고 확신합니다. 내가 너에게 엄지 손가락을 줄 수 있다면 나는 새로운 사람이고 적어도 15의 담당자가 필요하다. –