在Java汽车模拟中设置不同的计时器延迟

时间:2019-02-24 15:38:42

标签: java swing timer

首先请原谅我的英语不好:D。我正在进行汽车模拟项目,汽车收集了一些硬币并避开了一些障碍,我遇到了一些问题:
1)它会生成太多的硬币和积木,而我又如何过慢呢? 2)当汽车收集硬币时,它必须更改(扩展)其类别,例如,它变成具有自己的类别的卡车,因此我松开了移动汽车的方法
3)当汽车撞到障碍物时,是否必须更改颜色,是否可以在图像上设置滤镜,以便使用同一图像更改颜色?
有什么提示吗?我不是Java大师,所以不要恨我:D 这是主板类的代码

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class Board extends JPanel implements ActionListener {

    private Timer timer;
    private Car car;
    private List<Coin> coin;
    private List <Block> block;
    private boolean ingame;
    private final int ICRAFT_X = 200;
    private final int ICRAFT_Y = 580;
    private final int B_WIDTH = 400;
    private final int B_HEIGHT = 600;
    private final int DELAY = 50;

    public Board() {

        initBoard();
    }

    private void initBoard() {

        setFocusable(true);
        setBackground(Color.GRAY);
        ingame = true;
        coin= new ArrayList<>();
        block= new ArrayList<>();
        setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));

        car = new Car(ICRAFT_X, ICRAFT_Y);

        timer = new Timer(DELAY, this);      // don't know how to handle this :(

        timer.start();
    }

    public void initCoins() {

        //generate a coin all up the screen in random position

        Random randomno = new Random();
        int value = randomno.nextInt(385);
        coin.add(new Coin(value, 0));
    }

    public void initBlocks() {

        //generate blocks
        Random randomno = new Random();
        int value = randomno.nextInt(385);
        block.add(new Block(value, 0));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (ingame) {

            drawObjects(g);

        } else {

            drawGameOver(g);
        }

        Toolkit.getDefaultToolkit().sync();
    }

    private void drawObjects(Graphics g) {              //repaint all the       objects

        if (car.isVisible()) {
            g.drawImage(car.getImage(), car.getX(), car.getY(), this);
        }

        for (Coin coin : coin) {
            if (coin.isVisible()) {
                g.drawImage(coin.getImage(), coin.getX(), coin.getY(), this);
            }
        }
        for (Block block : block) {
            if (block.isVisible()) {
                g.drawImage(block.getImage(), block.getX(), block.getY(),
                        this);}

        }}

    private void drawGameOver(Graphics g) {

        String msg = "Game Over";
        Font small = new Font("Helvetica", Font.BOLD, 14);
        FontMetrics fm = getFontMetrics(small);

        g.setColor(Color.white);
        g.setFont(small);
        g.drawString(msg, (B_WIDTH - fm.stringWidth(msg)) / 2,
                B_HEIGHT / 2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        inGame();

        updateCar();
        updateBlocks();

        updateCoins();

        checkCollisions();
        repaint();

        initCoins();            // the 2 void i'd like that work "less" quick
        initBlocks();          //
    }

    private void inGame() {

        if (!ingame) {
            timer.stop();
        }
    }

    private void updateCar() {         //car looking for closest coin

        if (car.isVisible()) {
            double distmin=1000;
            double xcar=car.getX();
            double ycar=car.getY();
            int finalx=0;
            int finaly=0;
            for (Coin coin : coin) {
                double xcoin=coin.getX();
                double ycoin=coin.getY();
                double distanza=Math.sqrt(Math.pow(xcar- xcoin, 2) + Math.pow(ycar - ycoin, 2));
                if(distanza < distmin) {
                    distmin=distanza;
                    finalx = (int)xcoin;
                    finaly =(int)ycoin;
                }

            }
            if( finalx >car.getX()) {
                car.right();

            }
            else {
                car.left();
            }
            if(finaly<car.getY()) {

                car.up();
            }
            else {
                car.down();
            }
        }}


    private void updateCoins() {

        for (int i = 0; i < coin.size(); i++) {

            Coin a = coin.get(i);
            if (a.isVisible()) {
                a.down();                       // all coins move down
                Random randomno = new Random();
                boolean value = randomno.nextBoolean(); //all coins move radomly right or left
                if (value==true && a.x>10) {
                    a.left();
                }
                else if(a.y<385)
                {
                    a.right();
                }
            } else {
                coin.remove(i);
            }
        }
    }

    private void updateBlocks() {

        for (int i = 0; i < block.size(); i++) {

            Block b = block.get(i);

            if (b.isVisible()) {          //same as coins
                b.down();
                Random randomno = new Random();
                boolean value = randomno.nextBoolean();
                if (value==true) {
                    b.left();
                }
                else {
                    b.right();
                }
            } else {
                block.remove(i);
            }
        }
    }
    public void checkCollisions() {

        Rectangle r1 = car.getBounds();        //check collision with coins and blocks

        for (Coin coin : coin) {

            Rectangle r2 = coin.getBounds();

            if (r1.intersects(r2)) {

                coin.setVisible(false);

                // here i want to "change class" Examples: delete the car and replace it  witch a class Truck that extend car,
                //is it possible?how can i dont loose the updatecar method?
            }
        }

        for (Block block : block) {

            Rectangle r3 = block.getBounds();

            if (r1.intersects(r3)) {

                block.setVisible(false);
                //here i want to change the colour of the car image
            }
        }
    }
}

汽车课

public class Car extends Sprite {

    public Car(int x, int y) {
        super(x, y);
        loadImage("src/car.png");
        getImageDimensions();

    }
    public void up() {
        y -= 1;   
    }
    public void down() {    

        y+=1;}

    public void left() {
        x-=1;
    }
    public void right() {
        x+=1;
    }
}

0 个答案:

没有答案