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

时间:2015-11-20 01:22:37

标签: java animation

这是交易,基本上我必须有一个代码,有一个桶可以捕捉落下的水果,每次它捕获它你得到一个新的水果落点。

所以我知道如何让水桶移动,以及如果水果到达水底后如何让它再次移动。但是,我不知道如何让它真正下降。到目前为止,我有一个开关,但不知道如何处理它。我在随机的地方突然出现了水果,这是一个开始。无论如何,这是我的代码。所有帮助赞赏。再次,当其中一个到达底部时,我需要随机掉落水果。

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;
		}
	}
}

在我的代码中你可以看到一段时间(真实)我只是假设这是我写的地方。但是,我实际上会在那里失去一点点。

1 个答案:

答案 0 :(得分:2)

  • 你需要保留一份对所有成果的引用列表,以便以后可以操作它们
  • 游戏循环的每次迭代,将每个果实移动
  • 你可能想要实现一些计时机制,以便结果 速度不依赖于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;
        }
    }
    }