Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1664 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Giuseppe Lipari       <lipari@sssup.it>
7
 *   Paolo Gai               <pj@gandalf.sssup.it>
8
 *
9
 * Authors     :
10
 *   Kabilan Sukumar      <kabbys2@yahoo.com>
11
 *   Rajenish Kumar jain  <rajenish_jain@yahoo.com>
12
 *   Deepaknath T K       <deepaknathtk@yahoo.com>
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
 
22
/*
23
 * Copyright (C) 2002      Kabilan Sukumar,  Rajenish Kumar jain, Deepaknath T K
24
 *
25
 * This program is free software; you can redistribute it and/or modify
26
 * it under the terms of the GNU General Public License as published by
27
 * the Free Software Foundation; either version 2 of the License, or
28
 * (at your option) any later version.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with this program; if not, write to the Free Software
37
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38
 *
39
 */
40
 
41
/*--------------------------------------------------------------*/
42
/*		THE PING PONG GAME                              */
43
/*--------------------------------------------------------------*/
44
 
45
#include "demo.h"
46
#include <kernel/func.h>
47
#include <stdlib.h>
48
#include <drivers/keyb.h>
49
 
50
# define R  4        /* the radius of the ball */
51
 
52
 
53
int ballexit=0;
54
int horizgo;         /*when set increases the horizontal speed of the ball*/
55
int vertigo;         /*when set increases the horizontal speed of the ball*/
56
int batflagP1=0;
57
int batflagP2=0;
58
KEY_EVT keypressP1;
59
KEY_EVT keypressP2;
60
char points[3];
61
int jump;             /*when set makes the bat move faster*/
62
 
63
 
64
struct ballData
65
{
66
	/*Coordinates of centre for ball*/
67
	int xcur;
68
	int ycur;
69
        /*to store the directions of the ball*/
70
	int xdir;
71
	int ydir;
72
 
73
        int path_change;
74
 
75
}ball;
76
 
77
 
78
struct batData
79
{
80
               /*Coordinates of centre for bat*/
81
	int x;
82
	int y;
83
	int points;
84
}p1,p2;
85
 
86
 
87
char dir;    /*to store the direction of movement of bats*/
88
 
89
int gamefinish=0; /* indicates the end of the game */
90
 
91
 
92
/* to print the score on the screen */
93
void score_increase(char temp[3], int x1, int y1)
94
{
95
     grx_box(x1+10,y1+2,x1+60,y1+18,black);
96
 
97
     grx_text(temp,x1+30,y1-4,rgb16(0,200,0),black);
98
}
99
 
100
/* the function to take care of the score increase and determining the end of game */
101
void score_keeper()
102
{
103
     int i=0;
104
     /*ball hits upper boundry*/
105
     if( ball.ycur == 23)
106
     {
107
	  p1.points = p1.points + 1;
108
	  itoa(p1.points, points);
109
	  score_increase(points,435,120);
110
     }
111
     else if( ball.ycur == 453 )/*If ball hits lower boundry*/
112
     {
113
	  p2.points = p2.points + 1;
114
	  itoa(p2.points, points);
115
	  score_increase(points,520,180);
116
     }
117
 
118
     if( p1.points == 5 )
119
     {
120
 
121
          grx_text("!!!!! PLAYER TWO WINS, CONGRATULATIONS !!!!!",20,200,green,black);
122
          grx_text("Press ESC to exit to menu",70,250,green,black);
123
          gamefinish=1; // indicates that the game is over
124
     }
125
     if(p2.points== 5)
126
     {
127
          grx_text("!!!!! PLAYER ONE WINS, CONGRATULATIONS !!!!!",20,200,green,black);
128
          grx_text("Press ESC to exit to menu",70,250,green,black);
129
          gamefinish=1; // indicates that the game is over
130
     }
131
 
132
}
133
 
134
/* The ball thread which keeps tracking the new position of the ball and moves the ball */
135
TASK balls(int i)
136
{
137
     while(1){
138
        /*Delete the ball at previous position*/
139
      grx_disc(ball.xcur,ball.ycur,R,black);
140
      if(escapeflag||gamefinish)return 0;
141
 
142
      if( grx_getpixel(ball.xcur-7,ball.ycur) != black ) /*Left hand sensor*/
143
      {
144
	  ball.path_change = 1;
145
	  ball.xdir = 1;
146
      }
147
      else if( grx_getpixel(ball.xcur+7,ball.ycur) != black ) /*Right hand sensor*/
148
      {
149
	  ball.path_change = 1;
150
	  ball.xdir = 0;
151
      }
152
      if( grx_getpixel(ball.xcur,ball.ycur-8) !=black ) /*Top sensor*/
153
      {
154
	  vertigo = 0;
155
	  horizgo = 0;
156
 
157
	  ball.path_change = 1;
158
	  ball.ydir = 1;
159
 
160
 
161
          if((grx_getpixel(ball.xcur,ball.ycur-5) == white ) && ( p2.x - ball.xcur > 20 ) && ( ball.xdir == 1 ) )  if( (grx_getpixel(ball.xcur,ball.ycur-5) == white) && ( p2.x - ball.xcur > 20 ) && ( ball.xdir == 1 ) )
162
          {
163
	       ball.xdir = 0;
164
	  }
165
	  else if( (grx_getpixel(ball.xcur,ball.ycur-5) == black) && (abs(p2.x-ball.xcur)<10))
166
	  {
167
	       vertigo = 1;
168
	  }
169
	  else if( (grx_getpixel(ball.xcur,ball.ycur-5) == white) && ( ball.xcur - p2.x > 20 ) && ( ball.xdir == 0 ) )
170
	  {
171
	       ball.xdir = 1;
172
	  }
173
	  else if( (grx_getpixel(ball.xcur,ball.ycur-5) == white) && ( p2.x - ball.xcur > 20 ) && ( ball.xdir == 0 ) )
174
	  {
175
	       horizgo = 1;
176
	  }
177
	  else if( (grx_getpixel(ball.xcur,ball.ycur-5) == white) && ( ball.xcur - p2.x > 20 ) && ( ball.xdir == 1 ) )
178
	  {
179
 
180
	       horizgo = 1;
181
	  }
182
      }
183
      if( grx_getpixel(ball.xcur,ball.ycur+7) !=black ) /*Bottom sensor*/
184
      {
185
	  vertigo = 0;
186
	  horizgo = 0;
187
 
188
	  ball.path_change = 1;
189
	  ball.ydir = 0;
190
 
191
 
192
	 if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && ( p1.x - ball.xcur > 20 ) && ( ball.xdir == 1 ) )
193
	  {
194
	       ball.xdir = 0;
195
	  }
196
	  else if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && (abs(p1.x-ball.xcur)<10) )
197
	  {
198
	       vertigo = 1;
199
	  }
200
	  else if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && ( ball.xcur - p1.x > 20 ) && ( ball.xdir == 0 ) )
201
	  {
202
	       ball.xdir = 1;
203
	  }
204
	  else if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && ( p1.x - ball.xcur > 20 ) && ( ball.xdir == 0 ) )
205
	  {
206
	       horizgo = 1;
207
	  }
208
	  else if( (grx_getpixel(ball.xcur,ball.ycur+5) == white) && ( ball.xcur - p1.x > 20 ) && ( ball.xdir == 1 ) )
209
	  {
210
	       horizgo = 1;
211
	  }
212
     }
213
 
214
     score_keeper(); /* call the score keeper to update the scores */
215
 
216
     /*calculate the direction of the ball*/
217
     if ( (vertigo == 0) && (horizgo == 0) )
218
     {
219
	  if(ball.xdir == 1) ball.xcur=ball.xcur+2; else ball.xcur=ball.xcur-2;
220
	  if(ball.ydir == 1) ball.ycur=ball.ycur+2; else ball.ycur=ball.ycur-2;
221
     }
222
     else if (vertigo == 1)
223
     {
224
	  if(ball.xdir == 1) ball.xcur=ball.xcur+2; else ball.xcur=ball.xcur-2;
225
	  if(ball.ydir == 1) ball.ycur=ball.ycur+3; else ball.ycur=ball.ycur-3;
226
     }
227
     else if (horizgo == 1)
228
     {
229
	  if(ball.xdir == 1) ball.xcur=ball.xcur+3; else ball.xcur=ball.xcur-3;
230
	  if(ball.ydir == 1) ball.ycur=ball.ycur+2; else ball.ycur=ball.ycur-2;
231
     }
232
 
233
     /*draw the ball at new position*/
234
 
235
     grx_disc(ball.xcur,ball.ycur,R,white);
236
 
237
 
238
 
239
    task_endcycle();
240
   }
241
 
242
}
243
 
244
/* called if the escape key is pressed during the game, it sets the flag */
245
void EscapeToMenu(KEY_EVT *k){
246
      escapeflag=1;
247
}
248
 
249
/* the bat thread for player 1. */
250
/* it controls the movement of the bat by reading the keystrokes of the player */
251
TASK movebatP1(int k){
252
 
253
     while(1){
254
       if(escapeflag||gamefinish){
255
          return 0;
256
       }
257
 
258
       if((getKeyFlag()&CNTL_BIT)||getKeyFlag()&SHFL_BIT){
259
         /*delete the bat at old position*/
260
         grx_box( p1.x-30, p1.y, p1.x+30, p1.y+5,black);
261
         if( (getKeyFlag()&CNTL_BIT) && (p1.x > 42) )          /*moves the bat to the left*/
262
         {
263
	         p1.x-=1;
264
         }
265
         else if( (getKeyFlag()&SHFL_BIT) && (p1.x < 343) )   /*moves the bat to the right*/
266
         {
267
	        p1.x+=1;
268
         }
269
 
270
         /*draw the bat at the new position*/
271
         grx_box(p1.x-30, p1.y, p1.x+30, p1.y+5,blue);
272
         batflagP1=0;
273
       }
274
     task_endcycle();
275
    }
276
}
277
 
278
/* the bat thread for player 2. */
279
/* it controls the movement of the bat by reading the keystrokes of the player */
280
TASK movebatP2(int k){
281
     /*delete the bat at old position*/
282
     while(1){
283
        if(escapeflag||gamefinish){
284
         return 0;
285
        }
286
       if((getKeyFlag()&CNTR_BIT)||(getKeyFlag()&SHFR_BIT)){
287
        grx_box( p2.x-30, p2.y, p2.x+30, p2.y+5,black);
288
        if( (getKeyFlag()&CNTR_BIT) && (p2.x > 42) )       /*moves the bat to the left*/
289
        {
290
	        p2.x-=1;
291
        }
292
        else if((getKeyFlag()&SHFR_BIT)&&(p2.x < 343))     /*moves the bat to the right*/
293
         {
294
	        p2.x+=1;
295
         }
296
 
297
         /*draw the bat at the new position*/
298
         grx_box( p2.x-30, p2.y, p2.x+30, p2.y+5,yellow);
299
         batflagP2=0;
300
       }
301
       task_endcycle();
302
     }
303
}
304
 
305
/* the cpu player to be used in single player game*/
306
TASK movebatCPU(){
307
     while(1){
308
          /*delete the bat at old position*/
309
         if(escapeflag||gamefinish){
310
           return 0;
311
          }
312
	  grx_box(p2.x-30, p2.y, p2.x+30, p2.y+5,black);
313
 
314
	  if( (ball.xdir == 1) && (p2.x < 345) && (p2.x-ball.xcur < 15) && (ball.ydir == 0) )
315
	  {
316
	       p2.x = p2.x + jump;
317
	  }
318
	  else if( (ball.xdir == 0) && (p2.x > 45) && (ball.xcur-p2.x < 15) && (ball.ydir == 0) )
319
	  {
320
	       p2.x = p2.x - jump;
321
	  }
322
 
323
	  grx_box(p2.x-30, p2.y, p2.x+30, p2.y+5,yellow);
324
          task_endcycle();
325
      }
326
 
327
}
328
 
329
/*Creates a hard task for the player p1*/
330
void hardbatP1()
331
{
332
  HARD_TASK_MODEL mp;
333
  PID pid;
334
 
335
  hard_task_default_model(mp);
336
  hard_task_def_ctrl_jet(mp);
337
  hard_task_def_arg(mp,NULL);
338
  hard_task_def_wcet(mp,500);
339
  hard_task_def_mit(mp,5000);
340
  hard_task_def_usemath(mp);
341
  pid= task_create("batEDF1", movebatP1, &mp, NULL);
342
  if (pid== NIL) {
343
        grx_close();
344
	perror("Could not create task <batEDF1>");
345
	sys_end();
346
  }
347
  else
348
    task_activate(pid);
349
}
350
 
351
/*Creates a hard task for the player p2*/
352
void hardbatP2(){
353
 
354
  HARD_TASK_MODEL mp;
355
  PID pid;
356
 
357
  hard_task_default_model(mp);
358
  hard_task_def_ctrl_jet(mp);
359
  hard_task_def_arg(mp,NULL);
360
  hard_task_def_wcet(mp, 500);
361
  hard_task_def_mit(mp, 5000);
362
  hard_task_def_usemath(mp);
363
  pid = task_create("batEDF2", movebatP2, &mp, NULL);
364
  if (pid == NIL) {
365
        grx_close();
366
	perror("Could not create task <batEDF2>");
367
	sys_end();
368
  }
369
  else
370
    task_activate(pid);
371
 
372
}
373
 
374
/*Creates a hard task for the ball*/
375
void hardball()
376
{
377
 
378
  HARD_TASK_MODEL mp;
379
  PID pid;
380
 
381
  hard_task_default_model(mp);
382
  hard_task_def_ctrl_jet(mp);
383
  hard_task_def_arg(mp,NULL);
384
  hard_task_def_wcet(mp, 500);
385
  hard_task_def_mit(mp,PERIOD_BALL);
386
  hard_task_def_usemath(mp);
387
  pid = task_create("ballEDF", balls, &mp, NULL);
388
  if (pid == NIL) {
389
        grx_close();
390
	perror("Could not create task <ballEDF>");
391
	sys_end();
392
  }
393
  else
394
    task_activate(pid);
395
 
396
}
397
 
398
/*Creates a hard task for the cpu bat*/
399
void cpubat(){
400
  HARD_TASK_MODEL mp;
401
  PID pid;
402
 
403
  hard_task_default_model(mp);
404
  hard_task_def_ctrl_jet(mp);
405
  hard_task_def_arg(mp,NULL);
406
  hard_task_def_wcet(mp, 500);
407
  hard_task_def_mit(mp, 10000);
408
  hard_task_def_usemath(mp);
409
  pid = task_create("cpuBatEDF1", movebatCPU, &mp, NULL);
410
  if (pid == NIL) {
411
        grx_close();
412
	perror("Could not create task <cpuBatEDF1>");
413
	sys_end();
414
  }
415
  else
416
    task_activate(pid);
417
}
418
 
419
 
420
/*--------------------------------------------------------------*/
421
/*			The Game initializations 		*/
422
/*--------------------------------------------------------------*/
423
 
424
void scenario_game()
425
{
426
  int i;
427
  ball.xcur = 185; /*place ball in the horizontal centre of screen*/
428
 
429
  /*randomly choose the y cordinate of the ball
430
  this makes the game less predictable*/
431
 
432
  ball.ycur = myrand(400)+30;
433
  ball.xdir = myrand(1);	/*randomly choose the direction of ball*/
434
 
435
  ball.path_change = 0;
436
 
437
  dir = '\0';
438
 
439
  p1.points = 0; p2.points = 0;
440
  jump=2;
441
 
442
  for(i=0; i<=3; i++)
443
  {
444
        points[i] = '\0';
445
  }
446
 
447
  mutex_lock(&mutex);
448
  p1.x = 185; p1.y = 452;
449
  p2.x = 185; p2.y = 18;
450
 
451
/* This draws the two bats at their initial positions */
452
  grx_box(155,452,215,457,blue);
453
  grx_box(155,18,215,23,yellow);
454
  mutex_unlock(&mutex);
455
 
456
  ball.xcur=185;
457
  ball.ycur=185;
458
  mutex_lock(&mutex);
459
  grx_disc(ball.xcur, ball.ycur, R, white);
460
  mutex_unlock(&mutex);
461
}
462
 
463
 
464
void init_ball(int choice)
465
{
466
    KEY_EVT k;
467
 
468
    hardball();
469
    hardbatP1();
470
    if(choice==2)
471
        hardbatP2();
472
    else if(choice==1)
473
         cpubat();
474
 
475
}
476
 
477
 
478
 
479
/*--------------------------------------------------------------*/