-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGround.java
More file actions
127 lines (100 loc) · 3.28 KB
/
Copy pathGround.java
File metadata and controls
127 lines (100 loc) · 3.28 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
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import java.awt.*;
/**
Draws the ground in the game
@author Jack Humphries
@version 5/16/2014
*/
public class Ground
{
/** the distance by which the ground moves */
public static final int GROUND_MOVE = 1;
/** the height of the ground */
public static final int HEIGHT = 12;
/** the y coordinate of the top of the ground */
private int y;
/** the width of the ground */
private int width;
/** the image tiles that make up the ground */
private ArrayList<BufferedImage> groundTiles;
/** the x coordinates for the image tiles that make up the ground */
private ArrayList<Integer> groundTileXCoords;
/**
generates the graphics for the ground
*/
private void generateGround()
{
groundTiles = new ArrayList<BufferedImage>();
groundTileXCoords = new ArrayList<Integer>();
File groundPath = new File("images/ground.png");
BufferedImage groundTile = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_RGB);
try
{
groundTile = ImageIO.read(groundPath);
}
catch (Exception exception)
{
}
int tempWidth = 0;
//add enough tiles to the array so that there is an extra tile
while (tempWidth < width + groundTile.getWidth())
{
groundTiles.add(groundTile);
groundTileXCoords.add(tempWidth);
tempWidth += groundTile.getWidth();
}
}
/**
redraws the ground
@param g2 the graphics object
*/
public void redrawGround(Graphics2D g2)
{
for (int tile = 0; tile < groundTiles.size(); tile++)
{
BufferedImage groundTile = groundTiles.get(tile);
int groundTileXCoord = groundTileXCoords.get(tile);
g2.drawImage(groundTile, groundTileXCoord, y, null);
}
}
/**
moves the ground
*/
public void moveGround()
{
int tileWidth = groundTiles.get(0).getWidth();
//the first tile has moved completely off the screen
//add it back to the end of the array
if (groundTileXCoords.get(0) - 1 <= -tileWidth)
{
BufferedImage firstGroundTile = groundTiles.get(0);
int firstGroundTileXCoord = groundTileXCoords.get(0);
BufferedImage lastGroundTile
= groundTiles.get(groundTiles.size() -1);
int lastGroundTileXCoord
= groundTileXCoords.get(groundTileXCoords.size() - 1);
int newXCoord = lastGroundTileXCoord + lastGroundTile.getWidth();
groundTiles.add(firstGroundTile);
groundTileXCoords.add(newXCoord);
groundTiles.remove(0);
groundTileXCoords.remove(0);
}
for (int tile = 0; tile < groundTiles.size(); tile++)
{
BufferedImage groundTile = groundTiles.get(tile);
int groundTileXCoord = groundTileXCoords.get(tile);
groundTileXCoord -= GROUND_MOVE;
groundTileXCoords.set(tile, groundTileXCoord);
}
}
public Ground(int theWidth, int theY)
{
y = theY;
width = theWidth;
generateGround();
}
}