In: Computer Science
(Java program) Write an applet that draws two animated cars that must race each other. The race cars should move along a straight line, with each starting behind a Start Line. The user should have the option to set the speed of each car. The speed range is 0 to 100. There should be a Start button to begin the race and an End button to stop the race at any time.
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.net.url;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionEventListener;
import javax.swing.JButton;
public class CarRace extends JPanel
{
int cross_a, cross_b;
int car_a, car_b;
int speed_a, speed_b;
int nOpp;
String imageLoc[];
int la[], lb[];
int score;
int max_score;
int speed_Opp;
boolean isDone;
boolean isUP, isDOWN, isLEFT, isRIGHT;
public CarRace()
{
cross_a = cross_b = -999;
addKeyListener(new KeyListener(){
public void keyTyped(keyEvent e)
}
public void keyReleased(keyEvent e){
car_stop(e);
public void keyPressed(keyEvent e){
car_move(e);
}});
jButton play = new JButton("play");
play.ActionEventListener(new ActionListener()
{
public void ActionPerformed(ActionEvent e)
{
CarRace play = new CarRace();
}
});
car_a = car_b = 0;
speed_a = speed_b = 0;
nOpp = 0;
la = new int[20];
lb = new int[20];
imageLoc = new String[20];
speed_Opp = new int[20];
score = max_score = 0;
}
public void paint(Graphics g){
super.paint(g);
Graphics2D obj = (Graphics2D) g;
obj.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try{
obj.drawImage(getToolkit().getImage("images/straight_road.jpg"), 0, 0, this);
if (cross_a >= -499 && cross_b >= -499)
obj.drawImage(getToolkit().getImage("images/cross_road.jpg"), cross_x, cross_y, this);
obj.drawImage(getToolkit().getImage("images/get_car.jpg"), car_a, car_b, this);
}
catch(Exception e){
System.out.println(e);
}
void Road_move(int count){
if(cross_a == -999 && cross_b == -999)
if(count % 10 == 0)
cross_a = 499;
cross_b = 0;
}}
else
cross_a--;
if(cross_a = -499 && cross_b = 0)
cross_a = cross_b = -999;
car_a += speed_a;
car_b += speed_b;
if (car_a < 0)
car_a = 0;
int index[] = new int[nOpp]
for(int i = 0; i < nOpp; i++){
if(la[i] >= 127)
index[i] == 1
}
int c = 0;
for(int i = 0; i < nOpp; i++){
if(index[i] == 1){
imageLoc[c] = imageLoc[i];
la[c] = la[i];
lb[c] = lb[i];
speed_Opp[c] = speed_Opp[i];
c++;
}
}
score += nOpp - c;
if(score > max_score)
max_score = score;
nOpp = c;
void Finish()
{
String str = "";
this.repaint();
if(score == max_score && score != 0)
str = "\n Congratulations...!!!You won";
JOptionPane.showMessageDialog(this, "Game Over!!\n Your score" :+score+"\nMax score" :+max_score+str)
System.exit(ABORT);
public void car_move(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP){
isUP = true;
speed_a = 1;
if(e.getKeyCode() == KeyEvent.VK_DOWN){
isDOWN = true;
speed_a = -2;
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
isRIGHT = true;
speed_b = 1;
if(e.getKeyCode() == KeyEvent.VK_LEFT){
isLEFT = true;
speed_b = -1;
}
}
public void car_stop(keyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP){
isUP = false;
speed_a = 0;
if(e.getKeyCode() == KeyEvent.VK_DOWN){
isDOWN = false;
speed_a = 0;
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
isRIGHT = false;
speed_b = 0;
if(e.getKeyCode() == KeyEvent.VK_LEFT){
isLEFT = false;
speed_b = 0;
public static void main(String arge[]){
JFrame Frame = new JFrame("Car Racing Game");
CarRace game = new CarRace();
frame.add(game);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int count = 1, c = 1;
while(true){
game.Road_move(count){
while (c <= 1){
game.repaint();
}
c++;
}
c = 1;
count++;
}
}