-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
66 lines (58 loc) · 1.24 KB
/
Player.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
/*
* File: Player.java
* Author: Luke S. Snyder
* UA ID: 010691128
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Player extends Sprite implements Serializable
{
private int health;
// Constructor.
public Player(int health)
{
// Initialize superclass.
setX(500);
setY(675);
setSpan(65);
this.health = health;
}
public void tick(int width)
{
if ((getX() > 0 && getX() < width) ||
(getX() <= 0 && getVelocityX() > 0) ||
(getX() >= width && getVelocityX() < 0))
setX(getX() + getVelocityX());
}
@Override
public void updateImage(Graphics g)
{
g.drawImage(Enemy.getPlayerImage(), getX(), getY(), 80, 80, null);
Font font = new Font("arial", Font.BOLD, 15);
g.setFont(font);
g.setColor(Color.white);
g.drawString("Health:", 900, 30);
g.drawRect(900, 40, 150, 25);
g.setColor(Color.green);
g.fillRect(900, 40, getHealth(), 25);
}
public void setHealth(int health)
{
this.health = health < 0 ? 0 : health;
}
public int getHealth()
{
return health;
}
// Reset player state.
public void reset()
{
Sprite.setPlayerImage("spaceship.png");
health = 150;
setX(500);
setY(675);
}
}