-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoad.java
More file actions
91 lines (56 loc) · 3.16 KB
/
Copy pathRoad.java
File metadata and controls
91 lines (56 loc) · 3.16 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.util.Random;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
public class Road extends Segment {
//Variable ================================================================================================================================================
int laneMarkerStart; //This is just part of the drawing. We need it to be randomized once, rather than every cycle, so it needs to be an instance variable.
//================================================================================================================================================
// Constructor ================================================================================================
public Road(int yCoordinateTop){
super();
this.yCoordinateTop=yCoordinateTop;
obstacles = new ObjectHolder<Obstacle>();
Random rand = new Random();
laneMarkerStart=rand.nextInt(80);//Start location of white lines when drawing lanes
int leftOrRight=rand.nextInt(2);//0 means it drives to the right
for (int i=0; i<150; i++){//This code adds 150 obstacles to the road, spacing them semi-randomly.
int l = rand.nextInt(200);//Determines the initial locations of the first obstacles and prevents them from all lining up and moving in line with each other
int j = rand.nextInt(10);//Determines what type of obstacle
int k = rand.nextInt(50);//Determines spacing between obstacles
if (j>5){//There are essentially 4 options for obstacles. Each of the two types and each of two directions. All obstacles in one road are same direction, but everything else (including spacing) gets randomized on a per car basis.
if (leftOrRight==0)
obstacles.append (new Sedan(Frogger.WIDTH-l-i*(k+300), yCoordinateTop, leftOrRight));
else
obstacles.append (new Sedan(l+i*(k+300), yCoordinateTop, leftOrRight));
}
else{
if (leftOrRight==0)
obstacles.append (new Truck(Frogger.WIDTH-l-i*(k+300), yCoordinateTop, leftOrRight));
else
obstacles.append (new Truck(l+i*(k+300), yCoordinateTop, leftOrRight));
}
}
}//Constructor
//================================================================================================================================================
// Methods ================================================================================================================================================
public void update(){
obstacles.update();
}//update
public void draw(Graphics g, String biome){
g.setColor(Color.BLACK);
for (int i=0; i<Frogger.WIDTH; i+=4)
for (int j=yCoordinateTop; j<yCoordinateTop+50; j+=5)
g.drawRect(i, j, 0, 0);
g.setColor(Color.WHITE);
for (int i= laneMarkerStart; i<Frogger.WIDTH; i+=80)
g.fillRect(i, yCoordinateTop, 25, 5);
obstacles.draw(g, biome);
}//draw
//================================================================================================================================================
}// Road