From 45dc9282fd7b9236faf44e24971c1d320196aa6c Mon Sep 17 00:00:00 2001 From: soeren Date: Fri, 15 Jan 2010 12:19:54 +0000 Subject: [PATCH] does not compute yet (don't even try ;)) --- games/breakout/ball.c | 56 +++++++++++++++++++++++ games/breakout/ball.h | 35 ++++++++++++++ games/breakout/breakout.c | 32 +++++++++++++ games/breakout/bricks.h | 8 ++++ games/breakout/common.h | 12 +++++ games/breakout/config.h | 8 ++++ games/breakout/level.c | 67 +++++++++++++++++++++++++++ games/breakout/level.h | 5 ++ games/breakout/playfield.c | 93 ++++++++++++++++++++++++++++++++++++++ games/breakout/playfield.h | 31 +++++++++++++ games/breakout/score.h | 13 ++++++ 11 files changed, 360 insertions(+) create mode 100644 games/breakout/ball.c create mode 100644 games/breakout/ball.h create mode 100644 games/breakout/breakout.c create mode 100644 games/breakout/bricks.h create mode 100644 games/breakout/common.h create mode 100644 games/breakout/config.h create mode 100644 games/breakout/level.c create mode 100644 games/breakout/level.h create mode 100644 games/breakout/playfield.c create mode 100644 games/breakout/playfield.h create mode 100644 games/breakout/score.h diff --git a/games/breakout/ball.c b/games/breakout/ball.c new file mode 100644 index 0000000..cefdce7 --- /dev/null +++ b/games/breakout/ball.c @@ -0,0 +1,56 @@ +#include "common.h" + +void ball_think (ball_t *b) +{ + uint8_t new_x, new_y; + if (!b->strength) + return; + + new_x = (b->x + b->dir_x) >> 8; + new_y = (b->y + b->dir_y) >> 8; + + /* ball fell out of the field */ + if (new_y >= NUM_ROWS) + ball_die (b); + + /* bounce in x direction */ + if (check_bounce (new_x, b->y >> 8)) + { + b->dir_x *= -1; /* invert x vector */ + +#if BOUNCE_SLOWDOWN + if (b->dir_x < 0) + { + b->dir_x += BOUNCE_SLOWDOWN; + } else + { + b->dir_x -= BOUNCE_SLOWDOWN; + } +#endif + } + + /* bounce in y direction */ + if (check_bounce (b->x >> 8), new_y) + { + b->dir_y *= -1; /* invert y vector */ + +#if BOUNCE_SLOWDOWN + if (b->dir_y < 0) + { + b->dir_y += BOUNCE_SLOWDOWN; + } else + { + b->dir_y -= BOUNCE_SLOWDOWN; + } +#endif + } +} + +void ball_die (ball_t *in_b) +{ + in_b->strength--; + + /* respawn ball with random direction */ + if (in_b->strength) + ball_spawn (in_b, (NUM_COLS / 2) << 8, (NUM_ROWS-2) << 8, - random8(), random8(), START_LIFES); +} diff --git a/games/breakout/ball.h b/games/breakout/ball.h new file mode 100644 index 0000000..1ccbc28 --- /dev/null +++ b/games/breakout/ball.h @@ -0,0 +1,35 @@ +#ifndef BALL_H +#define BALL_H + +#include +#include "common.h" + + +typedef struct +{ + uint16_t x; + uint16_t y; + int16_t dir_x; /* direction vector */ + int16_t dir_y; + uint8_t strength; +} ball_t; + +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) +{ + in_ball->x = in_x; + in_ball->y = in_y; + in_ball->dir_x = in_dir_x; + in_ball->dir_y = in_dir_y; + in_ball->strength = in_strength; +} + +/* @description Called once per game tick. Move the ball further along it's vector. + */ +void ball_think (ball_t *in_ball); + +/* @description Change the ball's moving vector according to bounce and collision type + */ +void ball_bounce (ball_t *in_ball, enum collision_t in_coltype); + +void ball_die (ball_t *in_b) +#endif /* BALL_H */ diff --git a/games/breakout/breakout.c b/games/breakout/breakout.c new file mode 100644 index 0000000..9804ec2 --- /dev/null +++ b/games/breakout/breakout.c @@ -0,0 +1,32 @@ + +#include "../../menu/menu.h" + +static uint8_t icon[8] PROGMEM = + {0x03, 0x03, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00}; /* our Icon */ + + +#ifdef MENU_SUPPORT +game_descriptor_t invaders_game_descriptor __attribute__((section(".game_descriptors"))) ={ + &borg_breakout, + icon, +}; +#endif + +void borg_breakout() +{ + uint8_t rungame = 1, num_balls = 1; + ball_t balls[1]; + + ball_init (balls[0]); + + /* 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(), random8(), START_LIFES); + level_init(1); + + while (rungame) + { + ball_think(balls[0]); + playfield_draw(); + ball_draw(balls[0]); + } +} diff --git a/games/breakout/bricks.h b/games/breakout/bricks.h new file mode 100644 index 0000000..ce106da --- /dev/null +++ b/games/breakout/bricks.h @@ -0,0 +1,8 @@ +typedef struct +{ + uint8_t x; + uint8_t y; + uint8_t strength; +} brick_t; + +void brick_damage (brick_t *in_brick); diff --git a/games/breakout/common.h b/games/breakout/common.h new file mode 100644 index 0000000..7cd915b --- /dev/null +++ b/games/breakout/common.h @@ -0,0 +1,12 @@ +#ifndef COMMON_H +#define COMMON_H +#include +#include "../../config.h" +#include "../../random/prng.h" +#include "../../pixel.h" +#include "config.h" +#include "playfield.h" +#include "ball.h" +#include "score.h" +#include "level.h" +#endif /* COMMON_H */ diff --git a/games/breakout/config.h b/games/breakout/config.h new file mode 100644 index 0000000..d4f8b47 --- /dev/null +++ b/games/breakout/config.h @@ -0,0 +1,8 @@ +/* amount of speed to slow down on bounce */ +#define BOUNCE_SLOWDOWN 1 + +/* minimum speed of the ball */ +#define BALL_MINSPEED 0x0010 + +/* initial amount of lifes */ +#define START_LIFES 3 diff --git a/games/breakout/level.c b/games/breakout/level.c new file mode 100644 index 0000000..86e4f85 --- /dev/null +++ b/games/breakout/level.c @@ -0,0 +1,67 @@ +#include "level.h" + +/* real level definition */ +inline void level_field (uint8_t in_x, uint8_t in_y, uint8_t in_lvl) +{ + switch (in_lvl) + { + case 1: + /* space for the lower half of the level */ + if (in_y < (NUM_ROWS / 2)) + return sp; + + /* type 2 bricks for 1/4th of the field */ + if (in_y >= (NUM_ROWS / 4)) + return b2; + + /* fill the rest with type 1 */ + return b1; + break; + + case 3: + /* add a row of solid bricks right in the middle of the field */ + if (in_y == (NUM_ROWS / 2) && + (in_x > (NUM_COLS / 4) && in_x < (NUM_COLS - (NUM_COLS / 4))) + return bs; + + /* intentional fallthrough: the rest of level 3 is like level 2 */ + + case 2: + /* space for the lower third of the level */ + if (in_y < (NUM_ROWS / 3)) + return sp; + + /* type 3 bricks for 1/8th of the field */ + if (in_y >= (NUM_ROWS / 8)) + return b3; + + /* type 2 bricks for 1/4th of the field */ + if (in_y >= (NUM_ROWS / 4)) + return b2; + + /* fill the rest with type 1 */ + return b1; + + default: /* random level generation */ + /* space for the lower half of the level */ + if (in_y < (NUM_ROWS / 2)) + return sp; + + return random8() % 5; /* fill field with random bricks (and spaces) */ + break; + } +} + +void level_init (uint8_t in_levelnum) +{ + uint8_t x,y; + + for (x=0;x= NUM_ROWS) + return 1; + + if (in_y >= NUM_COLS) + return 1; + + /* collisions with real objects */ + switch (playfield[in_x][in_y]) + { + case freespace: + case ball: + return 0; + + case brick_2: + case brick_3: + case brick_1: + brick_damage (in_x, in_y); + /* intentional fallthrough */ + + case brick_solid: + case bouncer: + return 1; + } +} + +/* this is the actual draw function for a single field + */ +static inline void draw_single_field (game_field_t in_f) +{ + switch (in_f) + { + case b1: + setPixel (); + return; + case rb: + case b2: + + return; + + case b3: + case bl: + case bs: + + return; + + default: /* this includes freespace */ + + return; + + } +} + +void playfield_draw () +{ + uint8_t x,y; + + for (x=0;x +#include "ball.h" +#include "score.h" + +#ifndef PLAYFIELD_H +#define PLAYFIELD_H + +/* entries for the playing field */ +enum game_field_t +{ + sp = 0, /* space */ + b1 = 1, b2 = 2, b3 = 3, /* bricks */ + bs = 4 /* solid (unbreakable) brick */ + bl, /* ball */ + rb, /* rebound */ +}; + + +/* @description draw the current field + */ +void playfield_draw(); + +/* @description set a field with given property. + */ +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); + +#endif /* PLAYFIELD_H */ diff --git a/games/breakout/score.h b/games/breakout/score.h new file mode 100644 index 0000000..3d5ce79 --- /dev/null +++ b/games/breakout/score.h @@ -0,0 +1,13 @@ +#include + +#ifndef SCORE_H +#define SCORE_H + +static uint16_t score = 0; + +void score_add(uint8_t); +score_add (uint8_t in_score) +{ + score += in_score; +} +#endif