移动具有随机数的 JPanel 图像



我正在为我的班级制作赛车游戏,我只用java编程了几个星期,所以我想知道如何使用随机数让我的汽车以不同的速度移动。

package racing.game;
import java.awt.Color;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RacingGame {
    public static void main(String[] args) {
        int temp ;
        int dist1 = 0;
        int dist2 = 0;
        int dist3 = 0;
        int dist4 = 0;
        int i = 0;
        JFrame freRace = new JFrame();
        freRace.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        freRace.setSize(1900, 1080);
        freRace.setLayout(null);
        //Creating the vehicles
        JLabel labelCar1 = new JLabel();
        labelCar1.setIcon(new ImageIcon("images/car1.jpg"));
        labelCar1.setBounds((0), (50), labelCar1.getPreferredSize().width, labelCar1.getPreferredSize().height);
        freRace.add(labelCar1);
        JLabel labelCar2 = new JLabel();
        labelCar2.setIcon(new ImageIcon("images/car2.jpg"));
        labelCar2.setBounds((0), (310), labelCar2.getPreferredSize().width, labelCar2.getPreferredSize().height);
        freRace.add(labelCar2);
        JLabel labelCar3 = new JLabel();
        labelCar3.setIcon(new ImageIcon("images/car3.jpg"));
        labelCar3.setBounds((0), (580), labelCar3.getPreferredSize().width, labelCar3.getPreferredSize().height);
        freRace.add(labelCar3);
        JLabel labelCar4 = new JLabel();
        labelCar4.setIcon(new ImageIcon("images/car4.jpg"));
        labelCar4.setBounds((0), (900), labelCar4.getPreferredSize().width, labelCar4.getPreferredSize().height);
        freRace.add(labelCar4);
        JLabel labelBackground = new JLabel();
        labelBackground.setIcon(new ImageIcon("images/race track.jpg"));
        labelBackground.setBounds((0), (0), labelBackground.getPreferredSize().width, labelBackground.getPreferredSize().height);
        freRace.add(labelBackground);
        freRace.setBackground(Color.WHITE);
        // freRace.setVisible(true);
        while (dist1 <= 1890) {
            Random rn = new Random();  
            dist1 = dist1 + rn.nextInt(5);
            dist2 = dist2 + rn.nextInt(4);
            dist3 = dist3 + rn.nextInt(3);
            dist4 = dist4 + rn.nextInt(2);
            labelCar1.setBounds((dist1), (50), labelCar1.getPreferredSize().width, labelCar1.getPreferredSize().height);
            labelCar2.setBounds((dist2), (310), labelCar2.getPreferredSize().width, labelCar2.getPreferredSize().height);
            labelCar3.setBounds((dist3), (580), labelCar3.getPreferredSize().width, labelCar3.getPreferredSize().height);
            labelCar4.setBounds((dist4), (900), labelCar4.getPreferredSize().width, labelCar4.getPreferredSize().height);
            freRace.setVisible(true);  
        } 
    }
}

该代码旨在让汽车以随机不同的速度在屏幕上比赛,但我不知道为此选择哪个循环以及如何让他的汽车速度随机移动,所以我完全猜测

我想知道如何使用特定的随机数让我的汽车以不同的速度行驶

您需要为每辆车提供一个speed属性。而不是为每辆车创建速度值列表。如果属性在 Car 对象本身中是自包含的,则会更容易:

class Car{
    private double speed;
}

要移动汽车,您可以添加移动方法:

class Car{
    private int x;
    private int y;
    //private JLabel image;    //Better to use BufferedImage
    private double speed;
    //constructor not shown
    public void moveForward(){
        x += speed;
    }
    public void moveBackward(){
        x -= speed;
    }
}

然后你可以有一个数组或一个汽车列表:

ArrayList<Car> cars = new ArrayList<Car>();

要随机化汽车的速度,只需为汽车分配一个随机值:

//Example
Random rnd = new Random();
car.setSpeed(rnd.nextInt(5)+1);  //random speed of 1 to 5

要移动所有汽车:

//Example
for(Car c : cars)
    c.moveForward();

如果时间对您有利,实际上值得探索如何在不使用JLabels的情况下实现这一点,而是使用自定义图纸实现它。

如果你在 main 方法中循环,你只会看到移动的最后一个结果,你可以做这样的事情:

 freRace.setVisible(true);
 new Thread(new Runnable() {
     public void run() {
        Random rn = new Random();  
        int s1 = rn.nextInt(5);
        int s2 = rn.nextInt(5);
        int s3 = rn.nextInt(5);
        int s4 = rn.nextInt(5);
       while (dist1 <= 1890) {
        dist1 = dist1 + s1;
        dist2 = dist2 + s2;
        dist3 = dist3 + s3;
        dist4 = dist4 + s4;
        labelCar1.setBounds((dist1), (50), labelCar1.getPreferredSize().width, labelCar1.getPreferredSize().height);
        labelCar2.setBounds((dist2), (310), labelCar2.getPreferredSize().width, labelCar2.getPreferredSize().height);
        labelCar3.setBounds((dist3), (580), labelCar3.getPreferredSize().width, labelCar3.getPreferredSize().height);
        labelCar4.setBounds((dist4), (900), labelCar4.getPreferredSize().width, labelCar4.getPreferredSize().height);
        freRace.invalidate();
        try { Thread.sleep(100); } catch (Exception e) {}
       } 
     }
 }).start();

请注意,我还添加了一个计时器,以便它可以不断移动。

此外,还可以学习如何在 JPanel 的 PaintComponent 方法中执行此操作,以便您可以从双缓冲中受益。

最新更新