2016-03-13 74 views
3

我是一個初學者,我有一個程序是一個遊戲,你從一個點到另一個點。你是一個綠色塊,你避免了紅色塊。但我不知道如何創建另一個移動的紅色塊,而無需創建另一種方法enemyMovement2(),這似乎效率低下,因爲如果我想創造這些敵人中的許多,那該怎麼辦。有沒有辦法使這種行爲產生arraylist我的程序似乎效率低下

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.util.ArrayList; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

@SuppressWarnings("serial") 
public class Game extends JPanel implements KeyListener, ActionListener { 
    private static boolean dead = false; 
    private static boolean won = false; 

    boolean isPaused=false; 
    boolean hasStarted=false; 
    int level = 1; 

    boolean powerUpIsEaten; 
    boolean keepPowerUpOffScreen; 
    int powerupx; 
    int powerupy; 
    int powerupwidth=20; 
    int powerupheight=20; 

    int winningx; 
    int winningy; 
    int winningwidth; 
    int winningheight; 

    int losingx; 
    int losingy; 
    int losingwidth; 
    int losingheight; 

    int losing2x; 
    int losing2y; 
    int losing2width; 
    int losing2height; 

    int losing3x; 
    int losing3y; 
    int losing3width; 
    int losing3height; 

    int losing4x; 
    int losing4y; 
    int losing4width; 
    int losing4height; 

    int losing5x; 
    int losing5y; 
    int losing5width; 
    int losing5height; 


    int enemywidth; 
    int enemyheight; 

    int userwidth; 
    int userheight; 


    ArrayList<Integer> badRectangles = new ArrayList<Integer>(); 
    int x = 1, y = 1; 

    int enemyX=400, enemyY=100; 

    int velx=0, vely=0; 

    boolean hasWon = false, hasDied = false; 

    int movementcounter=0; 

    private void move() throws InterruptedException { 
     addKeyListener(this); 
     setFocusable(true); 
     setFocusTraversalKeysEnabled(false); 

     if (vely > 0){ 
      if(y>700-userheight *2){   
      } 
      else{ 
       y = y + 5 + movementcounter/300; 
      } 
     } 
     if (vely < 0){ 
      if(y<0){   
      } 
      else{ 
       y = y - 5 - movementcounter/300; 
      } 
     } 
     if (velx > 0){ 
      if(x>1200-userwidth){ 
      } 
      else{ 
       x = x + 5 + movementcounter/300; 
      } 
     } 
     if (velx < 0){ 
      if(x<0){ 
      } 
      else{ 
       x = x - 5 - movementcounter/300; 
      } 
     } 
     if(isInARectangle()){ 
      hasDied=true; 
     } 
     if(isInWinningArea()){ 
      x=0; 
      y=0; 
      hasWon=true; 
     } 
     movementcounter++; 
    } 
    @Override 
    public void paint(Graphics g) { 
     super.paint(g); 
     getValues(level); 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
     g.setFont(new Font("TimesRoman", Font.PLAIN, 30)); 
     if(level==5){ 
      g2d.drawString("Congratulations", 550, 350); 
      this.won=true; 

     } 
     else if(hasStarted 
       && !isPaused){ 
      eatPowerup(); 
      g2d.setColor(Color.RED); 
      for(int i=1; i<=level; i++){ 
       g2d.fillRect(badRectangles.get(4 * i - 4),badRectangles.get(4 * i - 3),badRectangles.get(4 * i - 2),badRectangles.get(4 * i - 1)); 
      } 
      g2d.setColor(Color.GREEN); 
      g2d.fillRect(winningx, winningy, winningwidth, winningheight); //WINNING RECTANGLE 
      g2d.setColor(Color.RED); 
      g2d.fillRect(enemyX, enemyY, enemywidth, enemyheight); //ENEMY 
      enemyMovement(); 

      if(!hasDied){ 
       g2d.setColor(Color.GREEN); 
       g2d.fillRect(x, y, userwidth, userheight); //USER 
      } 
      if(hasWon 
        && !hasDied){ 
       level++; 
       if(level==2){ 
        enemyX=1000; 
        enemyY=0; 
       } 
       if(level==3){ 
        enemyX=700; 
        enemyY=0; 
       } 
       if(level==4){ 
        enemyX=400; 
        enemyY=100; 
       } 
       powerUpIsEaten=false; 
       hasWon=false; 
       keepPowerUpOffScreen=false; 
      } 
      if(hasDied){ 
       g2d.setColor(Color.RED); 
       g2d.drawString("You died", 500, 300); //LOSING SEQUENCE 
       this.dead=hasDied; 
      } 
      if(!powerUpIsEaten 
        && level>1 
        && !keepPowerUpOffScreen){ 
       g2d.setColor(Color.BLUE); 
       g2d.fillRect(powerupx, powerupy, powerupwidth, powerupheight); 
      } 
      else{ 
       keepPowerUpOffScreen=true; 
       powerUpIsEaten=false; 
      } 
     } 
     if(isPaused){ 
      g2d.drawString("PAUSED", 450, 300); 
     } 
     if(!hasStarted){ 
      g2d.drawString("PRESS UP TO START", 450, 300); 
     } 
    } 
    public void eatPowerup(){ 
     if(powerupx<x + userwidth + 5 
       && powerupx>x 
       && powerupy<y + userheight + 5 
       && powerupy>y){ 
      powerUpIsEaten=true; 
      powerupx=0; 
      powerupy=1000; 
      enemyX=0; 
      enemyY=0; 
     } 
     else{ 

      powerUpIsEaten=false; 
     } 
    } 
    public boolean isInWinningArea(){ 
     if(x<winningx + winningwidth - userwidth 
       && x>winningx 
       && y<winningy + winningheight-userheight 
       && y>winningy){ 
      return true; 
     } 
     else{ 
      return false; 
     } 
    } 
    public boolean isInARectangle(){ 
     if((x > losingx-userwidth 
       && x < losingx + losingwidth 
       && y > losingy-userheight 
       && y < losingy + losingheight) 
       || x > losing2x-userwidth 
       && x < losing2x + losing2width 
       && y > losing2y-userheight 
       && y < losing2y + losing2height 
       || x > losing3x-userwidth 
       && x < losing3x + losing3width 
       && y > losing3y-userheight 
       && y < losing3y + losing3height 
       || x > losing4x-userwidth 
       && x < losing4x + losing4width 
       && y > losing4y-userheight 
       && y < losing4y + losing4height){ 
      return true; 
     } 
     else{ 
      return false; 
     } 

    } 
    public void getValues(int level){ 
     if(level==1){ 

      winningx=550; 
      winningy=550; 
      winningwidth=100; 
      winningheight=100; 

      losingx=200; 
      losingy=100; 
      losingwidth=200; 
      losingheight=200; 

      enemywidth=60; 
      enemyheight=60; 

      userwidth=50; 
      userheight=50; 

     } 
     if(level==2){ 

      winningx=1000; 
      winningy=500; 
      winningwidth=200; 
      winningheight=200; 

      losingx=200; 
      losingy=100; 
      losingwidth=200; 
      losingheight=200; 

      losing2x=700; 
      losing2y=500; 
      losing2width=300; 
      losing2height=200; 

      enemywidth=60; 
      enemyheight=60; 

      userwidth=50; 
      userheight=50; 

      powerupx=900; 
      powerupy=100; 

     } 
     if(level==3){ 

      winningx=1000; 
      winningy=500; 
      winningwidth=200; 
      winningheight=200; 

      losingx=200; 
      losingy=100; 
      losingwidth=200; 
      losingheight=200; 

      losing2x=700; 
      losing2y=500; 
      losing2width=300; 
      losing2height=200; 

      losing3x=100; 
      losing3y=100; 
      losing3width=100; 
      losing3height=450; 

      enemywidth=60; 
      enemyheight=60; 

      userwidth=50; 
      userheight=50; 

      powerupx=100; 
      powerupy=600; 
     } 
     if(level==4){ 

      winningx=1000; 
      winningy=500; 
      winningwidth=200; 
      winningheight=200; 

      losingx=200; 
      losingy=100; 
      losingwidth=200; 
      losingheight=200; 

      losing2x=700; 
      losing2y=500; 
      losing2width=300; 
      losing2height=200; 

      losing3x=100; 
      losing3y=100; 
      losing3width=100; 
      losing3height=450; 

      losing4x=900; 
      losing4y=300; 
      losing4width=500; 
      losing4height=100; 

      enemywidth=60; 
      enemyheight=60; 

      userwidth=50; 
      userheight=50; 

      powerupx=600; 
      powerupy=500; 
     } 
     if (level==5){ 

     } 
     badRectangles.clear(); 
     badRectangles.add(losingx); 
     badRectangles.add(losingy); 
     badRectangles.add(losingwidth); 
     badRectangles.add(losingheight); 

     badRectangles.add(losing2x); 
     badRectangles.add(losing2y); 
     badRectangles.add(losing2width); 
     badRectangles.add(losing2height); 

     badRectangles.add(losing3x); 
     badRectangles.add(losing3y); 
     badRectangles.add(losing3width); 
     badRectangles.add(losing3height); 

     badRectangles.add(losing4x); 
     badRectangles.add(losing4y); 
     badRectangles.add(losing4width); 
     badRectangles.add(losing4height); 

     badRectangles.add(losing5x); 
     badRectangles.add(losing5y); 
     badRectangles.add(losing5width); 
     badRectangles.add(losing5height); 
    } 
    public void enemyMovement(){ 

     if(enemyX<x){ 
      enemyX= enemyX+1 + movementcounter/300; 
     } 
     if(enemyX>x){ 
      enemyX=enemyX-1 - movementcounter/300; 
     } 
     if(enemyY<y){ 
      enemyY=enemyY + 1 + movementcounter/300; 
     } 
     if(enemyY>y){ 
      enemyY=enemyY -1 - movementcounter/300; 
     } 
     if(x<enemyX+enemywidth 
       && x>enemyX-userwidth 
       && y<enemyY+ enemyheight 
       && y>enemyY-userheight){ 
      hasDied=true; 
     } 

    } 
    @Override 
    public void keyPressed(KeyEvent e) { 
     int key = e.getKeyCode(); 
     switch(key) { 
     case KeyEvent.VK_UP: 
      isPaused=false; 
      hasStarted=true; 
      vely = -1; 
      break; 
     case KeyEvent.VK_DOWN: 
      vely = 1; 
      break; 
     case KeyEvent.VK_LEFT: 
      velx = -1; 
      break; 
     case KeyEvent.VK_RIGHT : 
      velx = 1; 
      break; 
     case KeyEvent.VK_P: 
      isPaused=true; 
     } 

    } 
    @Override 
    public void keyReleased(KeyEvent e) { 
     velx = 0; 
     vely = 0; 

    } 
    @Override 
    public void keyTyped(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 
    public static void main(String[] args) throws InterruptedException { 

     JFrame frame = new JFrame("Square Game"); 
     Game game = new Game(); 
     frame.add(game); 
     frame.setSize(1200, 700); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setBackground(Color.BLUE); 

     while (!dead 
       && !won) { 

      game.move(); 
      game.repaint(); 
      Thread.sleep(10); 
     } 
    } 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

    } 
} 
+2

我想你應該考慮效率低下,不良的設計之間的差異。你的問題似乎與後者有關。 – ChiefTwoPencils

+0

工作代碼。爲什麼不嘗試Code Review? –

+0

避免使用'paint'並考慮'paintComponent'。避免使用'KeyListener'並考慮使用[Key Bindings API](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html)而不是 – MadProgrammer

回答

4

這是一個噸代碼!但是,您可以快速瀏覽一下您未能實現的內容。

一些遊戲對象是Movable

因此,製作一個Movableinterface其中每個可移動的GO執行。然後,您只需遍歷MovablemovableGameObject.move()即可。

例子:

public interface Movable { 
    void move(); 
} 

class Player extends GameObject implements Movable { 

    @Override 
    public void move() { 
     System.out.println("Player moving..."); 
    } 
} 

class Enemy extends GameObject implements Movable { 

    @Override 
    public void move() { 
     System.out.println("Enemy moving...") 
    } 
} 

public static void main(String... args) { 
    Arrays.asList(
     new Movable[] { 
      new Player(), new Enemy() 
     } 
    ).forEach(Movable::move); 
} 

正如預期的那樣,這使輸出...

Player moving... 
Enemy moving...