-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClouds.java
More file actions
176 lines (139 loc) · 5.24 KB
/
Copy pathClouds.java
File metadata and controls
176 lines (139 loc) · 5.24 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import java.awt.*;
import java.util.Random;
/**
Draws the clouds in the game
@author Jack Humphries
@version 5/15/2014
*/
public class Clouds
{
/** the number of cloud files available in the game */
public static final int NUMBER_OF_FILE_CLOUDS = 4;
/** the height in pixels of the tallest cloud image included
in the project */
public static final int MAXIMUM_CLOUD_HEIGHT = 260;
/** the top amount of horizontal distance that the clouds should move */
public static final double TOP_MOVE_DISTANCE = 0.5;
/** the bottom amount of horizontal distance that the clouds should move */
public static final double BOTTOM_MOVE_DISTANCE = 0.1;
/** the cloud images that can be shown in the game */
private ArrayList<BufferedImage> fileClouds;
/** the clouds shown in the game */
private ArrayList<BufferedImage> displayClouds;
/** the x coordinates of the clouds shown in the game */
private ArrayList<Double> displayCloudXCoords;
/** the y coordinates of the clouds shown in the game */
private ArrayList<Integer> displayCloudYCoords;
/** the distance by which each cloud should move */
private ArrayList<Double> displayCloudMoveDistances;
/** the top possible y coordinate of the top of clouds */
private int topY;
/** the bottom possible y coordinate of the top of clouds */
private int bottomY;
/** the width of the game scene */
private int sceneWidth;
/**
generates the graphics for the clouds
*/
private void generateClouds()
{
fileClouds = new ArrayList<BufferedImage>();
displayClouds = new ArrayList<BufferedImage>();
displayCloudXCoords = new ArrayList<Double>();
displayCloudYCoords = new ArrayList<Integer>();
displayCloudMoveDistances = new ArrayList<Double>();
for (int count = 0; count < NUMBER_OF_FILE_CLOUDS; count++)
{
File cloudPath = new File("images/cloud_" + count + ".png");
BufferedImage cloud = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_RGB);
try
{
cloud = ImageIO.read(cloudPath);
}
catch(Exception exception)
{
}
fileClouds.add(cloud);
}
Random gen = new Random();
int cloudToLoad = gen.nextInt(NUMBER_OF_FILE_CLOUDS);
BufferedImage newCloud = fileClouds.get(cloudToLoad);
displayClouds.add(newCloud);
displayCloudXCoords.add((double)(sceneWidth - 1));
int y = bottomY + gen.nextInt(topY - bottomY);
displayCloudYCoords.add(y);
double distanceToMove
= BOTTOM_MOVE_DISTANCE
+ (TOP_MOVE_DISTANCE - BOTTOM_MOVE_DISTANCE)
* gen.nextDouble();
displayCloudMoveDistances.add(distanceToMove);
}
/**
redraws the clouds
@param g2 the graphics object
*/
public void redrawClouds(Graphics2D g2)
{
for (int count = 0; count < displayClouds.size(); count++)
{
BufferedImage cloud = displayClouds.get(count);
double cloudX = displayCloudXCoords.get(count);
int y = displayCloudYCoords.get(count);
g2.drawImage(cloud, (int)cloudX, y, null);
}
}
/**
moves the clouds
*/
public void moveClouds()
{
Random gen = new Random();
BufferedImage lastCloud = displayClouds.get(displayClouds.size() - 1);
Double lastCloudX
= displayCloudXCoords.get(displayCloudXCoords.size() - 1);
if (lastCloudX <= sceneWidth - lastCloud.getWidth() - 10
|| displayClouds.size() == 0)
{
int cloudToLoad = gen.nextInt(NUMBER_OF_FILE_CLOUDS);
BufferedImage cloud = fileClouds.get(cloudToLoad);
displayClouds.add(cloud);
displayCloudXCoords.add((double)(sceneWidth - 1));
int y = bottomY + gen.nextInt(topY - bottomY);
displayCloudYCoords.add(y);
double distanceToMove
= BOTTOM_MOVE_DISTANCE
+ (TOP_MOVE_DISTANCE - BOTTOM_MOVE_DISTANCE)
* gen.nextDouble();
displayCloudMoveDistances.add(distanceToMove);
}
BufferedImage firstCloud = displayClouds.get(0);
Double firstCloudX = displayCloudXCoords.get(0);
if (firstCloudX <= -firstCloud.getWidth())
{
displayClouds.remove(0);
displayCloudXCoords.remove(0);
displayCloudYCoords.remove(0);
displayCloudMoveDistances.remove(0);
}
for (int count = 0; count < displayClouds.size(); count++)
{
BufferedImage cloud = displayClouds.get(count);
Double cloudX = displayCloudXCoords.get(count);
double distanceToMove = displayCloudMoveDistances.get(count);
double newX = (cloudX - distanceToMove);
displayCloudXCoords.set(count, newX);
}
}
public Clouds(int theSceneWidth, int theTopY, int theBottomY)
{
sceneWidth = theSceneWidth;
topY = theTopY;
bottomY = theBottomY;
generateClouds();
}
}