-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoad.java
80 lines (62 loc) · 2.65 KB
/
Road.java
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
package seriousgame;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;
public class Road{
GamePanel gp;
public BufferedImage[] roadImg = new BufferedImage[7];
private int moveRoadCounter = 0;
private int moveRoadNumber = 1;
public int x, y, width, height;
public double speed;
public Road(GamePanel gp) {
this.gp = gp;
setDefaultValues();
getRoadAnimation();
}
public void getRoadAnimation() {
try {
roadImg[0] = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/res/road1.png")));
roadImg[1] = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/res/road2.png")));
roadImg[2] = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/res/road3.png")));
roadImg[3] = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/res/road4.png")));
roadImg[4] = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/res/road5.png")));
roadImg[5] = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/res/road6.png")));
roadImg[6] = ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/res/road7.png")));
} catch (IOException e) {
e.printStackTrace();
}
}
public void setDefaultValues() {
x = 0;
y = 0;
width = 800;
height = 930;
}
public void update() {
moveRoadCounter++;
if(moveRoadCounter > speed) {
if (moveRoadNumber == 1) moveRoadNumber = 2;
else if (moveRoadNumber == 2) moveRoadNumber = 3;
else if (moveRoadNumber == 3) moveRoadNumber = 4;
else if (moveRoadNumber == 4) moveRoadNumber = 5;
else if (moveRoadNumber == 5) moveRoadNumber = 6;
else if (moveRoadNumber == 6) moveRoadNumber = 7;
else if (moveRoadNumber == 7) moveRoadNumber = 1;
moveRoadCounter = 0;
}
}
public void draw(Graphics g2D) {
BufferedImage image = null;
if(moveRoadNumber == 1) image = roadImg[0];
if(moveRoadNumber == 2) image = roadImg[1];
if(moveRoadNumber == 3) image = roadImg[2];
if(moveRoadNumber == 4) image = roadImg[3];
if(moveRoadNumber == 5) image = roadImg[4];
if(moveRoadNumber == 6) image = roadImg[5];
if(moveRoadNumber == 7) image = roadImg[6];
g2D.drawImage(image,x,y,width,height, null);
}
}