3.car Game
3.car Game
01 INTRODUCTION
03 FLOW CHART
04 SOURCE CODE
05 OUTPUT
07 BIBLIOGRAPHY
1|Page
PROJECT ON CAR GAME
INTRODUCTION
2|Page
Rocket League have cars in a large soccer field and
players use their car to drive hitting a ball to goal.
OBJECTIVE OF THE PROJECT
1. When you run the game, you will see your car
moving on the road. You can select the start button to
play the game.
3|Page
FLOW CHART
4|Page
SOURCE CODE
package hk.car;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.net.URL;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
int lx[],ly[]; //integer arrays used to store the x and y values of the oncoming vehicles
int score; //intger variable used to store the current score of the player
int highScore; //integer variable used to store the high score of the player
int speedOpponent[]; //integer array used to store the spped value of each opponent
vehicle in the game
boolean isFinished; //boolean that will be used the end the game when a colision occurs
boolean isUp, isDown, isRight, isLeft; //boolean values that show when a user clicks the
corresponding arrow key
5|Page
public Game(){
crx = cry = -999; //initialing setting the location of the crossing to (-999,-999)
//Listener to get input from user when a key is pressed and released
addKeyListener(new KeyListener() {
});
car_x = car_y = 300; //initialling setting the user's car location to (300,300)
isUp = isDown = isLeft = isRight = false; //initial arrow key values set to false,
meaning user has not pressed any arrow keys
speedX = speedY = 0; //movement of the car in the x and y direction initially set to 0
(starting position)
lx = new int[20]; //array to be used to store the x position of all enemy cars
ly = new int[20]; //array to be used to store the y position of all enemy cars
speedOpponent = new int[20]; //integer array used to store the spped value of each
opponent vehicle in the game
6|Page
isFinished = false; //when false, game is running, when true, game has ended
score = highScore = 0; //initialling setting the current score and the highscore to zero
//function that paints all graphic images to the screen at specified locations
super.paint(g);
obj.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
try{
if(cry >= -499 && crx >= -499) //if a road crossing has passed the window view
obj.drawImage(getToolkit().getImage("images/cross_road.png"),crx,cry,this);
//draw another road crossing on window
obj.drawImage(getToolkit().getImage("images/car_self.png"),car_x,car_y,this);
//draw car on window
obj.drawImage(getToolkit().getImage("images/boom.png"),car_x-30,car_y-
30,this); //draw explosion image on window at collision to indicate the collision has occured
if(this.nOpponent > 0){ //if there is more than one opponent car in the game
7|Page
for(int i=0;i<this.nOpponent;i++){ //for every opponent car
catch(Exception e){
System.out.println(e);
//function that moves the road scene across the window to make it seem like the car is
driving
if(crx == -999 && cry == -999){ //if the road crossing has passed by
cry = 0;
else{ //otherwise
if(crx == -499 && cry == 0){ //if the opponent car passes the user without colliding
8|Page
crx = cry = -999; //reset opponent car position to beginning to start over
//case analysis to restrict car from going outside the left side of the screen
if(car_x < 0) //if the car has reached or gone under its min x axis value
//case analysis to restrict car from going outside the right side of the screen
if(car_x+93 >= 500) //if the car has reached or gone over its max x axis value
//case analysis to restrict car from going outside the right side of the road
if(car_y <= 124) //if the car has reached the the right side of the road or is trying to
go further
//case analysis to restrict car from going outside the left side of the road
if(car_y >= 364-50) //if the car has reached the the left side of the road or is trying to
go further
9|Page
this.lx[i] -= speedOpponent[i]; //move across the screen based on already calculated
speed values
for(int i=0;i<nOpponent;i++){
index[i] = 1;
int c = 0;
for(int i=0;i<nOpponent;i++){
if(index[i] == 1){
imageLoc[c] = imageLoc[i];
lx[c] = lx[i];
ly[c] = ly[i];
speedOpponent[c] = speedOpponent[i];
c++;
if(score > highScore) //if the current score is higher than the high score
10 | P a g e
highScore = score; //update high score to the current score
nOpponent = c;
int diff = 0; //difference between users car and opponents car initially set to zero
diff = car_y - ly[i]; //diff is the distance between the user's car and the opponent car
if((ly[i] >= car_y && ly[i] <= car_y+46) || (ly[i]+46 >= car_y && ly[i]+46 <=
car_y+46)){ //if the cars collide vertically
if(car_x+87 >= lx[i] && !(car_x >= lx[i]+87)){ //and if the cars collide
horizontally
//function that will display message after user has lost the game
void finish(){
String str = ""; //create empty string that will be used for a congratulations method
isFinished = true; //indicates that game has finished to the rest of the program
this.repaint(); //tells the window manager that the component has to be redrawn
11 | P a g e
if(score == highScore && score != 0) //if the user scores a new high score, or the same
high score
//function that handles input by user to move the user's car up, left, down and right
isUp = true;
isDown = true;
isRight = true;
isLeft = true;
12 | P a g e
speedY = -1; //moves car to the left
//function that handles user input when the car is supposed to be stopped
isUp = false;
isDown = false;
else if(e.getKeyCode() == KeyEvent.VK_LEFT){ //if user clicks on the left arrow key
isLeft = false;
isRight = false;
13 | P a g e
//main method where the java application begins processing
frame.setVisible(true); //allows the JFrame and its children to displayed on the screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int count = 1, c = 1;
while(true){
try{
catch(Exception e){
System.out.println(e);
c++;
c = 1;
14 | P a g e
if(game.nOpponent < 4 && count % 200 == 0){ //if there is less than 4 cars and
count timer reaches 200
game.imageLoc[game.nOpponent] = "images/car_left_"+((int)
((Math.random()*100)%3)+1)+".png"; //assign images to the opponent cars
else{ //otherwise
game.speedOpponent[game.nOpponent] = (int)(Math.random()*100)%2 + 2;
//sets the speed of the new opponent car to a random number that is the remainder of a
number between 0 and 100, plus 2
15 | P a g e
}
OUTPUT
16 | P a g e
17 | P a g e
18 | P a g e
19 | P a g e
HARDWARE AND SOFTWARE REQUIREMENTS
HARDWARE REQUIRED:
MEMORY: 4 Gb
FILE SIZE: 2 MB
OS : WINDOWS 7 OR ABOVE
SOFTWARE REQUIRED:
https://github.com/return007/car-racing-game/blob/master/Game.java
https://en.wikipedia.org/wiki/Racing_game
https://hackr.io/blog/best-java-books-for-beginners-and-advanced-
programmers
https://www.simplilearn.com/best-java-books-to-read-article
20 | P a g e