forked from keenPioneer/PacMan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reuse.java
436 lines (406 loc) · 10.6 KB
/
reuse.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import java.awt.*;
import java.awt.event.*;
public class reuse implements KeyListener{
Occupied oc=new Occupied();
int round;
boolean gameState=true;
int X=900,Y=571;
int Point=0;
int speed=5;
int xv=940;
int yv=100;
int earr[][]=new int[701][1002];
static int pacMouth=1;
char dir='a';
Frame frame;
Button b;
Button b2;
reuse(int round){
this.round=round;
b=new Button("Reset");
b2=new Button("Exit");
frame=new Frame(){
public void paint(Graphics g)
{ g.setColor(Color.white);
g.fillOval(xv,yv,25,25);
g.setColor(Color.green);
g.fillOval(xv2,yv2,25,25);
g.setColor(Color.black);
g.fillOval(xv3,yv3,25,25);
// g.fillOval(xv4,yv4,25,25);
if(pacMouth==1)pacMouth=0;
else if(pacMouth==0)pacMouth=1;//bitting system
g.setColor(Color.yellow);
if(pacMouth==0)//bite
switch (dir){
case 'd':g.fillArc(X,Y,25,25,35,317);break;
case 'a':g.fillArc(X,Y,25,25,219,317);break;
case 'w':g.fillArc(X,Y,25,25,129,325);break;
case 's':g.fillArc(X,Y,25,25,309,325);break;
default :g.fillArc(X,Y,25,25,35,325);
}
else if(pacMouth==1)
switch (dir){
case 'd':g.fillArc(X,Y,25,25,15,337);break;
case 'a':g.fillArc(X,Y,25,25,219,337);break;
case 'w':g.fillArc(X,Y,25,25,109,337);break;
case 's':g.fillArc(X,Y,25,25,287,337);break;
default :g.fillArc(X,Y,25,25,15,337);
}
int odd=0;
wall walls=new wall(g,oc);
if(round==1){
for(int let1=70;let1<800;let1+=220)
for(int let2=120;let2<650;let2+=60)
walls.dwall(let1,let2,170,'d');
} else if(round==2){
for(int let1=170;let1<1000;let1+=220)
for(int let2=120;let2<400;let2+=60)
walls.dwall(let1,let2,170,'s');
}
g.setColor(Color.BLACK);
for(int j=0;j<=1000;j++)
for(int k=0;k<=700;k++)
if(oc.ck(j,k)==2){g.fillOval(j,k,3,3);}
//border
walls.dwall(0,0,700,'s');
walls.dwall(0,30,1000,'d');
walls.dwall(970,0,700,'s');
walls.dwall(0,30,1000,'d');
walls.dwall(0,670,980,'d');
//border end
g.setColor(Color.yellow);
g.drawString("Points =>"+Point,900,70);
g.drawString("Speed =>"+speed,900,85);
}
};
b.setBounds(450,300,100,100);
b2.setBounds(450,400,100,40);
b2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){System.exit(0);}
}
);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{frame.setVisible(false);
new reuse(round);}
});
frame.setSize(1000,700);
b.setVisible(false);
b2.setVisible(false);
frame.setBackground(Color.GRAY);
for(int i=30;i<1000;i+=40)
for(int j=45;j<700;j+=60)
{ oc.dot(i,j); }
frame.addKeyListener(this);
frame.add(b);
frame.add(b2);
frame.setVisible(true);
pc pac=new pc();
// pac.setPriority(7);
pac.start();
t1 t11=new t1();
t11.start();
t2 t22=new t2();
t22.start();
t3 t33=new t3();
t33.start();
Re r=new Re();
r.start();
Marks mar=new Marks();
mar.start();
}
public void keyPressed(KeyEvent ke){}
public void keyTyped(KeyEvent ke){
if(gameState)
dir=ke.getKeyChar();
}
public void keyReleased(KeyEvent ke){
if(Point==240){
frame.setVisible(false);
new reuse(2);
}
if(27==ke.getKeyCode()){
System.exit(0);
}}
char dirv='s';
char dirv3='s';
/*
public boolean ckCollision(int X,int Y){
if(oc.ck(X,Y)==1)return true;
else return false;
}*/
int xv3=50;
int yv3=600;
class pc extends Thread{
public void run(){
while(gameState){
try {
Thread.sleep(7);
} catch(Exception e) {
}
if(dir=='d'){X+=1;
{if(oc.ck(X+25,Y+12)==1||oc.ck(X+25,Y)==1||oc.ck(X+25,Y+25)==1)
{X-=1;}
}
}// problem is'nt here //checked "for sure"
else if(dir=='a')
{X-=1;
{if(oc.ck(X,Y+12)==1||oc.ck(X,Y)==1||oc.ck(X,Y+25)==1)
{X+=1;}
}
}
else if(dir=='s')
{Y+=1;
{if(oc.ck(X+25,Y+25)==1||oc.ck(X,Y+25)==1||oc.ck(X+12,Y+25)==1)
{Y-=1;}
}
}
else if(dir=='w')
{Y-=1;
{if(oc.ck(X,Y)==1||oc.ck(X+12,Y)==1||oc.ck(X+25,Y)==1)
{Y+=1;}
}
}
for(int i=0;i<25;i++)//running a loop around the pac
for(int j=0;j<25;j++){
int locX=X; int locY=Y;
if(oc.ck(locX+i,locY+j)==2){
Point++;
// System.out.println("Points=>> "+Point);
oc.eatenDot(locX+i,locY+j);
}
}
}
}}
public boolean ck(int x,int y){
if(oc.ck(x,y)==1)return true;
else return false;
}
public void dirSwap(){
if(dirv=='a')dirv='d';
else if(dirv=='d')dirv='a';
else if(dirv=='w')dirv='s';
else if(dirv=='s')dirv='w';
}
public void move (){
if(dirv=='d'){xv++;
if(ck(xv+25,yv)||ck(xv+25,yv+12)||ck(xv+25,yv+25)){xv--;dirSwap();}
}
else if(dirv=='a'){xv--;
if(ck(xv,yv)||ck(xv,yv+12)||ck(xv,yv+25)){xv++;dirSwap();}
}
else if(dirv=='s'){yv++;
if(ck(xv,yv+25)||ck(xv+12,yv+25)||ck(xv+25,yv+25)){yv--;dirSwap();}
}
else if(dirv=='w'){yv--;}
if(ck(xv,yv)||ck(xv,yv+25)||ck(xv,yv+12)){yv++;dirSwap();}
}
public void move2(){
if(dirv2=='d'){xv2++;
if(ck(xv2+25,yv2)||ck(xv2+25,yv2+12)||ck(xv2+25,yv2+25)){xv2--;dirSwap2();}
}
else if(dirv2=='a'){xv2--;
if(ck(xv2,yv2)||ck(xv2,yv2+12)||ck(xv2,yv2+25)){xv2++;dirSwap2();}
}
else if(dirv2=='s'){yv2++;
if(ck(xv2,yv2+25)||ck(xv2+12,yv2+25)||ck(xv2+25,yv2+25)){yv2--;dirSwap2();}
}
else if(dirv2=='w'){yv2--;}
if(ck(xv2,yv2)||ck(xv2,yv2+25)||ck(xv2,yv2+12)){yv2++;dirSwap2();}
}
public void dirSwap2(){
if(dirv2=='a')dirv2='d';
else if(dirv2=='d')dirv2='a';
else if(dirv2=='w')dirv2='s';
else if(dirv2=='s')dirv2='w';
}
public void colCk(){
for(int i=xv;i<xv+25;i++)
for(int j=yv;j<yv+25;j++){
for(int k=X;k<X+25;k++)
for(int l=Y;l<Y+25;l++){
if(i==k&&j==l){gameState=false;
b.setVisible(true);
b2.setVisible(true);
break;}
}
}
}
public void colCk2(){
for(int i=xv2;i<xv2+25;i++)
for(int j=yv2;j<yv2+25;j++){
for(int k=X;k<X+25;k++)
for(int l=Y;l<Y+25;l++){
if(i==k&&j==l){gameState=false;
b.setVisible(true);
b2.setVisible(true);
break;}
}
}
}
public void move3(){
if(dirv3=='d'){xv3++;
if(ck(xv3+25,yv3)||ck(xv3+25,yv3+12)||ck(xv3+25,yv3+25)){xv3--;dirSwap3();}
}
else if(dirv3=='a'){xv3--;
if(ck(xv3,yv3)||ck(xv3,yv3+12)||ck(xv3,yv3+25)){xv3++;dirSwap3();}
}
else if(dirv3=='s'){yv3++;
if(ck(xv3,yv3+25)||ck(xv3+12,yv3+25)||ck(xv3+25,yv3+25)){yv3--;dirSwap3();}
}
else if(dirv3=='w'){yv3--;}
if(ck(xv3,yv3)||ck(xv3,yv3+25)||ck(xv3,yv3+12)){yv3++;dirSwap3();}
}
public void dirSwap3(){
if(dirv3=='a')dirv3='d';
else if(dirv3=='d')dirv3='a';
else if(dirv3=='w')dirv3='s';
else if(dirv3=='s')dirv3='w';
}
public void colCk3(){
for(int i=xv3;i<xv3+25;i++)
for(int j=yv3;j<yv3+25;j++){
for(int k=X;k<X+25;k++)
for(int l=Y;l<Y+25;l++){
if(i==k&&j==l){gameState=false;
b.setVisible(true);
b2.setVisible(true);
break;}
}
}
}
int no=0;
class t1 extends Thread{
public void run(){int difX;int difY;
while(gameState){if(no%2==0){
no=0;}
move();
if(oc.ck(xv,yv)==1){
if(dirv=='d')dirv='a';
else if(dirv=='a')dirv='d';
else if(dirv=='w')dirv='s';
else if(dirv=='s')dirv='w';
}//Collision with a wall for the ghost
try {
Thread.sleep(8);
} catch(Exception e) {}//sleep for 10 ms
difX=X-xv;//xv and yv are the CordOfVil
difY=Y-yv;
if(X==xv){
if(difY<0){dirv='w';}
else if(difY>0){dirv='s';}
}//searching for pac and following
else if(Y==yv)
{
if(difX<0){dirv='a';}
else if(difX>0){dirv='d';}
}//searching for pac and following
if(difX<50&&difY<50)//if ghost and pac are near by start looping around them
{
colCk();
}
no++;
}}}
int noo=0;
int xv2=100;
int yv2=80;
char dirv2='d';
class t2 extends Thread{
public void run(){
while (gameState) {if(noo%2==0){
noo=0;}
move2();
if(oc.ck(xv2,yv2)==1){
if(dirv2=='d')dirv2='a';
else if(dirv2=='a')dirv2='d';
else if(dirv2=='w')dirv2='s';
else if(dirv2=='s')dirv2='w';
}//Collision with a wall for the ghost
try {
Thread.sleep(9);
} catch(Exception e) {}//sleep for 10 ms
int difX=X-xv2;//xv2 and yv2 are the CordOfVil
int difY=Y-yv2;
if(X==xv2){
if(difY<0){dirv2='w';}
else if(difY>0){dirv2='s';}
}//searching for pac and following
else if(Y==yv2)
{
if(difX<0){dirv2='a';}
else if(difX>0){dirv2='d';}
}//searching for pac and following
if(difX<50&&difY<50)//if ghost and pac are near by start looping around them
{
colCk2();
}
noo++;
}}}
class t3 extends Thread{
public void run(){
while (gameState) {if(noo%2==0){
noo=0;}
move3();
if(oc.ck(xv3,yv3)==1){
if(dirv3=='d')dirv3='a';
else if(dirv3=='a')dirv3='d';
else if(dirv3=='w')dirv3='s';
else if(dirv3=='s')dirv3='w';
}//Collision with a wall for the ghost
try {
Thread.sleep(9);
} catch(Exception e) {}//sleep for 10 ms
int difX=X-xv3;//xv3 and yv3 are the CordOfVil
int difY=Y-yv3;
if(X==xv3){
if(difY<0){dirv3='w';}
else if(difY>0){dirv3='s';}
}//searching for pac and following
else if(Y==yv3)
{
if(difX<0){dirv3='a';}
else if(difX>0){dirv3='d';}
}//searching for pac and following
if(difX<50&&difY<50)//if ghost and pac are near by start looping around them
{
colCk3();
}
noo++;
}}}
int re=0;
class Re extends Thread{
public void run(){
while(gameState){re++;
try {
Thread.sleep(10);
} catch(Exception e) {}
if(re==1){
frame.repaint(X-7,Y-7,43,43);
}
else if(re==2){
frame.repaint(xv-6,yv-6,41,41);
}
else if(re==3){
frame.repaint(xv2-6,yv2-6,41,41);
}
else if(re==4){
frame.repaint(xv3-6,yv3-6,41,41);
re=0;
}
}
}
}
class Marks extends Thread{
public void run(){
setPriority(3);
while(gameState){
try {
Thread.sleep(1000);
} catch(Exception e) {
}
frame.repaint(890,50,85,50);
}}
}
}