-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
42 lines (32 loc) · 1014 Bytes
/
Copy pathPlayer.java
File metadata and controls
42 lines (32 loc) · 1014 Bytes
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
package game_template;
import java.awt.Color;
import java.awt.Graphics2D;
public class Player extends GameObject {
final private int speed = 5; // how fast the player moves
final private int fps = 10; // how often the player moves
final private double moveInterval = 1000000000/fps;
private double nextMoveTime;
public Player(int x_, int y_, int width_, int height_) {
super(x_, y_, width_, height_);
nextMoveTime = System.nanoTime() + moveInterval;
}
public void update(KeyHandler kh) {
double remainingTime = (nextMoveTime - System.nanoTime())/1000000;
if (remainingTime <= 0) {
move(kh.up, kh.left, kh.down, kh.right);
nextMoveTime += moveInterval;
}
}
private void move(boolean up, boolean left, boolean down, boolean right) {
int dx = 0, dy = 0;
if (up) dy = -speed;
if (down) dy = speed;
if (left) dx = -speed;
if (right) dx = speed;
x += dx;
y += dy;
}
@Override
public void draw(Graphics2D g) {
}
}