-
Notifications
You must be signed in to change notification settings - Fork 0
/
Movement.java
76 lines (68 loc) · 1.97 KB
/
Movement.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
package game;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
public class Movement extends Direction implements ActionListener, KeyListener{
double x = 0;
double y = 0;
double speed = 3;
double diagSpeed = 1.375;
public void gameLoop(SpillProsjekt spill) throws InterruptedException{
while(true){
if((keys[0] == 1) && (keys[1] == 1) && (keys[2] == 0) && (keys[3] == 0)){
x -= speed/diagSpeed;
y -= speed/diagSpeed;
}
if((keys[3] == 1) && (keys[0] == 1) && (keys[1] == 0) && (keys[2] == 0)){
x -= speed/diagSpeed;
y += speed/diagSpeed;
}
if((keys[1] == 1) && (keys[2] == 1) && (keys[0] == 0) && (keys[3] == 0)){
x += speed/diagSpeed;
y -= speed/diagSpeed;
}
if((keys[2] == 1) && (keys[3] == 1) && (keys[1] == 0) && (keys[0] == 0)){
x += speed/diagSpeed;
y += speed/diagSpeed;
}
if((keys[1] == 1) && (keys[3] == 0) && (keys[2] == 0) && (keys[0] == 0)){
y -= speed;
}
if((keys[3] == 1) && (keys[1] == 0) && (keys[2] == 0) && (keys[0] == 0)){
y += speed;
}
if((keys[2] == 1) && (keys[3] == 0) && (keys[1] == 0) && (keys[0] == 0)){
x += speed;
}
if((keys[0] == 1) && (keys[3] == 0) && (keys[2] == 0) && (keys[1] == 0)){
x -= speed;
}
spill.repaint();
Thread.sleep(10);
}
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(code >= 37 && code <= 40){
keys[code-37] = 1;
setDirection(code-37, false);
}
}
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if(code >= 37 && code <= 40){
keys[code-37] = 0;
setDirection(code-37, true);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
}
}