Java Report
Java Report
INDEX
2. Project Overview 3
4. Features 3
5. Program 4
6. Output 15
7. Conclusion 16
8. Reference 17
1
CAR GAME
INTRODUCTION
When it comes to building gaming applications, we always think of
racing games as we loved playing these types of games in our childhood. So
today in this article we will develop a Car Race Game in Java with Swing.
This game needs no explanation, but still has a bit of context for the
game. The player’s car needs to race with the opponent’s cars without hitting
any one of them and maintaining a high speed.
2
CAR GAME
Project Overview :
Features :
The player will be able to move the car left and right by pressing the left
and right key.
The Score and Speed of the car are updated continuously as the car moves
ahead.
If the car hits another car then the game is over and the player can restart
the game by pressing Enter Key.
3
CAR GAME
PROGRAM
CLASS :
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.util.Random;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.Timer;
4
CAR GAME
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillRect(0, 0, 700, 700);
g.setColor(Color.gray);
g.fillRect(90,0,10,700);
g.fillRect(600, 0, 10, 700);
g.setColor(Color.black);
g.fillRect(100, 0, 500, 700);
if(roadmove==0)
{
for(int i=0; i<=700; i+=100)
{
g.setColor(Color.white);
g.fillRect(350, i,10, 70);
}
roadmove=1;
}
else if(roadmove==1)
{
for(int i=50; i<=700; i+=100)
{
g.setColor(Color.white);
g.fillRect(350, i,10, 70);
}
5
CAR GAME
roadmove=0;
}
///for road
tree1.paintIcon(this, g, 0, tree1ypos);
num1=random.nextInt(500);
tree1ypos+=50;
if(tree1ypos>700)
{
num1=random.nextInt(500);
tree1ypos=-num1;
}
if(tree2ypos>700)
{
num1=random.nextInt(500);
tree2ypos=-num1;
}
if(tree3ypos>700)
{
num1=random.nextInt(500);
6
CAR GAME
tree3ypos=-num1;
}
if(tree4ypos>700)
{
num1=random.nextInt(500);
tree4ypos=-num1;
}
if(tree5ypos>700)
{
num1=random.nextInt(500);
tree5ypos=-num1;
}
if(tree6ypos>700)
{
num1=random.nextInt(500);
tree6ypos=-num1;
}
car.paintIcon(this,g,xpos,ypos);
ypos-=40;
if(ypos<500)
{
ypos=500;
}
// opposite car
car1=new ImageIcon("F:\\4th sem microprojects\\JPR
Microproject\\assets\\gamecar2.png");
car2=new ImageIcon("F:\\4th sem microprojects\\JPR
Microproject\\assets\\gamecar3.png");
car3=new ImageIcon("F:\\4th sem microprojects\\JPR
Microproject\\assets\\gamecar4.png");
7
CAR GAME
y1pos+=50;
y2pos+=50;
y3pos+=50;
if(y1pos>700)
{
// cxpos1++;
// if(cxpos1>4)
// {
// cxpos1=0;
// }
cxpos1=random.nextInt(5);
cypos1=random.nextInt(5);
y1pos=carypos[cypos1];
}
if(y2pos>700)
{
cxpos2++;
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);
8
CAR GAME
cypos3=random.nextInt(5);
y3pos=carypos[cypos3];
}
cxpos1-=1;
if(cxpos1<0)
{
cxpos1+=2;
}
}
if(cxpos1==cxpos3&& cypos1>-100 && cypos3>-100)
{
cxpos3-=1;
if(cxpos3<0)
{
cxpos3+=2;
}
}
if(cxpos2==cxpos3&& cypos3>-100 && cypos2>-100)
{
cxpos2-=1;
if(cxpos2<0)
{
cxpos2+=2;
}
}
if(cxpos1<2 && cxpos2<2 && cxpos3<2)
{
if(cxpos1==0 && cxpos2==0 && cxpos3==1)
{
cxpos3++;
cxpos2++;
}
else if(cxpos1==0 && cxpos2==1 && cxpos3==0)
{
cxpos3++;
9
CAR GAME
cxpos2++;
}
else if(cxpos1==1 && cxpos2==0 && cxpos3==0)
{
cxpos1++;
cxpos2++;
}
}
10
CAR GAME
if(speed>140)
{
speed=240-delay;
}
if(score%50==0)
{
delay-=10;
if(delay<60)
{
delay=60;
}
}
//delay
try
{
TimeUnit.MILLISECONDS.sleep(delay);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(y1pos<ypos && y1pos+175>ypos &&
carxpos[cxpos1]==xpos)
{
gameover=true;
}
if(y2pos<ypos && y2pos+175>ypos &&
11
CAR GAME
carxpos[cxpos2]==xpos)
{
gameover=true;
}
if(y3pos<ypos && y3pos+175>ypos &&
carxpos[cxpos3]==xpos)
{
gameover=true;
}
if(gameover)
{
g.setColor(Color.gray);
g.fillRect(120, 210, 460, 200);
g.setColor(Color.DARK_GRAY);
g.fillRect(130, 220, 440, 180);
g.setFont(new Font("Serif",Font.BOLD,50));
g.setColor(Color.yellow);
g.drawString("Game Over !",210, 270);
g.setColor(Color.white);
g.setFont(new Font("Arial",Font.BOLD,30));
g.drawString("Press Enter to Restart", 190, 340);
if(!paint)
{
repaint();
paint=true;
}
}
else
{
repaint();
}
}
12
CAR GAME
xpos-=100;
if(xpos<100)
{
xpos=100;
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT&&!gameover)
{
xpos+=100;
if(xpos>500)
{
xpos=500;
}
}
}
@Override
13
CAR GAME
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyChar()=='a'&&!gameover)
{
xpos-=100;
}
if(e.getKeyChar()=='s'&&!gameover)
{
xpos+=100;
}
repaint();
}
@Override
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
}
}
14
CAR GAME
OUTPUT
15
CAR GAME
CONCLUSION
Hope you enjoyed building the Car Race Game in Java as much as I
enjoyed creating it for you!! We learned how to deal with collisions between
different objects and re-rendering the object as they go out of view. You can
16
CAR GAME
try adding more functionalities like creating more obstacles in the way or to
increase the difficulty of the game you can try increasing the speed of the car
after each level.
REFERENCE
https://copyassignment.com/simple-car-race-game-in-java/
https://www.cs.columbia.edu/~sedwards/classes/2015/4840/designs/racing.pdf
https://en.wikipedia.org/wiki/Racing_game
17