static void draw_stuff(t_game *game) { draw_board(game); draw_blocks(game); draw_ball(game); draw_score(game->score, 1.8f, -1.6f); draw_score(game->life, 0.2f, -1.6f); }
void play_breakmeggy(void) { uint8_t i; uint8_t tick; uint8_t lives = 3; key_t keyp, lastp = 0; /* - 0 1 2 3 4 5 - */ uint8_t pos = 2; uint8_t ball_x = 3, ball_y = 1; uint8_t ball_v = STOP; /* XXX: This could be packed if space becomes a serious issue */ uint8_t blocks[4][4] = { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, }; /* 3 lives = 3 aux LEDs */ status_lights = LED_2 | LED_1 | LED_0; draw_pixel(ball_x, ball_y, BALL_COLOUR); for (;;) { draw_paddle(pos); draw_blocks(blocks); draw_pixel(ball_x, ball_y, BALL_COLOUR); /* * Paddle movement and time delay */ for (tick = 0; tick < TICKS; tick++) { keyp = get_key_down(); if (keyp != lastp) { switch (keyp) { case D_LEFT: pos = (pos > 0) ? pos - 1 : 0; if (ball_v == STOP) { draw_pixel(ball_x, ball_y, c000000); ball_x = (ball_x > 1) ? ball_x - 1 : 1; draw_pixel(ball_x, ball_y, BALL_COLOUR); } draw_paddle(pos); break; case D_RIGHT: pos = (pos < 5) ? pos + 1 : 5; if (ball_v == STOP) { draw_pixel(ball_x, ball_y, c000000); ball_x = (ball_x < 6) ? ball_x + 1 : 6; draw_pixel(ball_x, ball_y, BALL_COLOUR); } draw_paddle(pos); break; case B_RIGHT: if (ball_v == STOP) { ball_v = RIGHT | UP; } break; default: delay_us(2); break; } } lastp = keyp; delay(20); } /* * Update world if ball is moving */ if (ball_v != STOP) { /* * Clear old ball position */ draw_pixel(ball_x, ball_y, c000000); /* * Apply standard ball velocity */ ball_x = (ball_v & RIGHT) ? ball_x + 1 : ball_x - 1; ball_y = (ball_v & UP) ? ball_y + 1 : ball_y - 1; if (ball_x == 7) { ball_v &= ~RIGHT; } if (ball_x == 0) { ball_v |= RIGHT; } if (ball_y == 7) { ball_v &= ~UP; } /* * Check paddle hit */ if (ball_y == 1 && (ball_x >= pos && ball_x <= pos + 2)) { ball_v |= UP; } /* * Collision detection */ if (ball_y > 3) { /* Check block rows */ for (i = 0; i < 4; i++) { if (blocks[i][ball_y - 4] && (ball_x == 2*i || ball_x == 2*i+1)) { blocks[i][ball_y - 4] = 0; ball_v &= ~UP; play_tone(tCs2, 100); break; } } } /* * Ball missed */ if (ball_y == 0) { die_anim(75); if (--lives == 0) { break; } /* * Re-initialise game state */ pos = 2; ball_x = 3, ball_y = 1; ball_v = STOP; status_lights = LED_0; if (lives == 2) { status_lights |= LED_1; } } draw_pixel(ball_x, ball_y, BALL_COLOUR); } } /* * Game over... ;_; */ }