gpt4 book ai didi

java - 随机产生球

转载 作者:行者123 更新时间:2023-12-01 10:27:41 29 4
gpt4 key购买 nike

我正在制作一个游戏,球从智能手机屏幕的每一侧随机产生,尽管球主要在屏幕的顶部产生,在屏幕的底部和侧面有 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com