Java 生成随机矩形

Java 生成随机矩形,java,Java,所以我正在创建一个非常简单的平台游戏,我想让平台随机生成,这样每个playthrough都是唯一的。然而,我不确定如何做到完美。我设法得到了一些在屏幕上实际生成随机矩形的工作代码,但我不确定如何使“角色”可以在屏幕上移动,不会掉下来,什么都不会。这是我到目前为止的所有代码。现在一点也不漂亮,但我知道一切正常后会把它整理好的。提前感谢您的帮助:) 另外,我还注释了与随机矩形相关的内容,因为它们完全填满了屏幕,我看不出发生了什么:p Game.java public class Game e

所以我正在创建一个非常简单的平台游戏,我想让平台随机生成,这样每个playthrough都是唯一的。然而,我不确定如何做到完美。我设法得到了一些在屏幕上实际生成随机矩形的工作代码,但我不确定如何使“角色”可以在屏幕上移动,不会掉下来,什么都不会。这是我到目前为止的所有代码。现在一点也不漂亮,但我知道一切正常后会把它整理好的。提前感谢您的帮助:)

另外,我还注释了与随机矩形相关的内容,因为它们完全填满了屏幕,我看不出发生了什么: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();
        }
    }
}

基本上,你需要的是“想象”移动角色;如果移动的角色将与任何矩形重叠(“碰撞检测”),请阻止移动。如果没有,则确认角色的下一个位置是移动的位置。您可以使用
Rectangle
方法。

看看它的内置功能,它允许您绘制和检查集合…请创建一个。对于一个问题来说,这是太多的代码了,没有人会读所有这些。即使我有时间,原则上我也拒绝阅读那些调用其类的代码(除非我的老板强迫我这么做)。第一步是弄清楚他们在做什么。例如,如果一个类负责随机矩形,可以称它为
RandomRect
或类似的东西。命名东西对程序员来说是一项重要的技能。如果您不想与其他程序员合作,也不想在编写代码两个月后对其进行维护,那么请继续使用nonce名称。您可能希望在发布之前先清理一下。正如@Amadan所说的,这很难理解。例如,为什么创建时不将
ad
命名为
MyRectangle
ColoredRect
或类似的名称?它使阅读更容易,所以当我在ac类的代码中看到
ad
时,我不必考虑我在看什么。
    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);
     }
}