Java 2D赛车游戏中涉及道路的图形

时间:2019-05-15 22:41:25

标签: java graphics

我需要为我在学校的计算机科学课制作一个游戏项目,并且在游戏上取得了很大的进步。我需要帮助的是如何“绕行”道路并让屏幕随车移动。

但是我不知道从何处开始或如何在不进行重大更改的情况下将其放入代码中。

这是项目的第一个文件:

 package gameproject;

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class CarMovement {

private int dx;
private int dy;
private int x = 635;
private int y = 550;
private int w;
private int h;
private Image moveimage;

public CarMovement() {

    loadImage();
}

private void loadImage() {

    ImageIcon q = new ImageIcon("racecar.png");
    moveimage = q.getImage(); 

    w = moveimage.getWidth(null);
    h = moveimage.getHeight(null);
}

public void move() {

    x += dx;
    y += dy;
}

public int getX() {

    return x;
}

public int getY() {

    return y;
}

public int getWidth() {

    return w;
}

public int getHeight() {

    return h;
}    

public Image getImage() {

    return moveimage;
}

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_A) {
        dx = -10;
    }

    if (key == KeyEvent.VK_D) {
        dx = 10;
    }

    if (key == KeyEvent.VK_W) {
        dy = -10;
    }

    if (key == KeyEvent.VK_S) {
        dy = 10;
    }
}

public void keyReleased(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_A) {
        dx = 0;
    }

    if (key == KeyEvent.VK_D) {
        dx = 0;
    }

    if (key == KeyEvent.VK_W) {
        dy = 0;
    }

    if (key == KeyEvent.VK_S) {
        dy = 0;
    }
}
}

第二个:

 package gameproject;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;

public class CarMovement2 extends JPanel implements ActionListener {

private Timer timer;
private CarMovement racecar;
private final int DELAY = 10;

public CarMovement2() {

    initBoard();
}

private void initBoard() {

    addKeyListener(new TAdapter());
    setBackground(Color.black);
setFocusable(true);

    racecar = new CarMovement();

    timer = new Timer(DELAY, this);
    timer.start();
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(new Color(0, 204, 0));
    g.fillRect(0, 0, 400, 1100);
    g.fillRect(1525, 0, 400, 1100);
    g.setColor(new Color(102, 102, 102));
    g.fillRect(400, 0, 1125, 1100);
    g.setColor(new Color(255, 255, 255));
    g.fillRect(940, 25, 25, 100);
    g.fillRect(940, 325, 25, 100);
    g.fillRect(940, 475, 25, 100);
    g.fillRect(940, 625, 25, 100);
    g.fillRect(940, 775, 25, 100);
    g.fillRect(940, 925, 25, 100);
    g.setColor(new Color(255, 255, 255));
    g.fillRect(400, 175, 1125, 100);
    g.setColor(new Color(0, 0, 0));
    g.fillRect(400, 225, 50, 50);
    g.fillRect(450, 175, 50, 50);
    g.fillRect(500, 225, 50, 50);
    g.fillRect(550, 175, 50, 50);
    g.fillRect(600, 225, 50, 50);
    g.fillRect(650, 175, 50, 50);
    g.fillRect(700, 225, 50, 50);
    g.fillRect(750, 175, 50, 50);
    g.fillRect(800, 225, 50, 50);
    g.fillRect(850, 175, 50, 50);
    g.fillRect(900, 225, 50, 50);
    g.fillRect(950, 175, 50, 50);
    g.fillRect(1000, 225, 50, 50);
    g.fillRect(1050, 175, 50, 50);
    g.fillRect(1100, 225, 50, 50);
    g.fillRect(1150, 175, 50, 50);
    g.fillRect(1200, 225, 50, 50);
    g.fillRect(1250, 175, 50, 50);
    g.fillRect(1300, 225, 50, 50);
    g.fillRect(1350, 175, 50, 50);
    g.fillRect(1400, 225, 50, 50);
    g.fillRect(1450, 175, 50, 50);
    g.fillRect(1500, 225, 25, 50);
    g.setColor(new Color(255, 255, 255));
    g.fillRect(380, 0, 20, 1100);
    g.fillRect(1525, 0, 20, 1100);
    doDrawing(g);

    Toolkit.getDefaultToolkit().sync();
}

private void doDrawing(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    g2d.drawImage(racecar.getImage(), racecar.getX(), 
        racecar.getY(), this);
}

@Override
public void actionPerformed(ActionEvent e) {

    step();
}

private void step() {       
    Rectangle old = new Rectangle(racecar.getX(), racecar.getY(), racecar.getWidth(), racecar.getHeight());

    racecar.move();

    Rectangle now = new Rectangle(racecar.getX(), racecar.getY(), racecar.getWidth(), racecar.getHeight());

    repaint(old);
    repaint(now);
}

private class TAdapter extends KeyAdapter {

    @Override
    public void keyReleased(KeyEvent e) {
        racecar.keyReleased(e);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        racecar.keyPressed(e);
    }
}
}

第三个:

 package gameproject;


import java.awt.EventQueue;
import javax.swing.JFrame;
public final class CarMovement3 extends JFrame {

 public CarMovement3() {

    InitUI();
}

private void InitUI() {

    add(new CarMovement2());

    setTitle("Top Speed Triumph");
    setSize(1900, 1100);
    setResizable(false);

    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

    EventQueue.invokeLater(() -> {
        CarMovement3 ex = new CarMovement3();
        ex.setVisible(true);
    });
}


}

0 个答案:

没有答案