diff options
author | Teruaki Kawashima <teru@rockbox.org> | 2011-01-20 13:37:20 +0000 |
---|---|---|
committer | Teruaki Kawashima <teru@rockbox.org> | 2011-01-20 13:37:20 +0000 |
commit | ee6ea59c764e023c4cf63e1ca9eac8a62cf4a2b4 (patch) | |
tree | 6338ee59aacaa9a0f2ce829d5665127c81f65cca | |
parent | 1b779e0461f831631681bb16fecc4cfb7175de5a (diff) | |
download | rockbox-ee6ea59c764e023c4cf63e1ca9eac8a62cf4a2b4.tar.gz rockbox-ee6ea59c764e023c4cf63e1ca9eac8a62cf4a2b4.zip |
pong: make related variables a structure. Correct collision detecting. Disable turn off backlight.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29095 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/plugins/pong.c | 262 |
1 files changed, 144 insertions, 118 deletions
diff --git a/apps/plugins/pong.c b/apps/plugins/pong.c index 49fc8e1468..9394b7a857 100644 --- a/apps/plugins/pong.c +++ b/apps/plugins/pong.c | |||
@@ -19,6 +19,7 @@ | |||
19 | * | 19 | * |
20 | ****************************************************************************/ | 20 | ****************************************************************************/ |
21 | #include "plugin.h" | 21 | #include "plugin.h" |
22 | #include "lib/helper.h" | ||
22 | 23 | ||
23 | #define PAD_HEIGHT LCD_HEIGHT / 6 /* Recorder: 10 iRiver: 21 */ | 24 | #define PAD_HEIGHT LCD_HEIGHT / 6 /* Recorder: 10 iRiver: 21 */ |
24 | #define PAD_WIDTH LCD_WIDTH / 50 /* Recorder: 2 iRiver: 2 */ | 25 | #define PAD_WIDTH LCD_WIDTH / 50 /* Recorder: 2 iRiver: 2 */ |
@@ -28,8 +29,10 @@ | |||
28 | 29 | ||
29 | #define SPEEDX ( LCD_WIDTH * 3 ) / 2 /* Recorder: 168 iRiver: 240 */ | 30 | #define SPEEDX ( LCD_WIDTH * 3 ) / 2 /* Recorder: 168 iRiver: 240 */ |
30 | #define SPEEDY LCD_HEIGHT * 2 /* Recorder: 128 iRiver: 256 */ | 31 | #define SPEEDY LCD_HEIGHT * 2 /* Recorder: 128 iRiver: 256 */ |
31 | #define CPU_PLAYER_DIST ( (LCD_WIDTH/8 ) * 5 ) /* This is the width of the dead spot where the */ | 32 | /* This is the width of the dead spot where the |
32 | #define DEM_PLAYER_DIST ( (LCD_WIDTH/8 ) * 3 ) /* cpu player doesnt care about the ball -- 3/8 of the screen */ | 33 | * cpu player doesnt care about the ball -- 3/8 of the screen */ |
34 | #define CPU_PLAYER_RIGHT_DIST ( (LCD_WIDTH/8 ) * 5 ) | ||
35 | #define CPU_PLAYER_LEFT_DIST ( (LCD_WIDTH/8 ) * 3 ) | ||
33 | 36 | ||
34 | #define RES 100 | 37 | #define RES 100 |
35 | 38 | ||
@@ -258,15 +261,24 @@ CONFIG_KEYPAD == MROBE500_PAD | |||
258 | #endif | 261 | #endif |
259 | #endif | 262 | #endif |
260 | 263 | ||
264 | struct player { | ||
265 | int xpos; /* X position of pad */ | ||
266 | int w_pad; /* wanted current Y position of pad */ | ||
267 | int e_pad; /* existing current Y position of pad */ | ||
268 | int score; | ||
269 | bool iscpu; /* Status of AI player */ | ||
270 | }; | ||
271 | |||
272 | struct ball { | ||
273 | int x; /* current X*RES position of the ball */ | ||
274 | int y; /* current Y*RES position of the ball */ | ||
275 | int speedx; /* */ | ||
276 | int speedy; /* */ | ||
277 | }; | ||
278 | |||
261 | struct pong { | 279 | struct pong { |
262 | int ballx; /* current X*RES position of the ball */ | 280 | struct ball ball; |
263 | int bally; /* current Y*RES position of the ball */ | 281 | struct player player[2]; |
264 | int w_pad[2]; /* wanted current Y positions of pads */ | ||
265 | int e_pad[2]; /* existing current Y positions of pads */ | ||
266 | int ballspeedx; /* */ | ||
267 | int ballspeedy; /* */ | ||
268 | int score[2]; | ||
269 | bool cpu_player[2]; /* Status of AI players */ | ||
270 | }; | 282 | }; |
271 | 283 | ||
272 | void singlepad(int x, int y, int set) | 284 | void singlepad(int x, int y, int set) |
@@ -281,17 +293,17 @@ void singlepad(int x, int y, int set) | |||
281 | } | 293 | } |
282 | } | 294 | } |
283 | 295 | ||
284 | static int xpos[2]={0, LCD_WIDTH-PAD_WIDTH}; | ||
285 | void pad(struct pong *p, int pad) | 296 | void pad(struct pong *p, int pad) |
286 | { | 297 | { |
298 | struct player *player = &p->player[pad]; | ||
287 | /* clear existing pad */ | 299 | /* clear existing pad */ |
288 | singlepad(xpos[pad], p->e_pad[pad], 0); | 300 | singlepad(player->xpos, player->e_pad, 0); |
289 | 301 | ||
290 | /* draw wanted pad */ | 302 | /* draw wanted pad */ |
291 | singlepad(xpos[pad], p->w_pad[pad], 1); | 303 | singlepad(player->xpos, player->w_pad, 1); |
292 | 304 | ||
293 | /* existing is now the wanted */ | 305 | /* existing is now the wanted */ |
294 | p->e_pad[pad] = p->w_pad[pad]; | 306 | player->e_pad = player->w_pad; |
295 | } | 307 | } |
296 | 308 | ||
297 | bool wallcollide(struct pong *p, int pad) | 309 | bool wallcollide(struct pong *p, int pad) |
@@ -300,11 +312,11 @@ bool wallcollide(struct pong *p, int pad) | |||
300 | the wall */ | 312 | the wall */ |
301 | if(pad) { | 313 | if(pad) { |
302 | /* right-side */ | 314 | /* right-side */ |
303 | if(p->ballx > ( LCD_WIDTH*RES ) - PAD_WIDTH ) | 315 | if(p->ball.x > ( LCD_WIDTH*RES ) - PAD_WIDTH ) |
304 | return true; | 316 | return true; |
305 | } | 317 | } |
306 | else { | 318 | else { |
307 | if(p->ballx < PAD_WIDTH) | 319 | if(p->ball.x + ( BALL_WIDTH*RES ) < PAD_WIDTH ) |
308 | return true; | 320 | return true; |
309 | } | 321 | } |
310 | return false; | 322 | return false; |
@@ -315,11 +327,12 @@ bool wallcollide(struct pong *p, int pad) | |||
315 | 327 | ||
316 | bool padcollide(struct pong *p, int pad, int *info) | 328 | bool padcollide(struct pong *p, int pad, int *info) |
317 | { | 329 | { |
318 | int x = p->ballx/RES; | 330 | struct player *player = &p->player[pad]; |
319 | int y = p->bally/RES; | 331 | int x = p->ball.x/RES; |
332 | int y = p->ball.y/RES; | ||
320 | 333 | ||
321 | if((y < (p->e_pad[pad]+PAD_HEIGHT)) && | 334 | if((y < (player->e_pad+PAD_HEIGHT)) && |
322 | (y + BALL_HEIGHT > p->e_pad[pad])) { | 335 | (y + BALL_HEIGHT > player->e_pad)) { |
323 | /* Y seems likely right */ | 336 | /* Y seems likely right */ |
324 | 337 | ||
325 | /* store the delta between ball-middle MINUS pad-middle, so | 338 | /* store the delta between ball-middle MINUS pad-middle, so |
@@ -331,7 +344,7 @@ bool padcollide(struct pong *p, int pad, int *info) | |||
331 | max number is +- PAD_HEIGHT/2 | 344 | max number is +- PAD_HEIGHT/2 |
332 | */ | 345 | */ |
333 | 346 | ||
334 | *info = (y+BALL_HEIGHT/2) - (p->e_pad[pad] + PAD_HEIGHT/2); | 347 | *info = (y+BALL_HEIGHT/2) - (player->e_pad + PAD_HEIGHT/2); |
335 | 348 | ||
336 | if(pad) { | 349 | if(pad) { |
337 | /* right-side */ | 350 | /* right-side */ |
@@ -348,43 +361,45 @@ bool padcollide(struct pong *p, int pad, int *info) | |||
348 | 361 | ||
349 | void bounce(struct pong *p, int pad, int info) | 362 | void bounce(struct pong *p, int pad, int info) |
350 | { | 363 | { |
351 | p->ballspeedx = -p->ballspeedx; | 364 | p->ball.speedx = -p->ball.speedx; |
352 | 365 | ||
353 | if(pad==0) { /* Give ball a little push to keep it from getting stuck between wall and pad */ | 366 | /* Give ball a little push to keep it from getting stuck between wall and pad */ |
354 | p->ballx += PAD_WIDTH; | 367 | if(pad) { |
368 | /* right side */ | ||
369 | p->ball.x -= PAD_WIDTH*RES/4; | ||
355 | } | 370 | } |
356 | else { | 371 | else { |
357 | p->ballx -= PAD_WIDTH; | 372 | p->ball.x += PAD_WIDTH*RES/4; |
358 | } | 373 | } |
359 | 374 | ||
360 | /* info is the hit-angle into the pad */ | 375 | /* info is the hit-angle into the pad */ |
361 | if(p->ballspeedy > 0) { | 376 | if(p->ball.speedy > 0) { |
362 | /* downwards */ | 377 | /* downwards */ |
363 | if(info > 0) { | 378 | if(info > 0) { |
364 | /* below the middle of the pad */ | 379 | /* below the middle of the pad */ |
365 | p->ballspeedy += info * RES/3; | 380 | p->ball.speedy += info * RES/3; |
366 | } | 381 | } |
367 | else if(info < 0) { | 382 | else if(info < 0) { |
368 | /* above the middle */ | 383 | /* above the middle */ |
369 | p->ballspeedy = info * RES/2; | 384 | p->ball.speedy = info * RES/2; |
370 | } | 385 | } |
371 | } | 386 | } |
372 | else { | 387 | else { |
373 | /* upwards */ | 388 | /* upwards */ |
374 | if(info > 0) { | 389 | if(info > 0) { |
375 | /* below the middle of the pad */ | 390 | /* below the middle of the pad */ |
376 | p->ballspeedy = info * RES/2; | 391 | p->ball.speedy = info * RES/2; |
377 | } | 392 | } |
378 | else if(info < 0) { | 393 | else if(info < 0) { |
379 | /* above the middle */ | 394 | /* above the middle */ |
380 | p->ballspeedy -= info * RES/3; | 395 | p->ball.speedy += info * RES/3; |
381 | } | 396 | } |
382 | } | 397 | } |
383 | 398 | ||
384 | p->ballspeedy += rb->rand()%21-10; | 399 | p->ball.speedy += rb->rand()%21-10; |
385 | 400 | ||
386 | #if 0 | 401 | #if 0 |
387 | fprintf(stderr, "INFO: %d YSPEED: %d\n", info, p->ballspeedy); | 402 | fprintf(stderr, "INFO: %d YSPEED: %d\n", info, p->ball.speedy); |
388 | #endif | 403 | #endif |
389 | } | 404 | } |
390 | 405 | ||
@@ -395,29 +410,29 @@ void score(struct pong *p, int pad) | |||
395 | else | 410 | else |
396 | rb->splash(HZ/4, "left scores!"); | 411 | rb->splash(HZ/4, "left scores!"); |
397 | rb->lcd_clear_display(); | 412 | rb->lcd_clear_display(); |
398 | p->score[pad]++; | 413 | p->player[pad].score++; |
399 | 414 | ||
400 | /* then move the X-speed of the ball and give it a random Y position */ | 415 | /* then move the X-speed of the ball and give it a random Y position */ |
401 | p->ballspeedx = -p->ballspeedx; | 416 | p->ball.speedx = -p->ball.speedx; |
402 | p->bally = rb->rand()%(LCD_HEIGHT*RES - BALL_HEIGHT); | 417 | p->ball.y = rb->rand()%((LCD_HEIGHT-BALL_HEIGHT)*RES); |
403 | 418 | ||
404 | /* avoid hitting the pad with the new ball */ | 419 | /* avoid hitting the pad with the new ball */ |
405 | p->ballx = (p->ballx < 0) ? | 420 | p->ball.x = (p->ball.x < 0) ? |
406 | (RES * PAD_WIDTH) : (RES * (LCD_WIDTH - PAD_WIDTH - BALL_WIDTH)); | 421 | (RES * PAD_WIDTH) : (RES * (LCD_WIDTH - PAD_WIDTH - BALL_WIDTH)); |
407 | 422 | ||
408 | /* restore Y-speed to default */ | 423 | /* restore Y-speed to default */ |
409 | p->ballspeedy = (p->ballspeedy > 0) ? SPEEDY : -SPEEDY; | 424 | p->ball.speedy = (p->ball.speedy > 0) ? SPEEDY : -SPEEDY; |
410 | 425 | ||
411 | /* set the existing pad positions to something weird to force pad | 426 | /* set the existing pad positions to something weird to force pad |
412 | updates */ | 427 | updates */ |
413 | p->e_pad[0] = -1; | 428 | p->player[0].e_pad = -1; |
414 | p->e_pad[1] = -1; | 429 | p->player[1].e_pad = -1; |
415 | } | 430 | } |
416 | 431 | ||
417 | void ball(struct pong *p) | 432 | void ball(struct pong *p) |
418 | { | 433 | { |
419 | int x = p->ballx/RES; | 434 | int oldx = p->ball.x/RES; |
420 | int y = p->bally/RES; | 435 | int oldy = p->ball.y/RES; |
421 | 436 | ||
422 | int newx; | 437 | int newx; |
423 | int newy; | 438 | int newy; |
@@ -425,24 +440,22 @@ void ball(struct pong *p) | |||
425 | int info; | 440 | int info; |
426 | 441 | ||
427 | /* movement */ | 442 | /* movement */ |
428 | p->ballx += p->ballspeedx; | 443 | p->ball.x += p->ball.speedx; |
429 | p->bally += p->ballspeedy; | 444 | p->ball.y += p->ball.speedy; |
430 | 445 | ||
431 | newx = p->ballx/RES; | 446 | newx = p->ball.x/RES; |
432 | newy = p->bally/RES; | 447 | newy = p->ball.y/RES; |
433 | 448 | ||
434 | /* detect if ball hits a wall */ | 449 | /* detect if ball hits a wall */ |
435 | if(newy + BALL_HEIGHT > LCD_HEIGHT) { | 450 | if(newy + BALL_HEIGHT > LCD_HEIGHT) { |
436 | /* hit floor, bounce */ | 451 | /* hit floor, bounce */ |
437 | p->ballspeedy = -p->ballspeedy; | 452 | p->ball.speedy = -p->ball.speedy; |
438 | newy = LCD_HEIGHT - BALL_HEIGHT; | 453 | p->ball.y = (LCD_HEIGHT - BALL_HEIGHT) * RES; |
439 | p->bally = newy * RES; | ||
440 | } | 454 | } |
441 | else if(newy < 0) { | 455 | else if(newy < 0) { |
442 | /* hit ceiling, bounce */ | 456 | /* hit ceiling, bounce */ |
443 | p->ballspeedy = -p->ballspeedy; | 457 | p->ball.speedy = -p->ball.speedy; |
444 | p->bally = 0; | 458 | p->ball.y = 0; |
445 | newy = 0; | ||
446 | } | 459 | } |
447 | 460 | ||
448 | /* detect if ball hit pads */ | 461 | /* detect if ball hit pads */ |
@@ -455,12 +468,12 @@ void ball(struct pong *p) | |||
455 | else if(wallcollide(p, 1)) | 468 | else if(wallcollide(p, 1)) |
456 | score(p, 0); | 469 | score(p, 0); |
457 | 470 | ||
458 | newx = p->ballx/RES; | 471 | newx = p->ball.x/RES; |
459 | newy = p->bally/RES; | 472 | newy = p->ball.y/RES; |
460 | 473 | ||
461 | /* clear old position */ | 474 | /* clear old position */ |
462 | rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); | 475 | rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); |
463 | rb->lcd_fillrect(x, y, BALL_WIDTH, BALL_HEIGHT); | 476 | rb->lcd_fillrect(oldx, oldy, BALL_WIDTH, BALL_HEIGHT); |
464 | rb->lcd_set_drawmode(DRMODE_SOLID); | 477 | rb->lcd_set_drawmode(DRMODE_SOLID); |
465 | 478 | ||
466 | /* draw the new ball position */ | 479 | /* draw the new ball position */ |
@@ -476,6 +489,36 @@ void padmove(int *pos, int dir) | |||
476 | *pos = 0; | 489 | *pos = 0; |
477 | } | 490 | } |
478 | 491 | ||
492 | void key_pad(struct pong *p, int pad, int up, int down) | ||
493 | { | ||
494 | struct player *player = &p->player[pad]; | ||
495 | if(player->iscpu) { | ||
496 | if((pad && (p->ball.x/RES > CPU_PLAYER_RIGHT_DIST)) /* cpu right */ | ||
497 | || (!pad && (p->ball.x/RES < CPU_PLAYER_LEFT_DIST)) /* cpu left */) | ||
498 | { | ||
499 | if(p->ball.y/RES > player->w_pad) /* player goes down */ | ||
500 | padmove(&player->w_pad, MOVE_STEP); | ||
501 | |||
502 | if(p->ball.y/RES < player->w_pad) /* player goes up */ | ||
503 | padmove(&player->w_pad, -MOVE_STEP); | ||
504 | } | ||
505 | |||
506 | if(down || up) { | ||
507 | /* if player presses control keys stop cpu player */ | ||
508 | player->iscpu = false; | ||
509 | p->player[0].score = p->player[1].score = 0; /* reset the score */ | ||
510 | rb->lcd_clear_display(); /* get rid of the text */ | ||
511 | } | ||
512 | } | ||
513 | else { | ||
514 | if(down) /* player goes down */ | ||
515 | padmove(&player->w_pad, MOVE_STEP); | ||
516 | |||
517 | if(up) /* player goes up */ | ||
518 | padmove(&player->w_pad, -MOVE_STEP); | ||
519 | } | ||
520 | } | ||
521 | |||
479 | int keys(struct pong *p) | 522 | int keys(struct pong *p) |
480 | { | 523 | { |
481 | int key; | 524 | int key; |
@@ -499,19 +542,39 @@ int keys(struct pong *p) | |||
499 | short touch_x, touch_y; | 542 | short touch_x, touch_y; |
500 | if(key & BUTTON_TOUCHSCREEN) | 543 | if(key & BUTTON_TOUCHSCREEN) |
501 | { | 544 | { |
545 | struct player *player; | ||
502 | touch_x = rb->button_get_data() >> 16; | 546 | touch_x = rb->button_get_data() >> 16; |
503 | touch_y = rb->button_get_data() & 0xFFFF; | 547 | touch_y = rb->button_get_data() & 0xFFFF; |
504 | if(touch_x >= xpos[0] && touch_x <= xpos[0]+(PAD_WIDTH*4)) | ||
505 | padmove(&p->w_pad[0], touch_y-(p->e_pad[0]*2+PAD_HEIGHT)/2); | ||
506 | 548 | ||
507 | if(touch_x >= xpos[1]-(PAD_WIDTH*4) && touch_x <= xpos[1]) | 549 | player = &p->player[0]; |
508 | padmove(&p->w_pad[1], touch_y-(p->e_pad[1]*2+PAD_HEIGHT)/2); | 550 | if(touch_x >= player->xpos && touch_x <= player->xpos+(PAD_WIDTH*4)) |
551 | { | ||
552 | padmove(&player->w_pad, touch_y-(player->e_pad*2+PAD_HEIGHT)/2); | ||
553 | if (player->iscpu) { | ||
554 | /* if left player presses control keys stop cpu player */ | ||
555 | player->iscpu = false; | ||
556 | p->player[0].score = p->player[1].score = 0; /* reset the score */ | ||
557 | rb->lcd_clear_display(); /* get rid of the text */ | ||
558 | } | ||
559 | } | ||
560 | |||
561 | player = &p->player[1]; | ||
562 | if(touch_x >= player->xpos-(PAD_WIDTH*4) && touch_x <= player->xpos) | ||
563 | { | ||
564 | padmove(&player->w_pad, touch_y-(player->e_pad*2+PAD_HEIGHT)/2); | ||
565 | if (player->iscpu) { | ||
566 | /* if right player presses control keys stop cpu player */ | ||
567 | player->iscpu = false; | ||
568 | p->player[0].score = p->player[1].score = 0; /* reset the score */ | ||
569 | rb->lcd_clear_display(); /* get rid of the text */ | ||
570 | } | ||
571 | } | ||
509 | } | 572 | } |
510 | #endif | 573 | #endif |
511 | 574 | ||
512 | #ifdef HAS_BUTTON_HOLD | 575 | #ifdef HAS_BUTTON_HOLD |
513 | if (rb->button_hold()) | 576 | if (rb->button_hold()) |
514 | return 2; /* Pause game */ | 577 | return 2; /* Pause game */ |
515 | #endif | 578 | #endif |
516 | 579 | ||
517 | if(key & PONG_QUIT | 580 | if(key & PONG_QUIT |
@@ -530,53 +593,8 @@ int keys(struct pong *p) | |||
530 | 593 | ||
531 | key = rb->button_status(); /* ignore BUTTON_REPEAT */ | 594 | key = rb->button_status(); /* ignore BUTTON_REPEAT */ |
532 | 595 | ||
533 | if(p->cpu_player[1] == true) { | 596 | key_pad(p, 0, (key & PONG_LEFT_UP), (key & PONG_LEFT_DOWN)); |
534 | if( (p->bally/RES > p->w_pad[0]) | 597 | key_pad(p, 1, (key & PONG_RIGHT_UP), (key & PONG_RIGHT_DOWN)); |
535 | & (p->ballx/RES < DEM_PLAYER_DIST) ) /* player right goes down */ | ||
536 | padmove(&p->w_pad[0], MOVE_STEP); | ||
537 | |||
538 | if( (p->bally/RES < p->w_pad[0]) | ||
539 | & (p->ballx/RES < DEM_PLAYER_DIST) ) /* player right goes up */ | ||
540 | padmove(&p->w_pad[0], -MOVE_STEP); | ||
541 | |||
542 | if( (key & PONG_LEFT_DOWN) || (key & PONG_LEFT_UP) ) { | ||
543 | /* if left player presses control keys stop cpu player */ | ||
544 | p->cpu_player[1] = false; | ||
545 | p->score[0] = p->score[1] = 0; /* reset the score */ | ||
546 | rb->lcd_clear_display(); /* get rid of the text */ | ||
547 | } | ||
548 | } | ||
549 | else { | ||
550 | if(key & PONG_LEFT_DOWN) /* player left goes down */ | ||
551 | padmove(&p->w_pad[0], MOVE_STEP); | ||
552 | |||
553 | if(key & PONG_LEFT_UP) /* player left goes up */ | ||
554 | padmove(&p->w_pad[0], -MOVE_STEP); | ||
555 | } | ||
556 | |||
557 | if(p->cpu_player[2] == true) { | ||
558 | if( (p->bally/RES > p->w_pad[1]) | ||
559 | & (p->ballx/RES > CPU_PLAYER_DIST) ) /* player right goes down */ | ||
560 | padmove(&p->w_pad[1], MOVE_STEP); | ||
561 | |||
562 | if( (p->bally/RES < p->w_pad[1]) | ||
563 | & (p->ballx/RES > CPU_PLAYER_DIST) ) /* player right goes up */ | ||
564 | padmove(&p->w_pad[1], -MOVE_STEP); | ||
565 | |||
566 | if( (key & PONG_RIGHT_DOWN) || (key & PONG_RIGHT_UP) ) { | ||
567 | /* if right player presses control keys stop cpu player */ | ||
568 | p->cpu_player[2] = false; | ||
569 | p->score[0] = p->score[1] = 0; /* reset the score */ | ||
570 | rb->lcd_clear_display(); /* get rid of the text */ | ||
571 | } | ||
572 | } | ||
573 | else { | ||
574 | if(key & PONG_RIGHT_DOWN) /* player right goes down */ | ||
575 | padmove(&p->w_pad[1], MOVE_STEP); | ||
576 | |||
577 | if(key & PONG_RIGHT_UP) /* player right goes up */ | ||
578 | padmove(&p->w_pad[1], -MOVE_STEP); | ||
579 | } | ||
580 | 598 | ||
581 | if(rb->default_event_handler(key) == SYS_USB_CONNECTED) | 599 | if(rb->default_event_handler(key) == SYS_USB_CONNECTED) |
582 | return -1; /* exit game because of USB */ | 600 | return -1; /* exit game because of USB */ |
@@ -589,7 +607,8 @@ void showscore(struct pong *p) | |||
589 | static char buffer[20]; | 607 | static char buffer[20]; |
590 | int w; | 608 | int w; |
591 | 609 | ||
592 | rb->snprintf(buffer, sizeof(buffer), "%d - %d", p->score[0], p->score[1]); | 610 | rb->snprintf(buffer, sizeof(buffer), "%d - %d", |
611 | p->player[0].score, p->player[1].score); | ||
593 | w = rb->lcd_getstringsize((unsigned char *)buffer, NULL, NULL); | 612 | w = rb->lcd_getstringsize((unsigned char *)buffer, NULL, NULL); |
594 | rb->lcd_putsxy( (LCD_WIDTH / 2) - (w / 2), 0, (unsigned char *)buffer); | 613 | rb->lcd_putsxy( (LCD_WIDTH / 2) - (w / 2), 0, (unsigned char *)buffer); |
595 | } | 614 | } |
@@ -618,24 +637,29 @@ enum plugin_status plugin_start(const void* parameter) | |||
618 | 637 | ||
619 | /* init the struct with some silly values to start with */ | 638 | /* init the struct with some silly values to start with */ |
620 | 639 | ||
621 | pong.ballx = 20*RES; | 640 | pong.ball.x = 20*RES; |
622 | pong.bally = 20*RES; | 641 | pong.ball.y = 20*RES; |
642 | pong.ball.speedx = SPEEDX; | ||
643 | pong.ball.speedy = SPEEDY; | ||
623 | 644 | ||
624 | pong.e_pad[0] = 0; | 645 | pong.player[0].xpos = 0; |
625 | pong.w_pad[0] = 7; | 646 | pong.player[0].e_pad = 0; |
626 | pong.e_pad[1] = 0; | 647 | pong.player[0].w_pad = 7; |
627 | pong.w_pad[1] = 40; | 648 | pong.player[1].xpos = LCD_WIDTH-PAD_WIDTH; |
628 | pong.cpu_player[1] = pong.cpu_player[2] = true; /* start every game in demo mode */ | 649 | pong.player[1].e_pad = 0; |
650 | pong.player[1].w_pad = 40; | ||
629 | 651 | ||
630 | pong.ballspeedx = SPEEDX; | 652 | /* start every game in demo mode */ |
631 | pong.ballspeedy = SPEEDY; | 653 | pong.player[0].iscpu = pong.player[1].iscpu = true; |
632 | 654 | ||
633 | pong.score[0] = pong.score[1] = 0; /* lets start at 0 - 0 ;-) */ | 655 | pong.player[0].score = pong.player[1].score = 0; /* lets start at 0 - 0 ;-) */ |
634 | 656 | ||
635 | /* if you don't use the parameter, you can do like | 657 | /* if you don't use the parameter, you can do like |
636 | this to avoid the compiler warning about it */ | 658 | this to avoid the compiler warning about it */ |
637 | (void)parameter; | 659 | (void)parameter; |
638 | 660 | ||
661 | /* Turn off backlight timeout */ | ||
662 | backlight_force_on(); | ||
639 | /* Clear screen */ | 663 | /* Clear screen */ |
640 | rb->lcd_clear_display(); | 664 | rb->lcd_clear_display(); |
641 | 665 | ||
@@ -648,7 +672,7 @@ enum plugin_status plugin_start(const void* parameter) | |||
648 | rb->lcd_clear_display(); | 672 | rb->lcd_clear_display(); |
649 | } | 673 | } |
650 | 674 | ||
651 | if( (pong.cpu_player[1]==true) && (pong.cpu_player[2]==true) ) { | 675 | if( pong.player[0].iscpu && pong.player[1].iscpu ) { |
652 | if(blink_timer<blink_rate) { | 676 | if(blink_timer<blink_rate) { |
653 | ++blink_timer; | 677 | ++blink_timer; |
654 | } | 678 | } |
@@ -675,5 +699,7 @@ enum plugin_status plugin_start(const void* parameter) | |||
675 | game = keys(&pong); /* deal with keys */ | 699 | game = keys(&pong); /* deal with keys */ |
676 | } | 700 | } |
677 | 701 | ||
702 | /* Turn on backlight timeout (revert to settings) */ | ||
703 | backlight_use_settings(); | ||
678 | return (game == 0) ? PLUGIN_OK : PLUGIN_USB_CONNECTED; | 704 | return (game == 0) ? PLUGIN_OK : PLUGIN_USB_CONNECTED; |
679 | } | 705 | } |