java - 随机产生球

标签 java android random libgdx

我正在制作一个游戏,球从智能手机屏幕的每一侧随机产生,尽管球主要在屏幕的顶部产生,在屏幕的底部和侧面有 2/10 次产生,但我得到了它的工作。代码如下:

//creates the balls and sets their position as well as the random timer for each
private void spawnBalls1() {

    Rectangle ball1 = new Rectangle();
    ball1.x = MathUtils.random(639, 641);
    ball1.y = 720;
    ball1.width = 32;
    ball1.height = 32;
    balls1.add(ball1);
    lastDropTime = TimeUtils.nanoTime();

}

private void spawnBalls2() {

    Rectangle ball2 = new Rectangle();
    ball2.x = 0;
    ball2.y = MathUtils.random(359, 361);
    ball2.width = 32;
    ball2.height = 32;
    balls2.add(ball2);
    lastDropTime = TimeUtils.nanoTime();
}

private void spawnBalls3() {

    Rectangle ball3 = new Rectangle();
    ball3.x = MathUtils.random(639, 641);
    ball3.y = 0;
    ball3.width = 32;
    ball3.height = 32;
    balls3.add(ball3);
    lastDropTime = TimeUtils.nanoTime();
}

private void spawnBalls4() {

    Rectangle ball4 = new Rectangle();
    ball4.x = 1280;
    ball4.y = MathUtils.random(359, 361);
    ball4.width = 32;
    ball4.height = 32;
    balls4.add(ball4);
    lastDropTime = TimeUtils.nanoTime();
}


//draws the balls
    for (Rectangle ball1 : balls1) {

        batch.draw(Ball, ball1.x, ball1.y);
    }

    for (Rectangle ball2 : balls2) {

        batch.draw(Ball, ball2.x, ball2.y);
    }

    for (Rectangle ball3 : balls3) {

        batch.draw(Ball, ball3.x, ball3.y);
    }

    for (Rectangle ball4 : balls4) {

        batch.draw(Ball, ball4.x, ball4.y);
    }


// if the time minus the time of the last ball spawn is less than x then spawn another ball in a random place
    if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls1();

    Iterator<Rectangle> iter1 = balls1.iterator();
    while(iter1.hasNext()) {

        Rectangle balls1 = iter1.next();
        balls1.y -= 500 * Gdx.graphics.getDeltaTime();
        if (balls1.overlaps(square)) {

            score++;
            showScore = "Score: " + score;
            iter1.remove();
        }
    }

    // if the time minus the time of the last ball spawn is less than x then spawn another ball in a random place
    if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls2();

    Iterator<Rectangle> iter2 = balls2.iterator();
    while (iter2.hasNext()) {

        Rectangle balls2 = iter2.next();
        balls2.x += 500 * Gdx.graphics.getDeltaTime();
        if (balls2.overlaps(square)) {

            score++;
            showScore = "Score: " + score;
            iter2.remove();
        }
    }

    // if the time minus the time of the last ball spawn is less than x then spawn another ball in a random place
    if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls3();

    Iterator<Rectangle> iter3 = balls3.iterator();
    while(iter3.hasNext()) {

        Rectangle balls3 = iter3.next();
        balls3.y += 500 * Gdx.graphics.getDeltaTime();
        if (balls3.overlaps(square)) {

            score++;
            showScore = "Score: " + score;
            iter3.remove();
        }
    }

    // if the time minus the time of the last ball spawn is less than x then spawn another ball in a random place
    if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls4();

    Iterator<Rectangle> iter4 = balls4.iterator();
    while(iter4.hasNext()) {

        Rectangle balls4 = iter4.next();
        balls4.x -= 500 * Gdx.graphics.getDeltaTime();
        if (balls4.overlaps(square)) {

            score++;
            showScore = "Score: " + score;
            iter4.remove();
        }
    }

我应该做什么/改变才能让它们在每一侧随机生成?

提前致谢!

最佳答案

所有四种球生成方法共享相同的 lastDropTime 变量。执行时间

if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls1();

if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBalls4();

将是 1000000000 ns 的一小部分,因此 spawnBalls2-4 不太可能被调用。

您应该只进行一项测试而不是四项,并随机选择一种方法。像这样的事情:

if (TimeUtils.nanoTime() - lastDropTime > 1000000000){
    switch (MathUtils.randomInt(4)){
        case 0: spawnBalls1(); break;
        case 1: spawnBalls2(); break;
        case 2: spawnBalls3(); break;
        case 3: spawnBalls4(); break;
    }
}

此外,这是大量的复制粘贴代码,这会增加您的时间投入。您应该编写一个 spawnBalls 方法,该方法采用一些影响球放置位置的参数。

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

相关文章:

java - java中的下拉菜单?

java - Android 设备上的 IndexOutOfBoundsException 错误不在模拟器上

java - JAXb 在解码期间不填充对象

java - 发送带有 URL 中的查询字符串的 HTTP Post

android - 对等方未通过身份验证-Android Studio

android - Android中的Realm VS Room

python - 生成总和为 1 的随机变量数组(正数和负数)

python - 快速选择范围内一定百分比的元素

java - Java 中的插值 : Mapping a random number from one range to another

用于创建加密 SAML 断言的 Java 代码