-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
63 lines (56 loc) · 2.1 KB
/
Car.java
File metadata and controls
63 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//import the libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.MouseInfo;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
//Class Car creates and displays the individual car objects in the main game. It is a basic object that
//moves in a certain speed along a certain direction in a certain position. If it collides with the
//player the player object will die.
public class Car{
private int dist; //the distance the object will travel in every loop
private int cx, cy; //the position of the object
private int dir; //the direction the cars will move in
private Image car_pic;
private int width, height; //the width and height of the "rectangular" car
//the directions the car can move in
public static final int LEFT = -1, RIGHT = 1;
//setup the information when a car is created
public Car(int x, int y, int distance, int direction, String name){
dist=distance;
cx=x;
cy=y;
dir=direction;
car_pic = new ImageIcon("cars/car"+name+".png").getImage(); //there are different types of car images, ordered by numbers
width = car_pic.getWidth(null); //the car doesn't have animation, therefore its width and height are stable.
height = car_pic.getHeight(null);
}
//move the car object
public void move(){
cx+=(dist*dir);
//all of the car objects have the same boundary and start (respectively 500 and -50 if RIGHT, -50 and 500 if LEFT)
if (cx>550 && dir == RIGHT){
cx=-50;
}
else if (cx<-50 && dir == LEFT){
cx=550;
}
}
//displays the car image
public void draw(Graphics g){
g.drawImage(car_pic, cx, cy, null);
}
//checks if the user had collided with the individual car object
public boolean checkCollide(Frog user){
//collision is based on image; they are all rectangles
Rectangle user_area = new Rectangle(user.getFx(), user.getFy(), user.getWidth(), user.getHeight());
Rectangle car_area = new Rectangle(cx, cy, width, height);
//check if the image intersects with each other
if (user_area.intersects(car_area)){
return true;
}
return false;
}
}