-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunAndDodge.java
More file actions
281 lines (216 loc) · 6.54 KB
/
RunAndDodge.java
File metadata and controls
281 lines (216 loc) · 6.54 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RunAndDodge implements Runnable{
public static int TICK_TIME_MIL = 50;
public static int STANDART_BULLET_SIZE = 50;
public static int STANDART_PLAYER_SIZE = 50;
public static String BULLET_ICON_PATH = "rec/RunAndDodge/Bullet/";
public static String PLAYER_ICON_PATH = "rec/RunAndDodge/Player/";
public static int MAP_SIZE = 500;
@SuppressWarnings("serial")
abstract private class Entity extends JLabel {
public double xPos,yPos;
public double lastTime = System.currentTimeMillis();
public Entity(double pXPos, double pYPos) {
this.xPos = pXPos;
this.yPos = pYPos;
this.lastTime = System.currentTimeMillis();
}
/*
* Updated die optische Position mit der Logischen.
*/
public void updatePos() {
this.setLocation((int)Math.round(this.xPos), (int)Math.round(this.yPos));
}
public void setImage(Image img) {
this.setIcon(new ImageIcon(img));
}
abstract public void move();
public void moveTo(double x,double y) {
this.xPos = x;
this.yPos = y;
}
}
@SuppressWarnings("serial")
private class Bullet extends Entity{
public double xSpeed,ySpeed;
public Bullet(double pXPos, double pYPos,double pXSpeed,double pYSpeed) {
super(pXPos,pYPos);
this.xSpeed = pXSpeed;
this.ySpeed = pYSpeed;
this.createBullet();
}
private void createBullet() {
this.setBounds( ((int)Math.round(this.xPos)), ((int)Math.round(this.yPos)), RunAndDodge.STANDART_BULLET_SIZE , RunAndDodge.STANDART_BULLET_SIZE );
try {
Image img = ImageIO.read(new File(RunAndDodge.BULLET_ICON_PATH + "bullet.png"));
this.setIcon(new ImageIcon(img));
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Ändert die Flugrichtung der Kugel
*/
public void changePath(double pXSpeed, double pYSpeed) {
this.xSpeed = pXSpeed;
this.ySpeed = pYSpeed;
}
public void move() {
double systemTime = System.currentTimeMillis();
this.xPos = this.xPos + this.xSpeed * ((systemTime - this.lastTime) / RunAndDodge.TICK_TIME_MIL);
this.yPos = this.yPos + this.ySpeed * ((systemTime - this.lastTime) / RunAndDodge.TICK_TIME_MIL);
this.lastTime = systemTime;
this.updatePos();
}
}
@SuppressWarnings("serial")
private class Player extends Entity implements KeyListener{
public double xPos = 250,yPos = 250;
public boolean isWpressed = false;
public boolean isApressed = false;
public boolean isSpressed = false;
public boolean isDpressed = false;
public double movementspeed = 5;
public Player(double pXPos, double pYPos) {
super(pXPos,pYPos);
this.createPlayer();
}
public void createPlayer(){
this.setBounds(RunAndDodge.MAP_SIZE/2, RunAndDodge.MAP_SIZE/2, RunAndDodge.STANDART_PLAYER_SIZE, RunAndDodge.STANDART_PLAYER_SIZE);
this.setFocusable(false);
}
public void setImage(Image img) {
this.setIcon(new ImageIcon(img));
}
public void move() {
double currentTime = System.currentTimeMillis();
if(this.isWpressed) {
if(this.isSpressed) {
// Do nothing
} else {
this.yPos = this.yPos + this.movementspeed * ((currentTime - this.lastTime) / RunAndDodge.TICK_TIME_MIL);
}
} else if(this.isSpressed){
this.yPos = this.yPos - this.movementspeed * ((currentTime - this.lastTime) / RunAndDodge.TICK_TIME_MIL);
}
if(this.isApressed) {
if(this.isDpressed) {
// Do nothing
} else {
this.xPos = this.xPos + this.movementspeed * ((currentTime - this.lastTime) / RunAndDodge.TICK_TIME_MIL);
}
} else if(this.isDpressed){
this.xPos = this.xPos - this.movementspeed * ((currentTime - this.lastTime) / RunAndDodge.TICK_TIME_MIL);
}
}
@Override
public void keyPressed(KeyEvent ke) {
if(ke.getKeyChar() == 'w' ) {
isWpressed = true;
}
else if(ke.getKeyChar() == 'a' ) {
isApressed = true;
}
else if(ke.getKeyChar() == 's' ) {
isSpressed = true;
}
else if(ke.getKeyChar() == 'd' ) {
isDpressed = true;
}
}
@Override
public void keyReleased(KeyEvent ke) {
if(ke.getKeyChar() == 'w' ) {
isWpressed = false;
}
else if(ke.getKeyChar() == 'a' ) {
isApressed = false;
}
else if(ke.getKeyChar() == 's' ) {
isSpressed = false;
}
else if(ke.getKeyChar() == 'd' ) {
isDpressed = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
private JFrame mainFrame;
private Player player;
private List<Image> playerImages;
private boolean gotHit = false;
private Bullet[] bullets;
private int bulletTimer = 1000;
public RunAndDodge() {
this.createFrame();
this.loadGraphics();
this.fillFrame();
this.mainFrame.revalidate();
this.mainFrame.repaint();
}
private void createFrame() {
this.mainFrame = new JFrame("Run And Dodge");
this.mainFrame.setBounds(0, 0, RunAndDodge.MAP_SIZE, RunAndDodge.MAP_SIZE );
this.mainFrame.setLocationRelativeTo(null);
this.mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.mainFrame.setVisible(true);
}
private void fillFrame() {
this.player = new Player(RunAndDodge.MAP_SIZE / 2,RunAndDodge.MAP_SIZE / 2);
this.player.setImage(this.playerImages.get(0));
this.mainFrame.addKeyListener(this.player);
this.mainFrame.add(this.player);
}
private void loadGraphics() {
FileReader fr;
try {
fr = new FileReader(RunAndDodge.PLAYER_ICON_PATH + "count.txt");
BufferedReader br = new BufferedReader(fr);
try {
int count = Integer.parseInt(br.readLine());
System.out.println("Count :" + count);
for(int i = 1; i< count+1; i++) {
File temp = new File(this.PLAYER_ICON_PATH +"1.png");
this.playerImages.add(ImageIO.read(temp));
}
}
catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(!gotHit) {
System.out.println("loop");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
RunAndDodge a = new RunAndDodge();
Thread t = new Thread(a);
t.start();
}
}