2012-05-24 3 views
2

나는 내 컴퓨터 수업을 위해 pac man을 만들고 있는데, 이것은 내가 지금까지 가지고있는 것 ... 나는 wahana가 프로그램에 유령을 추가한다. 그러나 나는 프로그램이 모든 것을 멈추지 않고 사용자가 입력 할 때까지 기다리지 않도록하는 방법을 모른다. c.getchar()을 사용할 때와 마찬가지로 프로그램을 중지하고 사용자 입력을 기다립니다. 내가 사용할 수있는 프로그램이 없으므로 유령이 팩맨을 쫓아 갈 수있는 다른 방법이 있습니까? 제발 도와주세요!프로그램을 일시 중지하지 않고 문자를 얻는 방법

// The "Pac_man" class. 
import java.awt.*; 
import hsa.Console; 


public class Pac_man 
{ 
    static Console c;   // The output console 

    public static void main (String[] args) 
    { 
     c = new Console(); 

     int outline_x = 189, outline_y = 99, body_x = 190, body_y = 100, eye_x = 208, eye_y = 106, outline_position_x = 26; 
     int body_position_x = 30, p = 1, point_x = 0, point_y = 0, run_time = 0, score = 0, random_number, life = 3; 
     char key_entered; 

     background(); 
     redraw (score, outline_x, outline_y, outline_position_x, body_x, body_y, body_position_x, life); 
     c.fillOval (eye_x, eye_y, 4, 4); // eye 

     do 
     { 
      key_entered = c.getChar(); 

      if (key_entered == 'd' || key_entered == 'D') 
      { 
       outline_x = outline_x + 10; 
       body_x = body_x + 10; 
       eye_x = eye_x + 10; 
       outline_position_x = 26; 
       body_position_x = 30; 

       redraw (score, outline_x, outline_y, outline_position_x, body_x, body_y, body_position_x, life); 
       c.fillOval (eye_x, eye_y-1, 4, 4); // eye 
      } 

      if (key_entered == 'a' || key_entered == 'A') 
      { 
       outline_x = outline_x - 10; 
       body_x = body_x - 10; 
       eye_x = eye_x - 10; 
       outline_position_x = 206; 
       body_position_x = 210; 

       redraw (score, outline_x, outline_y, outline_position_x, body_x, body_y, body_position_x, life); 
       c.fillOval (eye_x-6, eye_y, 4, 4); // eye 

      } 

      if (key_entered == 'w' || key_entered == 'W') 
      { 
       outline_y = outline_y - 10; 
       body_y = body_y - 10; 
       eye_y = eye_y - 10; 
       outline_position_x = 116; 
       body_position_x = 120; 

       redraw (score, outline_x, outline_y, outline_position_x, body_x, body_y, body_position_x, life); 
       c.fillOval (eye_x+2, eye_y+2, 4, 4); // eye 
      } 

      if (key_entered == 's' || key_entered == 'S') 
      { 
       outline_y = outline_y + 10; 
       body_y = body_y + 10; 
       eye_y = eye_y + 10; 
       outline_position_x = 296; 
       body_position_x = 300; 

       redraw (score, outline_x, outline_y, outline_position_x, body_x, body_y, body_position_x, life); 
       c.fillOval (eye_x+3, eye_y+8, 4, 4); // eye 
      } 

      if (key_entered == 'p' || key_entered == 'P') 
      { 
       c.setColor (Color.blue); 
       Font f2 = new Font ("Freestyle Script", Font.BOLD, 56); 
       c.setFont (f2); 
       c.drawString ("GAME PAUSED", 173, 250); 

       for (int i = 1; p == i ; i++) 
       { 
        key_entered = c.getChar(); 
        p++; 

        if (key_entered == 'p' || key_entered == 'P') 
        { 
         p = 0; 
         background(); 
         redraw (score, outline_x, outline_y, outline_position_x, body_x, body_y, body_position_x, life); 
         c.fillOval (eye_x, eye_y, 4, 4); // eye 
        } 
        c.setColor (Color.black); 
       } 
       p = 1; 
      } 
      random_number = (int) (Math.random()* 100)+1; 

      //point start 
      if (run_time == score) 
      { 
      do 
      { 
       point_x = (int)(Math.random() * 1000) + 1; 
      } 

      while (point_x < 65 || point_x > 546); 

      do 
      { 
       point_y = (int)(Math.random() * 1000) + 1; 
      } 

      while (point_y < 90 || point_y > 430); 

      run_time ++; 

      } 
      c.setColor (Color.green); 
      c.fillOval (point_x, point_y, 8, 8); 
      // point end 

      if (outline_x > (point_x-32) && outline_x < (point_x+4) && outline_y > (point_y-29) && outline_y < (point_y+7)) 
      { 
       score = score + 1; 
      } 


      if (outline_x < 59) 
      { 
       outline_x = 548; 
       body_x = 548; 
       eye_x = 569; 
      } 

      if (outline_x > 549) 
      { 
       outline_x = 60; 
       body_x = 60; 
       eye_x = 78; 
      } 

      if (outline_y < 89) 
      { 
       outline_y = 419; 
       body_y = 419; 
       eye_y = 425; 
      } 

      if (outline_y > 420) 
      { 
       outline_y = 90; 
       body_y = 90; 
       eye_y = 98; 
      } 

     } 

     while (1 == 1); 

    } // main method 

    public static void background() 
    { 
     c.setColor (new Color (0, 0, 0)); 
     c.fillRect (0, 0, 640, 500); 
     c.setColor (new Color (35, 47, 210)); 
     Font f3 = new Font ("Footlight MT Light", Font.BOLD, 16); 
     c.setFont (f3); 
     c.drawString ("a = move left", 10, 485); 
     c.drawString ("d = move right", 125, 485); 
     c.drawString ("s = move down", 255, 485); 
     c.drawString ("w = move up", 385, 485); 
     c.drawString ("p = pause game", 500, 485); 

    } 

    public static void redraw (int score, int outline_x, int outline_y, int outline_position_x, int body_x, int body_y, int body_position_x, int life) 
    { 
     c.setColor (new Color (0, 0, 0)); 
     c.fillRect (50, 50, 10, 400); // left border 
     c.fillRect (580, 50, 12, 400); // right border 
     c.fillRect (50, 450, 540, 15); // bottom border 
     c.fillRect (50, 40, 540, 10); // top border 
     c.setColor (new Color (35, 47, 210)); 
     Font f2 = new Font ("Footlight MT Light", Font.BOLD, 45); 
     c.setFont (f2); 
     c.drawString ("Welcome to Pac-Man", 100, 37); 
     c.setColor (new Color (125, 23, 137)); 
     c.fillRect (60, 50, 520, 400); 
     Font f3 = new Font ("Footlight MT Light", Font.BOLD, 24); 
     c.setFont (f3); 
     c.setColor (new Color (0, 0, 0)); 
     c.drawString("SCORE : " + score, 61, 75); 
     c.drawString ("Lives :", 380, 75); 
     lives (life); 
     c.fillRect (50, 85, 550, 5);// line 
     c.setColor (Color.black);// pac man drawing begins 
     c.fillArc (outline_x, outline_y, 35, 35, outline_position_x, 320); // outline 
     c.setColor (Color.yellow); 
     c.fillArc (body_x, body_y, 32, 32, body_position_x, 310); // body 
     c.setColor (Color.black);// pac man drawing ends 
    } 

    public static int lives (int life) 
    { 
     int x1 = 470; 

     for (int i = 1; i <= life; i++) 
     { 
      c.setColor (Color.black);// pac man drawing begins 
      c.fillArc (x1, 55, 30, 30, 26, 320); // outline 
      c.setColor (Color.yellow); 
      c.fillArc (x1, 55, 28, 28, 30, 310); // body 
      c.setColor (Color.black);// pac man drawing begins 
      c.fillOval (x1+15, 59, 4, 4); // eye 

      x1+=35; 
     } 
     return life; 
    } 

} // Pac_man class 

답변

1

난 당신의 코드를 읽고 (더 읽을 수 있도록 관련 부분에 단축),하지만 당신이 잘못된 방향으로 접근하는 것 같은 느낌하지 않았다. 아마도 KeyListener을 사용하고 싶을 것입니다. 이렇게하면 프로그램이 실행되는 동안 동시에 누르는 키를 "들을"수 있습니다. 당신이 초점에 문제가있는 경우

public class KeyEventDemo implements KeyListener { 
     typingArea = new JTextField(20); 
     typingArea.addKeyListener(this); 

    /** Handle the key typed event from the text field. */ 
    public void keyTyped(KeyEvent e) { 
     // snip 
    } 

    /** Handle the key-pressed event from the text field. */ 
    public void keyPressed(KeyEvent e) { 
     // snip 
    } 

    /** Handle the key-released event from the text field. */ 
    public void keyReleased(KeyEvent e) { 
     // snip 
    } 

, 나는 또한 KeyListeners에 대한 대안을 How to Use Key Bindings를 읽고 추천 할 것입니다.

0

교사가 팩맨 게임을 그리는 데 사용하는 맞춤형 콘솔 클래스를 만든 것처럼 보입니까?

내가 그래서이 콘솔은 사용하는 클래스입니다있어 가정 .. "hsa.Console"인터넷 검색과 약간의 자바 독을 발견 : Console 구현 선생님의 자바 독에 따르면 http://www.brentwoodhigh.com/apcs/holt/hsa-javadoc/Console.html

KeyListener 그래서 당신은 하위 클래스 당신 자신의 콘솔을 가지고 거기서 주요 사건들을 경청하십시오.

예 :

class MyConsole extends Console { 
    public void keyPressed(KeyEvent e) { 
     super.keyPressed(e); // Call your teacher's code in case he does something 
     // set some shared variable with the key that was typed 
     // you'll read this shared variable in your main loop 
    } 
} 

의 KeyEvent의 자바 독 : http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/event/KeyEvent.html

주에서 당신이 할 수 있습니다 :

c = new MyConsole(); 

대신 메인 루프에서

c = new Console(); 

당신 너와 함께 할거야. 개미가 입력 한 마지막 키를 폴링하려면 ... 기술적으로 당신은 공유 volatile 변수가 필요합니다.하지만 선생님도 모르는 사이에 일찍 터무니없이 스레딩하는 것을 소개합니다.

파일 같은 것을 볼 것이다 : 물론

// The "Pac_man" class. 
import java.awt.*; 
import hsa.Console; 

public class Pac_man { 
    public static volatile char lastChar; 
    static Console c; 
    public static void main(String [] args) { 
     c = new MyConsole(); 
     do { 
      // read whatever character may have been set 
      // by the console 
      char key = lastChar; 
      ...your event loop logic... 
     } while (running); 
    } 
} 
static class MyConsole extends Console { 
    // this method is called by the system whenever a key has been pressed 
    public void keyPressed(KeyEvent e) { 
     // call your prof's version of keyPressed 
     super.keyPressed(e); 

     // set the shared lastChar variable 
     Pac_man.lastChar = e.getKeyChar(); 
    } 
} 

는 선생님이 만든 라이브러리에 대한 액세스 권한을 가지고 있지를 내가 할 수있는 유일한 일 것입니다 무엇에 대한 교육을 추측합니다.

+2

다른 사람들의 숙제에 대한 완벽한 해결책을 쓰지 마십시오. 그것은 그들에게 호의를 베풀지 않습니다. 올바른 방향의 포인터가 훨씬 적합합니다. –

+0

내 선생님이 도서관을 만들지 않았어. http://compsci.ca/holtsoft/에서 프로그램 "Java 프로그램 준비"와 함께 제공되었다고 생각합니다. 프로그램을 다운로드 한 후 파일 -> 새로 만들기 -> hsa consol로 이동하십시오. 템플릿, 나는 코드에서 무슨 일이 일어나는지 이해하지 못한다 ... – user1413945

관련 문제