From ea1e89a2c2b78b828ede1c152703e566eada6ded Mon Sep 17 00:00:00 2001 From: soeren Date: Tue, 19 Jan 2010 09:55:28 +0000 Subject: [PATCH] done --- games/breakout/ball.c | 48 +++++++++++++++++++++++++++----------- games/breakout/ball.h | 4 ++-- games/breakout/breakout.c | 11 ++++----- games/breakout/common.h | 2 ++ games/breakout/config.h | 2 +- games/breakout/level.c | 14 +++++------ games/breakout/playfield.c | 31 +++++++++++++----------- games/breakout/playfield.h | 6 ++++- 8 files changed, 73 insertions(+), 45 deletions(-) diff --git a/games/breakout/ball.c b/games/breakout/ball.c index d5d02c3..78ffa8f 100644 --- a/games/breakout/ball.c +++ b/games/breakout/ball.c @@ -2,24 +2,38 @@ void ball_think (ball_t *b) { - uint8_t new_x, new_y; + int8_t proj_x, proj_y, bounce; if (!b->strength) return; - new_x = (b->x + b->dir_x) >> 8; - new_y = (b->y + b->dir_y) >> 8; + /* projection of the new coordinates */ + proj_x = (b->x + (b->dir_x)) / 256; + proj_y = (b->y + (b->dir_y)) / 256; + + bounce = check_bounce (proj_x, b->y / 256); + if (bounce & BOUNCE_UNDEF) + bounce = (BOUNCE_X | bounce) & (BOUNCE_X | BOUNCE_Y); + + bounce |= check_bounce (b->x / 256, proj_y); + if (bounce & BOUNCE_UNDEF) + bounce = (BOUNCE_Y | bounce) & (BOUNCE_X | BOUNCE_Y); + + bounce |= check_bounce (proj_x, proj_y); + if (bounce & BOUNCE_UNDEF) + bounce = BOUNCE_X | BOUNCE_Y; - printf("B: %i %i, d: %i %i\n", new_x, new_y); /* ball fell out of the field */ -// if (new_y >= NUM_ROWS) +// if (proj_y >= NUM_ROWS) // ball_die (b); + /* bounce in x direction */ - if (check_bounce (new_x, b->y >> 8)) + if (bounce & 0x01) { b->dir_x *= -1; /* invert x vector */ - new_x += b->dir_x; + b->dir_x ^= random8() & 0x0F; /* randomize bouncing */ + #if BOUNCE_SLOWDOWN if (b->dir_x < 0) @@ -33,11 +47,10 @@ void ball_think (ball_t *b) } /* bounce in y direction */ - if (check_bounce ((b->x >> 8), new_y)) + if (bounce & 0x02) { b->dir_y *= -1; /* invert y vector */ - new_y += b->dir_y; - + b->dir_y ^= random8() & 0x0F; #if BOUNCE_SLOWDOWN if (b->dir_y < 0) { @@ -49,8 +62,12 @@ void ball_think (ball_t *b) #endif } - b->x = new_x; - b->y = new_y; + b->y += b->dir_y; + b->x += b->dir_x; + + + + printf("B: %i %i, d: %i %i\n", b->x, b->y, b->dir_x, b->dir_y); } void ball_die (ball_t *in_b) @@ -65,14 +82,17 @@ void ball_die (ball_t *in_b) void ball_draw (ball_t *b) { pixel p; - p.x = b->x; - p.y = b->y; + p.x = (uint8_t) abs(b->x / 256); + p.y = (uint8_t) abs(b->y / 256); + + printf("db: %i %i\n", p.x, p.y); setpixel (p, 3); } void ball_spawn (ball_t *in_ball, uint16_t in_x, uint16_t in_y, int16_t in_dir_x, int16_t in_dir_y, uint8_t in_strength) { + printf ("spawn: %i %i, dir: %i, %i\n", in_x, in_y, in_dir_x, in_dir_y); in_ball->x = in_x; in_ball->y = in_y; in_ball->dir_x = in_dir_x; diff --git a/games/breakout/ball.h b/games/breakout/ball.h index 67f9fa8..ae2f882 100644 --- a/games/breakout/ball.h +++ b/games/breakout/ball.h @@ -7,8 +7,8 @@ typedef struct { - uint16_t x; - uint16_t y; + int16_t x; + int16_t y; int16_t dir_x; /* direction vector */ int16_t dir_y; uint8_t strength; diff --git a/games/breakout/breakout.c b/games/breakout/breakout.c index adb4e19..3608357 100644 --- a/games/breakout/breakout.c +++ b/games/breakout/breakout.c @@ -4,7 +4,7 @@ void borg_breakout(); #ifdef MENU_SUPPORT //static uint8_t breakout_icon[8] PROGMEM = {0x03, 0x03, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00}; /* our Icon */ -static uint8_t breakout_icon[8] PROGMEM = {0x03, 0x03, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00}; /* our Icon */ +static uint8_t breakout_icon[8] PROGMEM = {0x18, 0x18, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00}; /* our Icon */ game_descriptor_t breakout_game_descriptor __attribute__((section(".game_descriptors"))) = { @@ -17,12 +17,11 @@ void borg_breakout() { uint8_t rungame = 1, num_balls = 1; ball_t balls[1]; - - rebound_init(); - /* spawn a ball in the middle bottom of the field, let it move upwards with random speed & x-direction */ - ball_spawn (&balls[0], (NUM_COLS / 2) << 8, (NUM_ROWS-2) << 8, - (random8() % 8), (random8() % 8), START_LIFES); - level_init(1); + /* spawn a ball in the middle bottom of the field, let it move upwards with random speed & direction */ + ball_spawn (&balls[0], (uint16_t) (NUM_COLS / 2) * 256, (uint16_t) (NUM_ROWS-2) * 256, -120, 150, START_LIFES); + level_init(3); + rebound_init(); while (rungame) { diff --git a/games/breakout/common.h b/games/breakout/common.h index a3a2492..14986c7 100644 --- a/games/breakout/common.h +++ b/games/breakout/common.h @@ -1,6 +1,8 @@ #ifndef COMMON_H #define COMMON_H #include +#include +#include #include "../../joystick/joystick.h" #include "../../config.h" #include "../../autoconf.h" diff --git a/games/breakout/config.h b/games/breakout/config.h index 4d51640..85c54b0 100644 --- a/games/breakout/config.h +++ b/games/breakout/config.h @@ -1,5 +1,5 @@ /* amount of speed to slow down on bounce */ -#define BOUNCE_SLOWDOWN 1 +#define BOUNCE_SLOWDOWN 0 /* minimum speed of the ball */ #define BALL_MINSPEED 0x0010 diff --git a/games/breakout/level.c b/games/breakout/level.c index 23bc351..2027b3c 100644 --- a/games/breakout/level.c +++ b/games/breakout/level.c @@ -8,11 +8,11 @@ enum game_field_t level_field (uint8_t in_x, uint8_t in_y, uint8_t in_lvl) { case 1: /* space for the lower half of the level */ - if (in_y < (NUM_ROWS / 2)) + if (in_y > (NUM_ROWS / 2)) return sp; /* type 2 bricks for 1/4th of the field */ - if (in_y >= (NUM_ROWS / 4)) + if (in_y <= (NUM_ROWS / 4)) return b2; /* fill the rest with type 1 */ @@ -29,15 +29,15 @@ enum game_field_t level_field (uint8_t in_x, uint8_t in_y, uint8_t in_lvl) case 2: /* space for the lower third of the level */ - if (in_y < (NUM_ROWS / 3)) + if (in_y > (NUM_ROWS / 3)) return sp; /* type 3 bricks for 1/8th of the field */ - if (in_y >= (NUM_ROWS / 8)) + if (in_y <= (NUM_ROWS / 8)) return b3; /* type 2 bricks for 1/4th of the field */ - if (in_y >= (NUM_ROWS / 4)) + if (in_y <= (NUM_ROWS / 4)) return b2; /* fill the rest with type 1 */ @@ -45,10 +45,10 @@ enum game_field_t level_field (uint8_t in_x, uint8_t in_y, uint8_t in_lvl) default: /* random level generation */ /* space for the lower half of the level */ - if (in_y < (NUM_ROWS / 2)) + if (in_y > (NUM_ROWS / 2)) return sp; - return random8() % 5; /* fill field with random bricks (and spaces) */ + return random8() % 4; /* fill field with random bricks (and spaces) */ break; } } diff --git a/games/breakout/playfield.c b/games/breakout/playfield.c index a87f776..4d35fb6 100644 --- a/games/breakout/playfield.c +++ b/games/breakout/playfield.c @@ -12,27 +12,24 @@ void brick_damage (uint8_t in_x, uint8_t in_y) if (playfield[in_x][in_y] > bs || playfield[in_x][in_y] == 0) return; - - playfield[in_x][in_y]--; + playfield[in_x][in_y] -= 1; score_add (1); } -uint8_t check_bounce (uint8_t in_x, uint8_t in_y) +uint8_t check_bounce (int8_t in_x, int8_t in_y) { + uint8_t ov = 0; /* overflow check */ - if (in_x >= NUM_ROWS) - return 1; + if (in_x >= NUM_ROWS || in_x < 0) + ov |= BOUNCE_X; - if (in_y >= NUM_COLS) - return 1; + if (in_y >= NUM_COLS || in_y < 0) + ov |= BOUNCE_Y; + if (ov) return ov; /* collisions with real objects */ - switch (playfield[in_x][in_y]) + switch (playfield[abs(in_x)][abs(in_y)]) { - case sp: - case bl: - return 0; - case b2: case b3: case b1: @@ -40,11 +37,17 @@ uint8_t check_bounce (uint8_t in_x, uint8_t in_y) /* intentional fallthrough */ case bs: - return 1; + return BOUNCE_UNDEF | ov; /* bouncing on the rebound needs special care */ case rb: - return 2; + return BOUNCE_Y; + + case sp: + case bl: + default: + return ov; + } } diff --git a/games/breakout/playfield.h b/games/breakout/playfield.h index e4627d8..b7ef992 100644 --- a/games/breakout/playfield.h +++ b/games/breakout/playfield.h @@ -2,6 +2,10 @@ #define PLAYFIELD_H #include "common.h" +#define BOUNCE_X 0x01 +#define BOUNCE_Y 0x02 +#define BOUNCE_UNDEF 0x04 + /* entries for the playing field */ enum game_field_t { @@ -23,6 +27,6 @@ void playfield_set (uint8_t in_x, uint8_t in_y, enum game_field_t in_field); /* @description Checks if there is an object in the way. If so, it returns 1 */ -uint8_t check_bounce (uint8_t in_x, uint8_t in_y); +uint8_t check_bounce (int8_t in_x, int8_t in_y); #endif /* PLAYFIELD_H */