home / study / engineering / computer science / questions and answers / this pro
ID: 3836828 • Letter: H
Question
home / study / engineering / computer science / questions and answers / this program needs to be done in java. in this ...
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: This program needs to be done in java. In this gra...
Bookmark
This program needs to be done in java.
In this graphical program, you are going to simulate arcade car racing game. You need a single player that is movable with the arrow keys, and you need another vehicle that will race the player. You should be able to race by hitting the right arrow key toward the end of the screen. If you get to the end of the screen, the car should start over on the left side of the screen. The AI car should start over but should move faster this time. Each time you make it to the end of the screen before the AI, you gain a point. If the AI makes it before you, the game is over. You should be able to play the game for 30 seconds. (i.e. you should survive for at least 30 seconds). Don’t worry about collision detection with the AI car.
At the end of the simulation, you should gather the person’s name, their favorite number, and their email address. You should check to make sure it is correctly formatted.
Specifics:
Gather the person’s name.
Gather the person’s favorite number – should be a number.
Collect the individual’s email – should have an @ and the ". " operator.
Should pre-emptively check all those values as well as have exception handlers where
appropriate.
Should have a countdown clock that starts at 30 and counts down to 0.
The AI car should race the player until the clock hits zero.
The player object should be able to move using the arrow keys.
Ignore collision detection.
For any exception handlers, you should write any exceptions to a log file.
Explanation / Answer
package cargame;
import javax.swing.*;
public class CarGame {
public static void main(String[] args) {
// Create application fram and a game surface
JFrame frame = new JFrame("Car game");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
GameSurface s = new GameSurface();
// Create a few cars and assign behaviours to them
Car myCar = new Car("car.png");
myCar.updatePosition(450, 450);
myCar.setMass(1.0f);
myCar.setMaxSpeed(100.0f);
myCar.setMaxSteering(70.0f);
myCar.addBehaviour(new RoamBehaviour(100, 100, 300, 300));
Car myCar2 = new Car("car.png");
myCar2.updatePosition(50, 50);
myCar2.setMass(1.0f);
myCar2.setMaxSpeed(120.0f);
myCar2.setMaxSteering(100.0f);
myCar2.addBehaviour(new PursuitBehaviour(myCar));
myCar2.addBehaviour(new BounceOffWallsBehaviour(30, 30, 470, 470));
Car myCar3 = new Car("playercar.png");
myCar3.updatePosition(250, 250);
myCar3.setMass(1.0f);
myCar3.setMaxSpeed(120.0f);
myCar3.setMaxSteering(300.0f);
myCar3.updateVelocity(120.0f, 0.0f);
PlayerSteeringBehaviour steering = new PlayerSteeringBehaviour();
myCar3.addBehaviour(steering);
myCar3.addBehaviour(new BounceOffWallsBehaviour(30, 30, 470, 470));
s.addKeyListener(steering);
// Add the cars to the game surface so that
// they will be drawn
s.getVehicles().add(myCar);
s.getVehicles().add(myCar2);
s.getVehicles().add(myCar3);
// Display the game surface in the frame, and
// make the frame visible
frame.setContentPane(s);
frame.setSize(500, 500);
frame.setVisible(true);
// Since we want to receive keyboard events,
// the game surface needs to have the input focus
s.requestFocusInWindow();
// Create the animation thread and start it
AnimationSystem a = new AnimationSystem(s);
Thread t = new Thread(a);
t.start();
}
}
Car.java
package cargame;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
public class Car extends Vehicle implements ImageObserver {
protected Image img;
protected float w2;
protected float h2;
public Car(String imageFileName) {
ImageIcon iic = new ImageIcon(imageFileName);
img = Transparency.makeColorTransparent(iic.getImage(), Color.black);
}
public void draw(Graphics2D g2) {
AffineTransform saveXform = g2.getTransform();
g2.translate(position.x, position.y);
g2.rotate(Math.atan2(orientation.y, orientation.x));
g2.drawImage(img,
AffineTransform.getTranslateInstance(-img.getWidth(this) / 2.0, -img.getHeight(this) / 2.0),
this);
g2.setTransform(saveXform);
/*
g2.setPaint(Color.yellow);
g2.drawLine((int)Math.floor(position.x), (int)Math.floor(position.y),
(int)Math.floor(position.x + 50.0f * side.x), (int)Math.floor(position.y + 50.0f * side.y));
g2.setPaint(Color.blue);
g2.drawLine((int)Math.floor(position.x), (int)Math.floor(position.y),
(int)Math.floor(position.x + velocity.x), (int)Math.floor(position.y + velocity.y));
g2.setPaint(Color.white);
g2.drawLine((int)Math.floor(position.x), (int)Math.floor(position.y),
(int)Math.floor(position.x + steering.x), (int)Math.floor(position.y + steering.y));
*/
}
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
return true;
}
public boolean intersects(Vehicle v) {
if (v instanceof Car) {
Car c = (Car)v;
Point2D.Float d = new Point2D.Float(position.x - c.position.x, position.y - c.position.y);
if (length(d) < 25.0f) { // Should probably compute the radius from the images instead...
return true;
}
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.