static void key_pad(struct pong *p, int pad, int up, int down) { struct player *player = &p->player[pad]; if(player->iscpu) { if((pad && (p->ball.x/RES > CPU_PLAYER_RIGHT_DIST)) /* cpu right */ || (!pad && (p->ball.x/RES < CPU_PLAYER_LEFT_DIST)) /* cpu left */) { if(p->ball.y/RES > player->w_pad) /* player goes down */ padmove(&player->w_pad, MOVE_STEP); if(p->ball.y/RES < player->w_pad) /* player goes up */ padmove(&player->w_pad, -MOVE_STEP); } if(down || up) { /* if player presses control keys stop cpu player */ player->iscpu = false; p->player[0].score = p->player[1].score = 0; /* reset the score */ rb->lcd_clear_display(); /* get rid of the text */ } } else { if(down) /* player goes down */ padmove(&player->w_pad, MOVE_STEP); if(up) /* player goes up */ padmove(&player->w_pad, -MOVE_STEP); } }
int keys(struct pong *p) { int key; int time = 4; /* number of ticks this function will loop reading keys */ int start = *rb->current_tick; int end = start + time; while(end > *rb->current_tick) { key = rb->button_get_w_tmo(end - *rb->current_tick); if(key & PONG_QUIT) return 0; /* exit game NOW */ if(key & PONG_LEFT_DOWN) /* player left goes down */ padmove(&p->w_pad[0], MOVE_STEP); if(key & PONG_LEFT_UP) /* player left goes up */ padmove(&p->w_pad[0], -MOVE_STEP); if(key & PONG_RIGHT_DOWN) /* player right goes down */ padmove(&p->w_pad[1], MOVE_STEP); if(key & PONG_RIGHT_UP) /* player right goes up */ padmove(&p->w_pad[1], -MOVE_STEP); if(rb->default_event_handler(key) == SYS_USB_CONNECTED) return -1; /* exit game because of USB */ } return 1; /* return 0 to exit game */ }
static int keys(struct pong *p) { int key; #ifdef PONG_PAUSE static bool pause = false; #endif /* number of ticks this function will loop reading keys */ #ifndef HAVE_TOUCHSCREEN int time = 4; #else int time = 1; #endif int start = *rb->current_tick; int end = start + time; while(TIME_BEFORE(*rb->current_tick, end)) { key = rb->button_get_w_tmo(end - *rb->current_tick); #ifdef HAVE_TOUCHSCREEN short touch_x, touch_y; if(key & BUTTON_TOUCHSCREEN) { struct player *player; touch_x = rb->button_get_data() >> 16; touch_y = rb->button_get_data() & 0xFFFF; player = &p->player[0]; if(touch_x >= player->xpos && touch_x <= player->xpos+(PAD_WIDTH*4)) { padmove(&player->w_pad, touch_y-(player->e_pad*2+PAD_HEIGHT)/2); if (player->iscpu) { /* if left player presses control keys stop cpu player */ player->iscpu = false; p->player[0].score = p->player[1].score = 0; /* reset the score */ rb->lcd_clear_display(); /* get rid of the text */ } } player = &p->player[1]; if(touch_x >= player->xpos-(PAD_WIDTH*4) && touch_x <= player->xpos) { padmove(&player->w_pad, touch_y-(player->e_pad*2+PAD_HEIGHT)/2); if (player->iscpu) { /* if right player presses control keys stop cpu player */ player->iscpu = false; p->player[0].score = p->player[1].score = 0; /* reset the score */ rb->lcd_clear_display(); /* get rid of the text */ } } } #endif #ifdef HAS_BUTTON_HOLD if (rb->button_hold()) return 2; /* Pause game */ #endif if(key & PONG_QUIT #ifdef PONG_RC_QUIT || key & PONG_RC_QUIT #endif ) return 0; /* exit game NOW */ #ifdef PONG_PAUSE if(key == PONG_PAUSE) pause = !pause; if(pause) return 2; /* Pause game */ #endif key = rb->button_status(); /* ignore BUTTON_REPEAT */ key_pad(p, 0, (key & PONG_LEFT_UP), (key & PONG_LEFT_DOWN)); key_pad(p, 1, (key & PONG_RIGHT_UP), (key & PONG_RIGHT_DOWN)); if(rb->default_event_handler(key) == SYS_USB_CONNECTED) return -1; /* exit game because of USB */ } return 1; /* return 0 to exit game */ }