Java 如何在不暂停程序的情况下获取字符

Java 如何在不暂停程序的情况下获取字符,java,methods,char,Java,Methods,Char,我正在为我的计算机课制作一个类似吃豆人的程序,这是我到目前为止所做的…我想在程序中添加幽灵。但我不知道如何使程序不会停止所有操作并等待用户输入。就像我使用c.getchar()时一样,它停止程序并等待用户输入。还有什么我可以用的,不会停止程序,所以它可能是一个幽灵追逐吃豆人。。。请帮忙 // The "Pac_man" class. import java.awt.*; import hsa.Console; public class Pac_man { static Console

我正在为我的计算机课制作一个类似吃豆人的程序,这是我到目前为止所做的…我想在程序中添加幽灵。但我不知道如何使程序不会停止所有操作并等待用户输入。就像我使用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
//吃豆人”类。
导入java.awt.*;
导入hsa.Console;
公共级吃豆人
{
静态控制台c;//输出控制台
公共静态void main(字符串[]args)
{
c=新控制台();
int outline_x=189,outline_y=99,body_x=190,body_y=100,eye_x=208,eye_y=106,outline_x=26;
int body_position_x=30,p=1,point_x=0,point_y=0,run_time=0,score=0,random_number,life=3;
输入字符键;
背景();
重画(分数、轮廓、轮廓、轮廓、位置、身体、身体、位置、生命);
c、 圆角(眼x,眼y,4,4);//眼
做
{
key_entered=c.getChar();
如果(输入的键='d'| |输入的键='d')
{
轮廓x=轮廓x+10;
body_x=body_x+10;
眼x=眼x+10;
轮廓_位置_x=26;
身体位置=30;
重画(分数、轮廓、轮廓、轮廓、位置、身体、身体、位置、生命);
c、 圆角(眼x,眼y-1,4,4);//眼
}
如果(输入的键=='a'| |输入的键=='a')
{
大纲×=大纲×10;
body_x=body_x-10;
眼x=眼x-10;
轮廓_位置_x=206;
车身位置x=210;
重画(分数、轮廓、轮廓、轮廓、位置、身体、身体、位置、生命);
c、 圆形(眼睛x-6,眼睛y,4,4);//眼睛
}
如果(输入的键=='w'| |输入的键=='w')
{
轮廓y=轮廓y-10;
主体y=主体y-10;
眼y=眼y-10;
轮廓_位置_x=116;
车身位置x=120;
重画(分数、轮廓、轮廓、轮廓、位置、身体、身体、位置、生命);
c、 圆形(眼睛x+2,眼睛y+2,4,4);//眼睛
}
如果(输入的键=='s'| |输入的键=='s')
{
轮廓y=轮廓y+10;
主体y=主体y+10;
眼y=眼y+10;
轮廓_位置_x=296;
车身位置x=300;
重画(分数、轮廓、轮廓、轮廓、位置、身体、身体、位置、生命);
c、 圆形(眼睛x+3,眼睛y+8,4,4);//眼睛
}
如果(输入的键='p'| |输入的键='p')
{   
c、 setColor(Color.blue);
Font f2=新字体(“自由式脚本”,Font.BOLD,56);
c、 setFont(f2);
c、 抽绳(“游戏暂停”,173250);
对于(int i=1;p==i;i++)
{
key_entered=c.getChar();
p++;
如果(输入的键='p'| |输入的键='p')
{
p=0;
背景();
重画(分数、轮廓、轮廓、轮廓、位置、身体、身体、位置、生命);
c、 圆角(眼x,眼y,4,4);//眼
}
c、 设置颜色(颜色为黑色);
}
p=1;
}
随机数=(int)(Math.random()*100)+1;
//起点
如果(运行时间==分数)
{
做
{
点x=(int)(Math.random()*1000)+1;
}
而(点|x<65 |点|x>546);
做
{
点y=(int)(Math.random()*1000)+1;
}
而(点y<90 | |点y>430);
运行时++;
}
c、 setColor(Color.green);
c、 圆角(x点,y点,8,8);
//点端
if(大纲>(点x-32)和大纲x<(点x+4)和大纲y>(点y-29)和大纲y<(点y+7))
{
分数=分数+1;
}
如果(轮廓_x<59)
{
轮廓_x=548;
体x=548;
眼x=569;
}
如果(大纲>549)
{
轮廓_x=60;
体x=60;
眼x=78;
}
如果(轮廓y<89)
{
轮廓y=419;
本体y=419;
眼y=425;
}
如果(轮廓y>420)
{
轮廓y=90;
本体y=90;
眼y=98;
}
}
而(1==1);
}//主方法
公共静态无效背景()
{
c、 setColor(新颜色(0,0,0));
c、 fillRect(0,0640500);
c、 setColor(新颜色(35,47,210));
Font f3=新字体(“Footlight MT Light”,Font.BOLD,16);
c、 setFont(f3);
c、 抽绳(“a=向左移动”,10,485);
c、 抽绳(“d=向右移动”,125,485);
c、 抽绳(“s=下移”,255,485);
c、 抽绳(“w=上移”,385485);
c、 抽绳(“p=暂停游戏”,500485);
}
公共静态无效重绘(整数分数、整数轮廓线、整数轮廓线、整数轮廓线位置、整数正文、整数正文、整数正文)
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
    }
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
   }
}
c = new MyConsole();
c = new Console();
// 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();
   }
}