-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.java
More file actions
124 lines (110 loc) · 3.42 KB
/
Sprite.java
File metadata and controls
124 lines (110 loc) · 3.42 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
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.io.*;
import java.awt.image.BufferedImage;
import java.util.Random;
// Class that creates and updates the sprites
public class Sprite {
final int SPRITE_Y_START = 160;
final String LIZARD_PATH = "/lizard.png";
final String CAR_PATH = "/car.png";
final String CACTUS_PATH = "/cactus.png";
private Boolean isHarmful = null;
private int spriteX, spriteY;
private BufferedImage image;
private String filePath;
public static Random generator = new Random();
private int spriteWidth, spriteHeight;
private final int MAX_SPEED = 45;
// Constructor
public Sprite (){
this.spriteX = setSpriteX();
this.spriteY = SPRITE_Y_START;
this.filePath = this.getSpritePath();
if (this.filePath == LIZARD_PATH){
this.isHarmful = false;
}
else {
this.isHarmful = true;
}
}
// Method to create and randomly place Sprite. Returns updated JLabel.
public JLabel getSprite(){
try
{
InputStream is = Roadrunner.class.getResourceAsStream(this.filePath);
this.image = ImageIO.read(is);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
ImageIcon imageIcon = new ImageIcon(this.image);
this.spriteWidth = imageIcon.getIconWidth();
this.spriteHeight = imageIcon.getIconHeight();
JLabel jLabel = new JLabel();
jLabel.setIcon(imageIcon);
jLabel.setBounds(this.spriteX, this.spriteY, this.spriteWidth + 20, this.spriteHeight + 20);
return jLabel;
}
// Method to create and randomly select sprite x-value
public int setSpriteX(){
int xValue = 320 + generator.nextInt(210);
return xValue;
}
// Method to update sprite Y value (i.e. speed)
public void updateSpriteY(int speed){
if (speed < MAX_SPEED){
this.spriteY += speed;
}
else{
this.spriteY += MAX_SPEED;
}
}
//Method to update and move sprite down screen each frame
public JLabel updateImage(){
JLabel updatedImg;
updatedImg = getSprite();
return updatedImg;
}
// Method to randomly generate Sprites
public String getSpritePath(){
String spritePath = "";
String[] spriteArray = { LIZARD_PATH, CAR_PATH, CACTUS_PATH };
int spriteIndex = generator.nextInt(spriteArray.length);
switch (spriteIndex) {
case 0:
spritePath = LIZARD_PATH;
break;
case 1:
spritePath = CAR_PATH;
break;
case 2:
spritePath = CACTUS_PATH;
break;
}
return spritePath;
}
// Method to return sprite X value as type int
public int getSpriteX(){
return this.spriteX;
}
// Method to return sprite y position as type int
public int getSpriteY() {
return this.spriteY;
}
// Method to return sprite width as type int
public int getSpriteWidth() {
return this.spriteWidth;
}
// Method to return sprite height as type int
public int getSpriteHeight() {
return this.spriteHeight;
}
// Method to return whether or not sprite is harmful as type boolean
public boolean isHarmful(){
return this.isHarmful;
}
}