import java.awt.*;
import java.applet.*;
import java.awt.image.*;
/**This Original Java Code is written by me, Using double buffering.
*I haven't actually loaded any GIF images for the car, but I had
*drawn it by myself. It would be great looking if I had Included
* GIF's but Since it may even slow down the game I had not used
* them. I had used MultiThreading and I am running two threads.
* One for having control over the user CAR and the other for the racing
* cars. This program has some bugs. I will remove in my later versions.
* for best vision use applet width and height as
* <applet code=Car.class width=640 height=400> </applet>
* You can modify this code and distribute among your friends
* Original Programmed by : A. LEVI.
* My e-mail address is levi57@hotmail.com * -still unemployed- */
public class Car extends Applet implements Runnable
{
Image img,car1,cup;
// This Field is to Trace out the User's Car position
static int position=235;
static int points = 0;
// You can change this delay to any value which effects in the speed of the game
static int delay = 50;
road rd;
Thread thr;
static int pts=50;
boolean msg=true;
// If this field is true then the 'road' thread will be stopped
static boolean kill=false;
public void init()
{
int x[] = { 15, 15, 0, 60, 45, 45 };
int y[] = { 45, 50, 58, 58, 50, 45 };
setBackground(Color.black);
// Drawing the Car Image
img = createImage(60,60);
Graphics g = img.getGraphics();
g.setColor(Color.black);
g.fillRect(0,0,60,60);
g.setColor(Color.green);
g.fillRect(12,20,36,7);
g.fillRect(8,15,4,17);
g.fillRect(48,15,4,17);
g.fillRect(5,40,50,7);
g.fillRect(0,35,5,17);
g.fillRect(55,35,5,17);
g.setColor(Color.red);
g.fillRect(20,0,20,15);
g.fillRect(15,15,30,40);
g.setColor(Color.blue);
g.fillRect(20,20,7,10);
g.fillRect(33,20,7,10);
g.setColor(Color.red);
g.fillRect(22,22,3,6);
g.fillRect(35,22,3,6);
g.setFont(new Font("TimesRoman",Font.PLAIN,7));
g.setColor(Color.white);
g.fillPolygon(x,y,6);
g.setColor(Color.black);
g.drawString("YAMAHA",15,52);
// Drawing the Car Image
car1 = createImage(60,60);
Graphics g1 = car1.getGraphics();
g1.setColor(Color.black);
g1.fillRect(0,0,60,60);
g1.setColor(Color.green);
g1.fillRect(12,20,36,7);
g1.fillRect(8,15,4,17);
g1.fillRect(48,15,4,17);
g1.fillRect(5,40,50,7);
g1.fillRect(0,35,5,17);
g1.fillRect(55,35,5,17);
g1.setColor(Color.blue);
g1.fillRect(20,0,20,15);
g1.fillRect(15,15,30,40);
g1.setColor(Color.red);
g1.fillRect(20,20,7,10);
g1.fillRect(33,20,7,10);
g1.setColor(Color.blue);
g1.fillRect(22,22,3,6);
g1.fillRect(35,22,3,6);
g1.setFont(new Font("TimesRoman",Font.PLAIN,7));
g1.setColor(Color.white);
g1.fillPolygon(x,y,6);
g1.setColor(Color.black);
g1.drawString(" B.M.W ",15,52);
thr = new Thread(this); thr.start(); rd = new road(getGraphics(),car1,this); rd.start();
// Cup Image
int a[] = {20,5,35};
int b[] = {150,160,160};
cup = createImage(50,165);
Graphics handle = cup.getGraphics();
handle.setColor(Color.black);
handle.fillRect(0,0,50,165);
handle.setColor(Color.red);
handle.fillArc(0,40,40,30,0,180);
handle.setColor(Color.yellow);
handle.fillArc(0,15,40,80,180,180);
handle.setColor(Color.red);
handle.drawLine(20,95,20,150);
handle.fillPolygon(a,b,3);
}
public void run()
{
// If you cross the 50 mark you will get a Cup of Java
while(points <= 52)
{
if(points == 52 || kill == true)
{
rd.stop();
repaint();
thr.stop();
}
if((points%4)==0)
{
rd.j = 0;
pts = points;
points++;
delay--;
if(delay <= 0)
delay = 0;
rd.flag=1;
repaint();
}
try
{
Thread.sleep(delay);
}catch(InterruptedException exp){}
}
}
public void destroy()
{
thr.stop();
rd.stop();
}
//If User presses mouse the the Car is shifted to opposite side of the road
public boolean mouseDown(Event e , int x, int y)
{
if(e.id == Event.MOUSE_DOWN)
{
if(position == 235)
position = 355;
else
position = 235;
repaint();
}
return true;
}
public void paint(Graphics gr)
{
if(!kill)
{
if(msg)
{
// This is the opening message
gr.setColor(Color.black);
gr.fillRect(0,0,640,400);
gr.setColor(Color.yellow);
gr.setFont(new Font("TimesRoman",Font.BOLD,16));
gr.drawString("TO PLAY THE GAME CLICK ANY MOUSE BUTTON",140,100);
gr.drawString("THE CAR WILL MOVE IN OPPOSITE DIRECTION",140,150);
gr.drawString("WAIT A MINUTE......",230,200);
msg = false;
try{
Thread.sleep(3000);
}catch(Exception exp){}
gr.setColor(Color.black);
gr.fillRect(0,0,640,400);
}
gr.setColor(Color.white);
gr.fillRect(200,0,10,400);
gr.fillRect(440,0,10,400);
gr.drawImage(img,position,300,this);
gr.setColor(Color.yellow);
gr.fillRect(550,5,637,25);
gr.setColor(Color.blue);
gr.setFont(new Font("TimesRoman",Font.BOLD,20));
gr.drawString("Score :"+pts,557,22);
if(points >= 52)
{
for(int xyz=0;xyz<3;xyz++)
{
gr.setColor(Color.yellow);
gr.drawString("Have a Cuppa Java",240,100);
gr.drawImage(cup,300,100,this);
gr.setColor(Color.yellow);
gr.fillRect(550,5,637,25);
gr.setColor(Color.blue);
gr.setFont(new Font("TimesRoman",Font.BOLD,20));
gr.drawString("Score :50",557,22);
try
{
Thread.sleep(500);
}catch(InterruptedException exp){}
}
}
}
else
{
gr.setColor(Color.yellow);
gr.drawString("YOU HAVE LOST THE GAME",250,200);
}
}
}
class road extends Thread
{
int i;
public static int j = 0;
Graphics g;
Image car2;
ImageObserver io;
public static int flag = 0;
boolean msg=true;
road(Graphics g,Image car2,ImageObserver io)
{
this.g = g;
this.io = io;
this.car2 = car2;
}
public void run()
{
drawRoad(g);
}
// The actual logic i.e Moving of cars is here
public void drawRoad(Graphics gr)
{
if(msg)
{
gr.setColor(Color.black);
gr.fillRect(0,0,640,400);
gr.setColor(Color.yellow);
gr.setFont(new Font("TimesRoman",Font.BOLD,16));
gr.drawString("TO PLAY THE GAME CLICK ANY MOUSE BUTTON",140,100);
gr.drawString("THE CAR WILL MOVE IN OPPOSITE DIRECTION",140,150);
gr.drawString("WAIT A MINUTE......",230,200);
msg = false;
try
{
Thread.sleep(3000);
}catch(Exception exp){}
gr.setColor(Color.black);
gr.fillRect(0,0,640,400);
}
for(;j<=1000;j+=10)
{
for(i=-1000;i<=479;i+=60)
{
gr.setColor(Color.black);
gr.fillRect(320,i+j,10,i+j+50);
gr.setColor(Color.white);
gr.fillRect(320,i+j+10,10,i+j+60);
}
gr.clearRect(235,j-10,60,60);
gr.drawImage(car2,235,0+j,io);
gr.clearRect(355,-150+(j-10),60,60);
gr.drawImage(car2,355,-150+j,io);
gr.clearRect(235,-300+(j-10),60,60);
gr.drawImage(car2,235,-300+j,io);
gr.clearRect(355,-450+(j-10),60,60);
gr.drawImage(car2,355,-450+j,io);
if( (Car.position == 235 && (j >= 250 && j <= 360)) || (Car.position == 355 && (j >= 400 && j <= 510)) || (Car.position == 235 && (j >= 550 && j <= 660)) || (Car.position == 355 && (j >= 700 && j <= 810)) )
{
try
{
Thread.sleep(2000);
Car.kill = true;
}catch(InterruptedException exp){}
}
if (j >= 360 ) { if( (( j - 360 ) % 150 ) == 0 )
{
if(flag == 1)
{
Car.points--;
flag = 0;
}
Car.points++;
gr.setColor(Color.yellow);
gr.fillRect(550,5,637,25);
gr.setColor(Color.blue);
gr.setFont(new Font("TimesRoman",Font.BOLD,20));
gr.drawString("Score :"+Car.points,557,22);
}
}
try
{
Thread.sleep(Car.delay);
}catch(InterruptedException exp){}
}
}
}