-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
67 lines (58 loc) · 1.48 KB
/
Player.java
File metadata and controls
67 lines (58 loc) · 1.48 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
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Player implements IGameConstants{
private int x;
private int y;
private int w;
private int h;
public int life;
private BufferedImage image;
private int speed;
private JFrame frame;
public Player(JFrame jFrame){
life = 4;
this.x = 20;
this.y = FLOOR;
this.h = PLAYER_H;
this.w = PLAYER_W;
this.frame = jFrame;
try {
image = ImageIO.read(Player.class.getResource(PLAYER_IMG));
}
catch (IOException e){
JOptionPane.showMessageDialog(frame, "Backgraound Image not found");
System.exit(0);
}
catch (IllegalArgumentException e){
JOptionPane.showMessageDialog(frame, "Backgraound Image not found");
System.exit(0);
}
}
public void drawPlayer(Graphics graphics){
graphics.drawImage(image, x, FLOOR, w, h, null);
}
public void direction(int dir){
speed = 5;
speed = speed * dir;
}
public void move(){
if (x < LEFT_CONSTRAINT){
x = LEFT_CONSTRAINT;
}
else if (x > RIGHT_CONSTRAINT - w){
x = RIGHT_CONSTRAINT - w;
}
else{
x += speed;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}