Car Racing in Java
Car Racing in Java
Submitted by,
MADHAV SURESH
1
INDEX
S.No CONTENTS PAGE
No.
1 ABSTRACT 4
2 INTRODUCTION 5
2
5 CODING 21-45
9 CONCLUSION 53
10 BIBLIOGRAPHY 54
3
1. ABSTRACT
Racing are always been one of the most popular type of project that
attracts students we have selected car race has our project as car race
are interesting and drag us to learn more about the subject. CAR RACE
is the game Played between the one or more cars. Racing is a
competition of a speed, against an objective criterion, usually a clock or
to a specific point. The competition in car race tries to complete a given
task in the shortest amount of time. The game is not to defeat a
The human player but to provide the player with a challenging and
enjoyable experience. This game is controlled in a two-dimension
environment. Our project has following tasks entry mode, car
movements, score, lives of players, action after collusions/dashing a car
and side walls.
4
2. INTRODUCTION
5
3. STRUCTURE ANALYSIS AND DESIGN
6
3.3 FEASIBILITY STUDY
A feasibility study is an evaluation of a proposal designed
to determine the difficulty in carrying out a designated task.
Generally, a feasibility study precedes technical development
and project implementation. In other words, a feasibility study
is an evaluation or analysis of the potential impact of a proposed
project .
Economic feasibility
7
system and compare them with costs. If benefits outweigh costs,
then the decision is made to design and implement the system. An
entrepreneur must accurately weigh the cost versus benefits
before taking an action.
1. Development costs
Legal feasibility
8
Operational feasibility
Schedule feasibility
9
3.4 SYSTEM REQUIREMENT
Hardware Specification:
▪ Processor
▪ Memory
Software Specifications:
10
• Operating system: windows NT/98/XP/2000
3.5 JAVA
In Java, the code is first compiled and then translated into a binary
form called Java Byte Code. Finally, the JBC gets compiled and
interpreted into the native language needed to run in the target operating
environment. This contrasting behaviour is a significant benefit for
developers as it means they can write code once and run it anywhere.
11
Java is still very popular and has expanded its reach into several
industries. Today Java issued for programming complex technologies
from mobile applications to AI programming and more.
Features of Java:
Simple
Secure
Portable
Object-Oriented
Robust
Multi-threaded
Architecture-Neutral
Interpreted
High performance
Distributed
Dynamic
12
3.5.1 Java Development Kit (JDK):
Java Swing:
Methods used:
13
✓ Add Action Listener Listener(ActionListener d) : add action
Listener for specified component
✓ Set Background(Color c): sets the background color of the
specified container
✓ Set Size(int a, int b): sets the size of container to specified
dimensions.
✓ setText(Strings): sets the text of the label to s.
✓ getText(): returns the text of the label.
Java AWT:
• Physical DFD– This type of DFD depicts how the system will be
implemented or how the current system operates. The processes
represent the programs, program modules, and manual
procedures. The data stores represent the physical files and
databases, manual files.
15
3.6.2 Basic Notations:
Data Flow
Process
Source
Data store
Decision
Input/output
16
DATA FLOW DIAGRAM
Use Case
17
3.7 DECISION TREE
18
4. SYSTEM DESIGN
19
5. CODING
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.Timer;
// this class is used to build the graphics and the game logic
20
{
21
private int
cypos1=random.nextInt(5),cypos2=random.nextInt(5),cypos3=rando
m.nextInt(5); // y position of the car
int
y1pos=carypos[cypos1],y2pos=carypos[cypos2],y3pos=carypos[cypo
s3]; // y position of the car
22
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
close the frame when the close button is clicked
23
// draw the road lines
if(roadmove==0)
g.setColor(Color.white);
else if(roadmove==1)
g.setColor(Color.white);
try {
tree1=new
ImageIcon(ImageIO.read(getClass().getResource("tree.png"))); // load
the tree image
} catch (IOException e) {
e.printStackTrace();
try {
tree2=new
ImageIcon(ImageIO.read(getClass().getResource("tree.png"))); // load
the tree image
} catch (IOException e) {
e.printStackTrace();
try {
25
tree3=new
ImageIcon(ImageIO.read(getClass().getResource("tree.png"))); // load
the tree image
} catch (IOException e) {
e.printStackTrace();
tree1.paintIcon(this,g,600,tree4ypos);
tree4ypos+=50;
26
tree3.paintIcon(this, g,600,tree5ypos);
tree5ypos+=50;
tree2.paintIcon(this, g,600,tree6ypos);
tree6ypos+=50;
if(tree1ypos>700)
{ // if the tree goes out of the screen then reset the tree
if(tree2ypos>700)
num1=random.nextInt(500);
tree2ypos=-num1;
if(tree3ypos>700)
num1=random.nextInt(500);
tree3ypos=-num1;
27
}
if(tree4ypos>700)
{ // if the tree goes out of the screen then reset the tree
num1=random.nextInt(500);
tree4ypos=-num1;
if(tree5ypos>700)
num1=random.nextInt(500);
tree5ypos=-num1;
if(tree6ypos>700)
{ // if the tree goes out of the screen then reset the tree
num1=random.nextInt(500);
tree6ypos=-num1;
try {
28
car=new
ImageIcon(ImageIO.read(getClass().getResource("gamecar3.png")));
// load the car image
} catch (IOException e) {
e.printStackTrace();
// car=new ImageIcon("gamecar1.png");
ypos-=40;
if(ypos<500)
ypos=500;
try {
29
car1=new
ImageIcon(ImageIO.read(getClass().getResource("gamecar1.png")));
} catch (IOException e) {
e.printStackTrace();
try {
car2=new
ImageIcon(ImageIO.read(getClass().getResource("gamecar2.png")));
} catch (IOException e) {
e.printStackTrace();
try {
car3=new
ImageIcon(ImageIO.read(getClass().getResource("gamecar4.png")));
} catch (IOException e) {
e.printStackTrace();
30
car1.paintIcon(this, g, carxpos[cxpos1], y1pos); // draw the
opponent car image on the screen
y2pos+=50;
y3pos+=50;
if(y1pos>700)
{ // if the opponent car goes out of the screen then reset the
opponent car
cypos1=random.nextInt(5);
if(y2pos>700)
{ // if the opponent car goes out of the screen then reset the
opponent car
cxpos2++;
31
if(cxpos2>4)
cxpos2=0;
cxpos2=random.nextInt(5);
cypos2=random.nextInt(5);
y2pos=carypos[cypos2];
if(y3pos>700)
cxpos3++;
if(cxpos3>4)
cxpos3=0;
cxpos3=random.nextInt(5);
cypos3=random.nextInt(5);
y3pos=carypos[cypos3];
32
}
cxpos1-=1;
if(cxpos1<0)
cxpos1+=2;
cxpos3-=1;
if(cxpos3<0)
cxpos3+=2;
{
33
cxpos2-=1;
if(cxpos2<0)
cxpos2+=2;
cxpos3++;
cxpos2++;
cxpos3++;
cxpos2++;
{
34
cxpos1++;
cxpos2++;
// if the opponent car hits the player car then reset the
game
gameover=true;
gameover=true;
35
gameover=true;
gameover=true;
gameover=true;
gameover=true;
//score
g.setColor(Color.red);
36
g.fillRect(120,35,220,50);
g.setColor(Color.black);
g.setColor(Color.red);
g.fillRect(385,35,180,50);
g.setColor(Color.black);
g.setColor(Color.white);
if(speed>140)
speed=240-delay;
if(score%50==0)
if(delay<60)
//delay
try
TimeUnit.MILLISECONDS.sleep(delay); // delay
the game
catch (InterruptedException e) {
e.printStackTrace();
gameover=true;
38
}
gameover=true;
gameover=true;
if(gameover)
g.setColor(Color.gray);
g.setColor(Color.DARK_GRAY);
g.setColor(Color.red);
39
g.drawString("Game Over !",210, 270);
g.setColor(Color.white);
if(!paint)
repaint();
paint=true;
else
repaint();
}
40
@Override
{ // if the left key is pressed then move the car to the left
xpos-=100;
if(xpos<100)
if(e.getKeyCode()==KeyEvent.VK_RIGHT&&!gameover)
xpos+=100;
if(xpos>500)
41
xpos=500; // if the car is at the right most
position then don't move it
if(e.getKeyCode()==KeyEvent.VK_ENTER &&
gameover)
gameover=false;
paint=false;
cxpos1=0;
cxpos2=2;
cxpos3=4;
cypos2=random.nextInt(5);
cypos3=random.nextInt(5);
y2pos=carypos[cypos2];
y3pos=carypos[cypos3];
42
speed=90; // set the speed to 90
@Override
@Override
if(e.getKeyChar()=='a'&&!gameover)
43
if(e.getKeyChar()=='s'&&!gameover)
repaint();
@Override
44
6. SCREEN SHOTS
45
46
7. SYSTEM TESTING AND IMPEMENTATION
47
Testing is done during the development (coding phase) of an
application by the developers.
48
7.2 SYSTEM IMPLEMENTATION
49
This is to review the performance of the system and to
evaluate against established standard or criteria. A study is conducted
for measuring the performance of the system against pre-defined
requirements. This study results a post- implementation review that
determines how well the system continues to meet the performance
specification.
8. SYSTEM MAINTENANCE
50
Maintenance Types:
51
9. CONCLUSION
52
10. BIBLIOGRAPHY
https://www.coursehero.com/file/158595623/car-racing-gamedocx/
https://www.hindawi.com/journals/ijcgt/2015/839721/
https://www.coursehero.com/file/84928377/Synopsis-For-Car-
Racing-Gamedocx/
https://copyassignment.com/simple-car-race-game-in-java/
https://www.slideshare.net/ravijotsingh4/report-on-car-racing-game-
for-android
https://code-projects.org/car-racing-game-in-java-with-source-code/
53