简体   繁体   English

如何在此代码中移动随机对象?

[英]How would I make a random object move in this code?

Here is the deal, basically I have to have a code that has a bucket that can catch the falling fruit and every time it catches it you get a point a new fruit falls. 这是交易,基本上我必须有一个代码,该代码具有可以捕获下降的水果的存储桶,每次捕获到该存储桶,您便会获得一个新的水果下降的分数。

So I know how to make the bucket move and how to make the fruit go again once it reaches the bottom. 因此,我知道如何使铲斗运动,以及一旦水果到达底部,如何使水果再次通过。 However, I don't know how to make it actually fall. 但是,我不知道如何使它真正下降。 So far i got a switch but no idea what to do with it. 到目前为止,我有了一个开关,但是不知道该怎么做。 I got the fruit popping up in random places which is a start. 我在随机的地方突然出现了水果,这是一个开始。 Anyways, here is my code. 无论如何,这是我的代码。 All help appreciated. 所有帮助表示赞赏。 Again, i need to have a random fruit drop once one of them reaches the bottom. 同样,一旦其中一个到达底部,我需要随机放一个水果。

 import java.awt.Color; import java.awt.event.KeyEvent; import acm.graphics.GOval; import acm.graphics.GPolygon; import acm.graphics.GRect; import acm.program.GraphicsProgram; import acm.util.RandomGenerator; import java.awt.event.*; public class FruitCatcher extends GraphicsProgram { private static final int APPLET_WIDTH = 500; private static final int APPLET_HEIGHT = 500; private static final int BUCKET_X = 250; private static final int BUCKET_Y = 500; private static final int BUCKET_SPEED = 10; private static final int BUCKET_SPEED2 = -10; private GPolygon Bucket; public void init() { setSize(APPLET_WIDTH, APPLET_HEIGHT); addKeyListeners(); } public void run() { RandomGenerator random = new RandomGenerator(); makeBucket(); for (int i = 1; i <= 3; i++) { int randomX = random.nextInt(0, 300 - 20); addFruit(i, randomX, 0); } while (true) ; } public void makeBucket() { Bucket = new GPolygon(BUCKET_X, BUCKET_Y); Bucket.addVertex(-60, 0); Bucket.addVertex(-70, -85); Bucket.addVertex(10, -85); Bucket.addVertex(0, 0); add(Bucket); Bucket.setFilled(true); Bucket.setFillColor(Color.GRAY); } public void addFruit(int a, int x, int y) { switch (a) { case 1: GRect Banana = new GRect(x, y, 10, 60); Banana.setColor(Color.YELLOW); Banana.setFilled(true); add(Banana); break; case 2: GOval lime = new GOval(x, y, 20, 20); lime.setColor(Color.GREEN); lime.setFilled(true); add(lime); break; case 3: GOval Orange = new GOval(x, y, 30, 30); Orange.setColor(Color.ORANGE); Orange.setFilled(true); add(Orange); } } public void keyPressed(KeyEvent event) { int keyCode = event.getKeyCode(); switch (keyCode) { case KeyEvent.VK_LEFT: if (Bucket.getX() > 0) { Bucket.move(-BUCKET_SPEED, 0); } break; case KeyEvent.VK_RIGHT: if (Bucket.getX() < APPLET_WIDTH) { Bucket.move(BUCKET_SPEED, 0); } break; } } } 

In my code you can see a while (true) I am just assuming that is where I would write it. 在我的代码中,您可以看到一会儿(true),我只是假设那是我编写它的地方。 However, I am at a little loss what would actually go there. 但是,我有些不知所措。

  • You need to keep a list of references to all the fruits, so you can manipulate them later 您需要保留对所有水果的引用列表,以便稍后进行操作
  • Each iteration of the game loop, move each fruit down 游戏循环的每次迭代,将每个水果下移
  • You may want to implement some timing mechanism, so that the fruit speed is not dependent on the CPU speed. 您可能需要实现一些计时机制,以便使水果速度不依赖于CPU速度。

     package jsyn; import java.awt.Color; import java.awt.Component; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import acm.graphics.GOval; import acm.graphics.GPolygon; import acm.graphics.GRect; import acm.program.GraphicsProgram; import acm.util.RandomGenerator; import java.awt.event.*; public class FruitCatcher extends GraphicsProgram { private static final int APPLET_WIDTH = 500; private static final int APPLET_HEIGHT = 500; private static final int BUCKET_X = 250; private static final int BUCKET_Y = 500; private static final int BUCKET_SPEED = 10; private static final int BUCKET_SPEED2 = -10; //Speed of fruit falling private static final int FRUIT_PX_PER_MS = 10; private GPolygon Bucket; public void init() { setSize(APPLET_WIDTH, APPLET_HEIGHT); addKeyListeners(); fruits = new ArrayList<Component>(); } // Keep list of fruits List<GObject> fruits; public void run() { RandomGenerator random = new RandomGenerator(); makeBucket(); for (int i = 1; i <= 3; i++) { int randomX = random.nextInt(0, 300 - 20); addFruit(i, randomX, 0); } long last = System.currentTimeMillis(); while (true) { long current = System.currentTimeMillis(); update(current - last); last = current; } } void update(long delta) { for (GObject fruit : fruits) { //this code may not work, replace with code that moves fruit down fruit.setLocation(fruit.getX(), fruit.getY() + delta * FRUIT_PX_PER_MS); } } public void makeBucket() { Bucket = new GPolygon(BUCKET_X, BUCKET_Y); Bucket.addVertex(-60, 0); Bucket.addVertex(-70, -85); Bucket.addVertex(10, -85); Bucket.addVertex(0, 0); add(Bucket); Bucket.setFilled(true); Bucket.setFillColor(Color.GRAY); } public void addFruit(int a, int x, int y) { switch (a) { case 1: GRect Banana = new GRect(x, y, 10, 60); Banana.setColor(Color.YELLOW); Banana.setFilled(true); add(Banana); fruits.add(Banana); break; case 2: GOval lime = new GOval(x, y, 20, 20); lime.setColor(Color.GREEN); lime.setFilled(true); add(lime); fruits.add(lime); break; case 3: GOval Orange = new GOval(x, y, 30, 30); Orange.setColor(Color.ORANGE); Orange.setFilled(true); add(Orange); fruits.add(Orange); } } public void keyPressed(KeyEvent event) { int keyCode = event.getKeyCode(); switch (keyCode) { case KeyEvent.VK_LEFT: if (Bucket.getX() > 0) { Bucket.move(-BUCKET_SPEED, 0); } break; case KeyEvent.VK_RIGHT: if (Bucket.getX() < APPLET_WIDTH) { Bucket.move(BUCKET_SPEED, 0); } break; } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM