2013-12-11 208 views
1

因此,我創建了一個非常簡單的平臺遊戲,我希望平臺隨機生成,以便每個遊戲都是獨一無二的。但是,我不確定如何完美地做到這一點。我設法得到了一些實際上在屏幕上生成隨機矩形的工作代碼,但我不確定如何製作,以便「角色」能夠在它們上面移動,並且不會經過或不經過。這是我迄今爲止所做的一切代碼。現在它並不漂亮,但當我知道一切正常時,我會把它整理好。在此先感謝您的幫助:)生成隨機矩形

P.S.我也註釋掉相關的隨機長方形的東西,因爲他們完全填滿屏幕,我看不出這是怎麼回事:P

Game.java

public class Game extends JPanel implements Runnable { 

    public Rectangle character, floor; 
    public int characterWidth = 24; 
    public int characterHeight = 36; 
    public int fps = 1000; //setting framerate 
    public int fallingSpeed = 1; 
    public int fallingFrame = 0; 
    public int floorHeight = 80; 
    public int movementSpeed = 1; 
    public int movementFrame = -1; 
    public int movementFallingSpeed = 10; 
    public int movementResetSpeed = 1; 
    public int jumpingLength = 150; //# of pixels 
    public int jumpingFrame = 1; 
    public int jumpingCountFrame = 0; 
    public int jumpingCountSpeed = fallingSpeed; 
    public int xs = 0; 
    public int ys = 0; 

    public int keyJump = KeyEvent.VK_SPACE; 
    public int keyLeft = KeyEvent.VK_A; 
    public int keyRight = KeyEvent.VK_D; 


    public boolean objectsDefined = false; 
    public boolean falling = false; 
    public boolean running = true; 
    public boolean right = false; 
    public boolean left = false; 
    public boolean jumping = true; 

    public Thread game; 
    RandomRects ad[]; 
    Random rand = new Random(); 

    public Game(Frame f){ 
     setBackground(Color.black); 

     defineObjects(); 

     game = new Thread(this); 
     game.start(); 

     f.addKeyListener(new KeyAdapter(){ 
      public void keyPressed(KeyEvent e){ 
       if (e.getKeyCode() == keyLeft){ 
        left = true; 

       } 

       if (e.getKeyCode() == keyRight){ 
        right = true; 
       } 

       if (e.getKeyCode() == keyJump){ 
        if (!falling){ 
         jumping = true; 
        } 
       } 
      } 

      public void keyReleased(KeyEvent e){ 
       if (e.getKeyCode() == keyLeft){ 
        left = false; 
       } 

       if (e.getKeyCode() == keyRight){ 
        right = false; 
       } 
      } 
     }); 
    } 

    public void defineObjects(){ 

     character = new Rectangle((Main.width/2) - (characterWidth/2), (Main.height/2) - (characterHeight/2), characterWidth, characterHeight); 
     floor = new Rectangle(-10, Main.height - floorHeight, Main.width + 10, floorHeight); 

     ad = new RandomRects[rand.nextInt(200)]; 

     /*for(int count=0; count< ad.length; count++){ 
      int posX1=rand.nextInt(400); 
      int posY1=rand.nextInt(400); 
      int posX2=rand.nextInt(400); 
      int posY2=rand.nextInt(400); 

      Color rectColor= new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); 
      ad[count] = new ad(posX1, posX2, posY1, posY2, rectColor); 

     }*/ 


     objectsDefined = true; 

     repaint(); 

    } 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 

     if (objectsDefined){ 
      g.setColor(Color.WHITE); 
      g.fill3DRect(character.x - xs, character.y - ys, character.width, character.height, true); 
      g.fill3DRect(floor.x -xs , floor.y - ys, floor.width, floor.height, true); 


     } 

     /*for(int count=0; count<ad.length; count++){ 
      ad[count].draw (g); 

     }*/ 
    } 

    public void run() { 
     while (running){ 

     //Character feet 
     Point pt1 = new Point(character.x, character.y + character.height); 
     Point pt2 = new Point(character.x + character.width, character.y + character.height); 

     //Falling 
     if (!jumping){ 
      if (fallingFrame >= fallingSpeed){ 
       if (floor.contains(pt1) || floor.contains(pt2)){ 
        falling = false; 

       }else{ 
        character.y += 1; 
        falling = true; 

       } 

       if (falling){ 
        character.y += 1; 
        //ys+=1; 

       } 

       fallingFrame = 0; 

      }else{ 
       fallingFrame += 1; 


      } 
     } 

     //Jumping 
     if (jumpingCountFrame >= jumpingCountSpeed){ 
      if (jumping){ 
       if (jumpingFrame <= jumpingLength){ 
        character.y -= 1; 
        //ys -=1; 

        jumpingFrame += 1; 

       }else{ 
        jumpingFrame = 0; 
        jumping = false; 
       } 
      } 

      jumpingCountFrame = 0; 
     }else{ 
      jumpingCountFrame += 1; 
     } 

     //Movement Speed Check 
     if (falling){ 
      movementSpeed = movementFallingSpeed; 

     }else{ 
      movementFrame += 1; 
     } 


     //Movement 
     if(movementFrame >= movementSpeed){ 
      if (right){ 
       character.x += 1; 
       xs +=1; 
      } 

      if (left){ 
       character.x -= 1; 
       xs-=1; 

      } 

      movementFrame = -1; 

     }else{ 
      movementFrame += 1; 
     } 

     fpsSetter(); 

     repaint(); 

     } 
    } 

    @SuppressWarnings("static-access") 
    public void fpsSetter(){ 
     try{ 
      game.sleep(fps/1000); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

RandomRects.java

public class RandomRects { 

    private int posX1; 
    private int posY1; 
    private int posX2; 
    private int posY2; 
    private Color rectColor; 

    public RandomRects(int posX1, int posX2, int posY1, int posY2, Color color){ 
     this.posX1 = posX1; 
     this.posX2 = posX2; 
     this.posY1 = posY1; 
     this.posY2 = posY2; 
     this.rectColor = color; 
    } 

    public void draw (Graphics g){ 
     g.setColor(rectColor); 
     g.fillRect(posX1,posY1,posX2,posY2); 
    } 
} 
+1

看看內置[幾何API](http://docs.oracle.com/javase/tutorial/2d/geometry/index.html)它具有允許您繪製和檢查collesions的功能.. – MadProgrammer

+1

請創建一個[簡短,自包含的正確示例](http://sscce.org/)。這是一個問題太多的代碼,沒有人會閱讀所有這些。即使我有時間,根據原則,我拒絕讀取調用其類'aa','ab','ac','ad'的代碼(除非我的老闆強迫我)。 – Amadan

+0

@Amadan抱歉,這是我第一次在這裏發表帖子。我刪除了前兩個班,因爲它們與問題無關。我只叫他們aa,ab等,因爲我不能想到更好的名字給他們哈哈。 –

回答

0

基本上,你需要的是「想象」移動角色;如果移動的角色會與任何矩形重疊(「碰撞檢測」),請阻止移動。如果不是,則確認角色的下一個位置是移動的位置。您可以使用Rectangle方法intersects()