2013-12-11 162 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()