2016-05-17 107 views
0

我對Java和一般編程相當陌生。我正在開發一個小型遊戲,用戶扮演一個白色方塊,其目標是收集屏幕上的所有硬幣。隨着用戶收集硬幣,移動紅色方塊出現在屏幕上,如果用戶觸摸它們將結束遊戲。我使用KeyListener來移動用戶的方塊。在另一個形狀獨立移動時使用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(); 
       } 
     } 
} 

回答

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(); 
    } 

    } 


    } 
} 

}

+0

不,你並不需要爲每一個新的線程移動的對象,這很愚蠢。遊戲循環的一個新線程移動並呈現所有內容就足夠了。 – eldo