2014-06-11 116 views
-2

我被要求爲我的CS(高中)班級做一個遊戲,作爲我學期末的任務。我們沒有正確地教授製作遊戲所需的所有編碼,所以我在這方面的知識很差。無論如何,我試圖製作的遊戲就像「Flappy Fall」(一款蘋果應用程序遊戲),其中物體從屏幕頂部落下並下降到屏幕底部。目標是在這些物體到達底部之前捕獲這些物體。我能夠得到一個物體落下,並創建了「捕捉器」,但我不知道如何創建多個落下的物體,也不知道如何在捕捉器捕捉到物體後如何移除物體。到目前爲止,我有「JavaGame」,「Catcher」和「Ball」類。任何幫助將不勝感激。創建一個對象的多個實例。 (遊戲)

int x, y; 
int t = 1; 
private Image dbImage; 
private Graphics dbGraphics; 
Image player; 
Image bkg; 
static Catcher p = new Catcher(150, 450); 

public JavaGame() { 
    //Game Images 
    ImageIcon b = new ImageIcon("bkg.png"); 
    bkg = b.getImage(); 

    //Game properties 
    setTitle("Game"); 
    setSize(350, 600); 
    setResizable(false); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLocationRelativeTo(null); 
    addKeyListener(new Keys()); 
    addMouseMotionListener(new Mouse()); 


} 

public void paint(Graphics g) { 
    // 
    dbImage = createImage(getWidth(), getHeight()); 
    dbGraphics = dbImage.getGraphics(); 
    draw(dbGraphics); 
    g.drawImage(dbImage, 0, 0, this); 
} 





public void draw(Graphics g) { 
    g.drawImage(bkg, 0, 0, this); //Creates background 
    p.draw(g); 
    //while (t < 100) { 
     p.b.draw(g); 
     //t++; 
    //} 
    g.setColor(Color.WHITE); 
    g.drawString(""+p.score, 175, 50); 
    repaint(); 
} 


public static void main(String[] args) { 
    JavaGame jg = new JavaGame(); 

    //Threads 
    Thread p1 = new Thread(p); 
    p1.start(); 
    Thread ball = new Thread(p.b); 
    ball.start(); 
} 

public class Keys extends KeyAdapter { 
    public void keyPressed(KeyEvent e) { 
     p.keyPressed(e); 
    } 

    public void keyReleased(KeyEvent e) { 
     p.keyReleased(e); 
    } 
} 

public class Mouse implements MouseMotionListener { 
    public void mouseDragged(MouseEvent e) { 
     p.mouseDragged(e); 
    } 
    public void mouseMoved(MouseEvent e) { 
     p.mouseMoved(e); 
    } 
} 

}

int x, y, ranX, xDirection; 
int score; 
Rectangle catch1; 
Ball b = new Ball(170, 1); 

public Catcher (int x, int y) { 
    score = 0; 
    this.x = x; 
    this.y = y; 
    catch1 = new Rectangle(this.x, this.y, 50, 15); 
} 

public void run() { 
    try { 
     while(true) { 
      move(); 
      Thread.sleep(5); 
     } 
    } 

    catch(Exception e) { 
     System.out.println("Error"); 
    } 
} 


public void collision() { 
    if (b.ball.intersects(catch1)) { 
     b.ball(Color.blue); 
     score++; 
     System.out.println(score); 
    } 
} 

public void move() { 
    collision(); 
    catch1.x += xDirection; 
    if (catch1.x <= 0) 
     catch1.x = 0; 
    if (catch1.x >= 300) 
     catch1.x = 300; 
} 

public void setXDirection(int xDir) { 
    xDirection = xDir; 
} 

    public void keyPressed(KeyEvent m) { 
     int keyCode = m.getKeyCode(); 
     if (keyCode == m.VK_LEFT) { 
      setXDirection(-1); 
     }  

     if (keyCode == m.VK_RIGHT) { 
      setXDirection(+1); 
     } 
     m.consume(); 
    }    

    public void keyReleased(KeyEvent m) { 
     int keyCode = m.getKeyCode(); 
     if (keyCode == m.VK_LEFT) { 
      setXDirection(0); 
     }     

     if (keyCode == m.VK_RIGHT) { 
      setXDirection(0); 
     } 
     m.consume(); 
    } 

    public void mouseDragged(MouseEvent e) { 
     catch1.x = e.getX()-25; 
     e.consume(); 
    } 
    public void mouseMoved(MouseEvent e) { 
     catch1.x = e.getX()-25; 
     e.consume(); 
    } 

public void draw(Graphics g) { 
    g.fillRect(catch1.x, catch1.y, catch1.width, catch1.height); 

} 

}

int x, y, yDirection; 
Rectangle ball; 

public Ball (int x, int y) { 
    this.x = x; 
    this.y = y; 
    ball = new Rectangle(this.x, this.y, 10, 10); 
} 

public void run() { 
    try{ 
     while(true) { 
      move(); 
      Thread.sleep(5); 
     } 
    } 

    catch(Exception e) { 
     System.out.println("Error"); 
    } 
} 

public void move() { 
    if (ball.y >= 600) { 
     ball.y = 600; 
    } 

    if (ball.y > 0) { 
     ball.y++; 
    } 
} 

public void setYDirection(int yDir) { 
    yDirection = yDir; 
} 

public void draw(Graphics g) { 
    g.setColor(Color.WHITE); 
    g.fillRect(ball.x, ball.y, ball.width, ball.height); 
    System.out.println(ball.x+ " "+ ball.y+ " " + ball.width + " " + ball.height); 
} 

}

回答

2

因爲我不想做你的作業,我在這裏只給一個簡短的回答:

致電new Ball()多次。 當然,您可能想要將它們添加到集合中,例如 List list = new ArrayList(); list.add(ball);

一旦它們完成,將它們從該集合中刪除。

3

我會重新組織一些代碼。在主遊戲中,您可以收集「球」類型的集合。我會把收藏選項留給你。但是,您需要將「新」球添加到收藏中,然後在捕捉它們後將其移除。

相關問題