java - 生成随机矩形

标签 java

所以我正在创建一个非常简单的平台游戏,我希望平台随机生成,以便每个游戏都是独一无二的。但是,我不确定如何完美地做到这一点。我设法获得了一些实际在屏幕上生成随机矩形的工作代码,但我不确定如何制作它,以便“角色”可以在它们上移动并且不会掉落等等。这是我迄今为止所有内容的代码。现在它一点也不漂亮,但当我知道一切正常时我会整理它。预先感谢您的帮助:)

附注我还注释掉了与随机矩形相关的内容,因为它们完全填满了屏幕,我看不到发生了什么:P

游戏.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();
        }
    }
}

随机矩形.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);
     }
}

最佳答案

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

关于java - 生成随机矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20509850/

相关文章:

java - Android:OnTextChanged 电子邮件验证未按预期工作

java - 如何添加 4 个畅销耳机商品以添加到 Amazon.com 的购物车

java - CAS proxyCallbackURL XML 响应

java - 具有不同参数的事件通知的最佳模式是什么?

java - java 中的正则表达式 : matching BOL and EOL

java - 如何获取Json数据

java - 在 android 中集成 Snap-kit

java - 使用Android-UIL,如何停止下载?

java - @Future 的实现在哪里定义?

Java - SonarLint 检测阻止程序 - 在 "PreparedStatement"子句中关闭此 "finally"