This repository was archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
162 lines (150 loc) · 2.28 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import javax.imageio.ImageIO;
import java.awt.Image;
import java.awt.event.KeyEvent;
public class Player
{
private Image stopSprite [];
private Image movingSprite[][];
private byte dir;
private Location loc;
private int mx, my;
private Stack direct;
private boolean isDir[];
public Player()
{
dir = 1;
stopSprite = new Image [4];
movingSprite = new Image [4][2];
isDir = new boolean[4];
try
{
for(int idx = 0; idx < 4; idx++)
stopSprite[idx] = ImageIO.read(this.getClass().getResource(idx + ".PNG"));
}
catch(Exception e)
{}
direct = new Stack();
loc = new Location(50,70);
}
public void move()
{
if (direct.peek() == 0)
my = -2;
else if (direct.peek() == 1)
mx = 2;
else if (direct.peek() == 2)
my = 2;
else if (direct.peek() == 3)
mx = -2;
else
{
if (isDir[0])
{
my = -2;
dir = 0;
}
else if (isDir[1])
{
mx = 2;
dir = 1;
}
else if (isDir[2])
{
my = 2;
dir = 2;
}
else if (isDir[3])
{
mx = -2;
dir = 3;
}
}
loc.x += mx;
loc.y += my;
//System.out.println(direct.length);
}
public Location getPos()
{
return this.loc;
}
public void keyPressed(KeyEvent ev)
{
int c = ev.getKeyCode();
boolean s = true;
if(c == KeyEvent.VK_UP)
{
//my = -2;
dir = 0;
}
else if (c == KeyEvent.VK_DOWN)
{
//my = 2;
dir = 2;
}
else if (c == KeyEvent.VK_LEFT)
{
//mx = -2;
dir = 3;
}
else if (c == KeyEvent.VK_RIGHT)
{
//mx = 2;
dir = 1;
}
else
{
direct.clear();
s = false;
}
if (s && isDir[dir] == false)
{
direct.push(dir);
isDir[dir] = true;
}
}
public void keyReleased(KeyEvent ev)
{
int c = ev.getKeyCode();
if (c == KeyEvent.VK_UP)
{
my = 0;
isDir[0] = false;
calcDir(0);
}
else if (c == KeyEvent.VK_DOWN)
{
my = 0;
isDir[2] = false;
calcDir(2);
}
else if (c == KeyEvent.VK_LEFT)
{
mx = 0;
isDir[3] = false;
calcDir(3);
}
else if (c == KeyEvent.VK_RIGHT)
{
mx = 0;
isDir[1] = false;
calcDir(1);
}
}
private void calcDir(int b)
{
if (direct.peek() == b)
{
direct.pop();
if (direct.peek() != -1)
dir = direct.peek();
}
else
{
direct.clear();
}
}
public Image getImage()
{
return stopSprite[dir];
}
}