Skip to content

Commit 3ffd40c

Browse files
committed
Started project
0 parents  commit 3ffd40c

21 files changed

+594
-0
lines changed

JavaRacer/Agent.java

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package JavaRacer;
2+
3+
import java.awt.Graphics2D;
4+
import java.awt.Rectangle;
5+
import java.awt.image.BufferedImage;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.util.Random;
9+
10+
import javax.imageio.ImageIO;
11+
12+
public class Agent {
13+
public int agentX,agentY; //x,y coordinates and rotation of the car in degrees. 0 representing right as in cartesian system(stored in velocity)
14+
public Rectangle solidArea = new Rectangle(); // aka hitbox of the car
15+
public double frictionCoefficient; //road friction: 1 in asphalt, 0.1 in grass checked by CollisionControl class
16+
public int points, laps = 0;
17+
public boolean onFinishLine, offFinishLine, isCollided = false; //flags for game logic
18+
public int[] instructions = new int[100];
19+
int nextActionTimer,instructionIndex;
20+
int screenMidX, screenMidY;
21+
public BufferedImage playerModel;
22+
GameWindow gameWindow;
23+
Velocity velocity;
24+
int counter;
25+
26+
27+
Random rand = new Random(0);
28+
29+
public Agent(GameWindow gw){
30+
this.gameWindow = gw;
31+
setDefault();
32+
}
33+
public void setDefault(){
34+
this.agentX=MapLoader.spawnX*gameWindow.tileSize;
35+
this.agentY=MapLoader.spawnY*gameWindow.tileSize;
36+
this.velocity = new Velocity();
37+
for(int i = 0; i<50; i++){
38+
instructions[i] = rand.nextInt(5);
39+
}
40+
switch (MapLoader.spawnDirection) {
41+
case 0:
42+
velocity.angle = 0;
43+
break;
44+
case 1:
45+
velocity.angle = 90;
46+
break;
47+
case 2:
48+
velocity.angle = 180;
49+
break;
50+
case 3:
51+
velocity.angle = 270;
52+
break;
53+
default:
54+
break;
55+
}
56+
this.solidArea.x = 30;
57+
this.solidArea.y = 5;
58+
this.solidArea.width = 25;
59+
this.solidArea.height = 25;
60+
61+
try {
62+
File tmp = new File("source/car.png");
63+
playerModel = ImageIO.read(tmp);
64+
this.screenMidX = this.gameWindow.WIDTH/2 - (playerModel.getWidth()/4);
65+
this.screenMidY = this.gameWindow.HEIGHT/2 -(playerModel.getHeight()/4);
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
public void update(){
71+
frictionCoefficient = gameWindow.collisionControl.checkCollision(this);
72+
checkLaps();
73+
calculatePoints();
74+
if(nextActionTimer%5 == 0 && instructionIndex<instructions.length){
75+
int rotationAngle = 5;
76+
double netVelocity = velocity.netVelocity();
77+
switch (instructions[instructionIndex]) {
78+
case 0: //accelerate
79+
velocity.accelerate(0.2*frictionCoefficient);
80+
break;
81+
case 1: //deaccelerate
82+
velocity.accelerate(-0.4);
83+
break;
84+
case 2: //turn left
85+
rotationAngle = 10;
86+
if (netVelocity > 0) {
87+
double scale = 1.0 / (1.0 + netVelocity * 0.1);
88+
rotationAngle *= scale;
89+
rotationAngle = Math.max(rotationAngle, 2);
90+
}
91+
velocity.rotate(rotationAngle);
92+
break;
93+
case 3: //turn right
94+
rotationAngle = -10;
95+
if (netVelocity > 0) {
96+
double scale = 1.0 / (1.0 + netVelocity * 0.1);
97+
rotationAngle *= scale;
98+
rotationAngle = Math.min(rotationAngle, -2);
99+
}
100+
velocity.rotate(rotationAngle);
101+
break;
102+
case 4: //Do nothing (keep velocity and angle)
103+
break;
104+
}
105+
instructionIndex++;
106+
}
107+
108+
while(frictionCoefficient<1&&velocity.netVelocity()>10*frictionCoefficient){
109+
velocity.accelerate(-0.5);
110+
}
111+
this.agentX += velocity.X;
112+
this.agentY -= velocity.Y;
113+
nextActionTimer++;
114+
}
115+
public void checkLaps(){
116+
if(onFinishLine){
117+
if(offFinishLine&&points>(laps*2500)+1000){
118+
laps++;
119+
points += 5000;
120+
}
121+
offFinishLine = false;
122+
}
123+
else{
124+
offFinishLine = true;
125+
}
126+
}
127+
public void calculatePoints(){
128+
if(frictionCoefficient<1){
129+
if(!isCollided){
130+
points-=1000;
131+
isCollided = true;
132+
}
133+
points -= 2*velocity.netVelocity()/frictionCoefficient;
134+
}
135+
else{
136+
isCollided = false;
137+
points += velocity.netVelocity();
138+
}
139+
if(points<0){
140+
points = 0;
141+
}
142+
}
143+
public void draw(Graphics2D graphics){
144+
graphics.rotate(Math.toRadians(-velocity.angle),agentX+playerModel.getWidth()/4,agentY+playerModel.getHeight()/4);
145+
graphics.drawImage(playerModel,agentX,agentY,playerModel.getWidth()/2,playerModel.getHeight()/2,null);
146+
}
147+
}

JavaRacer/CollisionControl.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package JavaRacer;
2+
3+
public class CollisionControl {
4+
GameWindow gameWindow;
5+
6+
public CollisionControl(GameWindow gw){
7+
this.gameWindow = gw;
8+
}
9+
public double checkCollision(Agent agent){
10+
int leftX = agent.agentX + agent.solidArea.x;
11+
int rightX = leftX + agent.solidArea.width;
12+
int upperY = agent.agentY + agent.solidArea.y;
13+
int lowerY = upperY + agent.solidArea.height;
14+
15+
int leftColNum = leftX / gameWindow.tileSize;
16+
int rightColNum = rightX / gameWindow.tileSize;
17+
int upperRowNum = upperY / gameWindow.tileSize;
18+
int lowerRowNum = lowerY / gameWindow.tileSize;
19+
20+
if(leftColNum>=0&&rightColNum<=gameWindow.collumn-1&&upperRowNum>=0&&lowerRowNum<=gameWindow.row-1){
21+
agent.onFinishLine = false;
22+
if(gameWindow.tileManager.tiles[gameWindow.tileManager.map[upperRowNum][leftColNum]].isFinishLine||gameWindow.tileManager.tiles[gameWindow.tileManager.map[lowerRowNum][rightColNum]].isFinishLine){
23+
agent.onFinishLine = true;
24+
}
25+
double friction = Math.min(gameWindow.tileManager.tiles[gameWindow.tileManager.map[upperRowNum][leftColNum]].friction, gameWindow.tileManager.tiles[gameWindow.tileManager.map[lowerRowNum][rightColNum]].friction);
26+
return friction;
27+
}
28+
return 0.1;
29+
}
30+
}

JavaRacer/GameWindow.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package JavaRacer;
2+
import java.awt.Color;
3+
import java.awt.Dimension;
4+
import java.awt.Graphics;
5+
import java.awt.Graphics2D;
6+
import javax.swing.JPanel;
7+
8+
public class GameWindow extends JPanel implements Runnable{
9+
public final int tileSize = 32;
10+
public final int collumn = 100;
11+
public final int row = 100;
12+
public final int WIDTH = tileSize*30;
13+
public final int HEIGHT = tileSize*20;
14+
private final int FPS = 60;
15+
Thread gameThread;
16+
KeyHandler keyHandle = new KeyHandler();
17+
TileManager tileManager = new TileManager(this);
18+
Agent[] agents = new Agent[10];
19+
CollisionControl collisionControl = new CollisionControl(this);
20+
21+
public GameWindow(){
22+
Color backgroundColor = new Color(34, 139, 34);
23+
for(int i = 0; i<agents.length;i++){
24+
agents[i] = new Agent(this);
25+
}
26+
27+
this.setBackground(backgroundColor);
28+
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
29+
this.setDoubleBuffered(true);
30+
this.addKeyListener(keyHandle);
31+
this.setFocusable(true);
32+
}
33+
34+
public void startGameLoop(){
35+
gameThread = new Thread(this);
36+
gameThread.start();
37+
}
38+
39+
@Override
40+
public void run() {
41+
while(gameThread != null){
42+
//Update
43+
update();
44+
45+
//Draw
46+
repaint();
47+
48+
//set FPS
49+
try {
50+
Thread.sleep(1000/FPS);
51+
} catch (InterruptedException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
}
56+
57+
public void update(){
58+
for (Agent agent : agents) {
59+
agent.update();
60+
}
61+
}
62+
public void paintComponent(Graphics graphics){
63+
super.paintComponent(graphics);
64+
Graphics2D graphic = (Graphics2D)graphics;
65+
66+
tileManager.draw(graphic,this.agents[0]);
67+
for (Agent agent : agents) {
68+
agent.draw(graphic);
69+
}
70+
71+
graphic.dispose();
72+
}
73+
}

JavaRacer/JavaRacer.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package JavaRacer;
2+
import javax.swing.JFrame;
3+
4+
public class JavaRacer {
5+
6+
public static void main(String[] args) {
7+
JFrame window = new JFrame();
8+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9+
window.setTitle("Java Racer");
10+
window.setResizable(false);
11+
12+
GameWindow gameWindow = new GameWindow();
13+
window.add(gameWindow);
14+
window.pack();
15+
window.setVisible(true);
16+
gameWindow.startGameLoop();
17+
}
18+
}

JavaRacer/KeyHandler.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package JavaRacer;
2+
3+
import java.awt.event.KeyEvent;
4+
import java.awt.event.KeyListener;
5+
6+
public class KeyHandler implements KeyListener{
7+
public boolean wPressed, sPressed, aPressed, dPressed, hPressed;
8+
@Override
9+
public void keyTyped(KeyEvent e) {}
10+
@Override
11+
public void keyPressed(KeyEvent e) {
12+
int code = e.getKeyCode();
13+
switch (code) {
14+
case KeyEvent.VK_W:
15+
wPressed = true;
16+
break;
17+
case KeyEvent.VK_A:
18+
aPressed = true;
19+
break;
20+
21+
case KeyEvent.VK_S:
22+
sPressed = true;
23+
break;
24+
25+
case KeyEvent.VK_D:
26+
dPressed = true;
27+
break;
28+
29+
case KeyEvent.VK_H:
30+
hPressed = true;
31+
break;
32+
33+
default:
34+
break;
35+
}
36+
}
37+
38+
@Override
39+
public void keyReleased(KeyEvent e) {
40+
int code = e.getKeyCode();
41+
switch (code) {
42+
case KeyEvent.VK_W:
43+
wPressed = false;
44+
break;
45+
case KeyEvent.VK_A:
46+
aPressed = false;
47+
break;
48+
49+
case KeyEvent.VK_S:
50+
sPressed = false;
51+
break;
52+
53+
case KeyEvent.VK_D:
54+
dPressed = false;
55+
break;
56+
57+
case KeyEvent.VK_H:
58+
hPressed = false;
59+
break;
60+
61+
default:
62+
break;
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)