Graphics involving the road in a 2d racing game in java

34
May 16, 2019, at 7:50 PM

I need to make a game project for my Computer Science class in school, and I have made a good amount of progress on the game. What I need help with is how I can "loop" the road and allow the screen to move with the car.

However I have no idea as to where to start or how to put this feature in my code without dramatically changing it.

Here is the first file of the project:

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

The second:

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

The third:

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

}
READ ALSO
Compare 2 different RDF databases for Measures of Similarity

Compare 2 different RDF databases for Measures of Similarity

I have two RDF databases inttl format

20
android recyclerview not loading on first launch

android recyclerview not loading on first launch

i am using firestore in androidin my fragment i have a recyclerview which fetch data from firestore but on first launch of app the recyclerview is empty but when i move to another fragment and come back , it get loaded with data

44
How to parse HH:mm:ss without hours?

How to parse HH:mm:ss without hours?

I have a string with format like: "2h 33m 50s"If hours reach zero this string changes to: "33m 50s"

31
Is JDK 8’s new strategy for dealing with HashMap collisions(tree instead of list) need only for those who is not able to write a good hashcode()?

Is JDK 8’s new strategy for dealing with HashMap collisions(tree instead of list) need only for those who is not able to write a good hashcode()?

In Java 8, HashMap replaces linked list with a binary tree when the number of elements in a bucket reaches certain threshold

45