使用KeyListener移动形状,而另一个形状独立移动

时间:2016-05-17 18:08:55

标签: java

我对Java和编程很新。我正在开发一个小游戏,用户扮演白色方块,其目的是收集屏幕上的所有硬币。当用户收集硬币时,移动红色正方形出现在屏幕上,如果用户触摸它将结束游戏。我使用KeyListener来移动用户的方块。

我遇到的问题是,当红色方块移动时,我无法移动玩家的方格。当在paint方法中激活while循环时,红色方块将移动。我希望用户能够在红色方块移动时移动他们的方块。

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class FinalGame extends Canvas
{



    private static final long  serialVersionUID   =1L;
    //refers to players coordinates
    static int x=230, y=230;
    static boolean coin1=true;
    static int coinsCollected=0;
    static int mj;
    static boolean bounce=true;
    public FinalGame() {
        setSize(new Dimension(500, 500));
        setBackground(Color.black);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                moveSquare(e);
            }
        });
    }

    public void paint(Graphics g)
        {
          //refers to coin's coordinates
            int c1x=20; int c1y=20;


            g.setColor(Color.white);
            g.fillRect(x,y,20,20);

            g.setColor(Color.yellow);
            g.fillOval(c1x, c1y, 20, 20);

            if((x==c1x)&&(c1y==20))
                {
                    coin1=false;

                }
            if(coin1==false)
                {
                    g.setColor(Color.black);
                    g.fillOval(20, 20, 20, 20);



                int j=230;
                mj=5;

//I am unable to move the square while this loop is active

                while(bounce)
                    {       

                        repaint();
                        if((j==480)||(j==0))
                            {
                                mj=-mj;
                            }

                        j=j+mj;

                        g.setColor(Color.red);
                        g.fillRect(230, j, 20, 20);

                        delay();

                        g.setColor(Color.black);
                        g.fillRect(230, j, 20, 20);

                        g.setColor(Color.white);
                        g.fillRect(x,y,20,20);



                    }
                mj=-mj;


                }

            g.setColor(Color.white);
            g.fillRect(x,y,20,20);

        }

    public void moveSquare(KeyEvent e)
        {
            switch(e.getKeyCode())
            {
                case KeyEvent.VK_DOWN:
                    y += 5;
                    break;
                case KeyEvent.VK_UP:
                    y-= 5;
                    break;
                case KeyEvent.VK_LEFT:
                    x-=5;
                    break;
                case KeyEvent.VK_RIGHT:
                    x+=5;
                    break;
            }
            repaint();
        }
    public static void main(String[] args)
        {
                JFrame frame = new JFrame("Basic Game");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                FinalGame ex = new FinalGame();
                frame.getContentPane().add(ex);
                frame.pack();
                frame.setResizable(false);
                frame.setVisible(true);
                ex.requestFocus();


        }
    public void delay()
        {
        try
                {
                Thread.sleep(15);
                } catch (InterruptedException e)
                {
                e.printStackTrace();
                }
        }
}

1 个答案:

答案 0 :(得分:0)

您的算法如下:

create coins at random

move the player to collect the coins

create red things also at random

所有这些同时发生。

因此,您需要不同的线程来强制执行此同时性。

一个线程为硬币一个线程为红色的东西和一个主线程已经存在于主类:

class FinalGame extends Canvas
{

private static final long  serialVersionUID   =1L;
//refers to players coordinates
static int x=230, y=230;
static boolean coin1=true;
static int coinsCollected=0;
static int mj;
static boolean bounce=true;
Red r;
Coin c;
public FinalGame() {
    setSize(new Dimension(500, 500));
    setBackground(Color.black);
    addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            moveSquare(e);
        }
    });
    r=new Red(this);
    r.start();
    c=new Coin(this);
    c.start();      
}

public void paint(Graphics g)
    {
        if(x==r.x && y==r.y) System.exit(0);

        if(c.visible) {
          g.setColor(Color.yellow);
          g.fillOval(c.c1x, c.c1y, 20, 20);
        }

        g.setColor(Color.white);
        g.fillRect(x,y,20,20);

        g.setColor(Color.red);
        g.fillRect(r.x, r.y, 20, 20);

    }

public void moveSquare(KeyEvent e)
    {

        switch(e.getKeyCode())
        {
            case KeyEvent.VK_DOWN:
                y += 5;
                break;
            case KeyEvent.VK_UP:
                y-= 5;
                break;
            case KeyEvent.VK_LEFT:
                x-=5;
                break;
            case KeyEvent.VK_RIGHT:
                x+=5;
                break;
        }

        repaint();
    }

public void m()
    {
            JFrame frame = new JFrame("Basic Game");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.getContentPane().add(this);
            frame.pack();
            frame.setResizable(false);
            frame.setVisible(true);
            requestFocus();

    }

class Coin extends Thread {
  int c1x=20, c1y=20;
  boolean visible=true;
  FinalGame f;

  public Coin(FinalGame f) {
    this.f=f;
  }

  public void run() {

    while(true)
    {       

        if((f.x==c1x)&&(c1y==20))
        {
                visible=false;

        }               
    }


  }
}


class Red extends Thread {
  int x=130, y=10;
  FinalGame f;

  public Red(FinalGame f) {
    this.f=f;
  }

  public void run() {

            int mj=5;

            while(true)
                {       

                    if((y==480)||(y==0))
                        {
                            mj=-mj;
                        }

                    y=y+mj;

    f.repaint();

    try
    {
            Thread.sleep(15);
    } catch (InterruptedException e)
    {
            e.printStackTrace();
    }

  }


  }
}

}