diff --git a/src/ledgames/Makefile b/src/ledgames/Makefile index 68c96fbb..3a732ab6 100755 --- a/src/ledgames/Makefile +++ b/src/ledgames/Makefile @@ -14,7 +14,7 @@ breakout.srcs += bricks.cc gpio_pin.cc controls.cc screen.cc sprite.cc ball_spri black.srcs += black.c white.srcs += white.c pong.srcs += pong.cc gpio_pin.cc controls.cc screen.cc sprite.cc ball_sprite.cc -invaders.srcs += invaders.cc gpio_pin.cc controls.cc screen.cc sprite.cc invader_sprite.cc png.cc +invaders.srcs += invaders.cc gpio_pin.cc controls.cc screen.cc sprite.cc invader_sprite.cc ship_sprite.cc png.cc include ../../Makefile.common diff --git a/src/ledgames/invaders.cc b/src/ledgames/invaders.cc index 1d912820..690ad8a5 100644 --- a/src/ledgames/invaders.cc +++ b/src/ledgames/invaders.cc @@ -22,6 +22,7 @@ #include "screen.hh" #include "sprite.hh" #include "invader_sprite.hh" +#include "ship_sprite.hh" #include "png.hh" static controls_t *player_controls[3]; @@ -30,7 +31,7 @@ static int player_lives[2]; static int number_players; static int current_player; -static sprite_t ship_sprite; +static ship_sprite_t ship_sprite; static sprite_t ship_missile_sprite; static std::vector invader_sprites[2]; static sprite_t invader_missile_sprite; @@ -60,6 +61,7 @@ static uint32_t frames_till_shot; static std::default_random_engine generator; static std::uniform_int_distribution invader_distribution(0,5); +static std::uniform_int_distribution shot_distribution(45,75); static void reset_invaders(int for_player) { invader_sprites[for_player].clear(); @@ -109,7 +111,10 @@ static bool reset_round(void) { static void reset_game(int with_number_players) { ship_sprite.set_active(true); ship_sprite.set_position(28, 55); - ship_sprite.set_image(168,0,7,7,&sprite_sheet); + ship_sprite.set_image(168,0,7,7,&sprite_sheet, 0); + ship_sprite.set_image(168+7,0,7,7,&sprite_sheet, 1); + ship_sprite.set_image(168+14,0,7,7,&sprite_sheet, 2); + ship_sprite.set_image(168+28,0,7,7,&sprite_sheet, 3); reset_invaders(0); reset_invaders(1); @@ -203,8 +208,8 @@ void render_game(Screen *screen) { sprite_t invader_sprite = invader_sprites[current_player][invader_column]; invader_missile_sprite.set_position(invader_sprite.get_x_position(), invader_sprite.get_y_position()); invader_missile_sprite.set_image(196,0,7,7,&sprite_sheet); - invader_missile_sprite.set_speed(0.0f, 2.0f); - frames_till_shot=30; + invader_missile_sprite.set_speed(0.0f, 1.0f); + frames_till_shot=shot_distribution(generator); } if (game_state == game_state_t::Attract) { @@ -288,7 +293,7 @@ void render_game(Screen *screen) { if (ship_sprite.test_collision(invader_missile_sprite)) { game_state = game_state_t::NewShip; - ship_sprite.set_active(false); + ship_sprite.destroy_sprite(); player_lives[current_player]--; frames_in_state = 0; } @@ -338,7 +343,7 @@ static void init_sdl(void) { exit(1); } - startup_bong = Mix_LoadWAV("/root/startup.wav"); + startup_bong = Mix_LoadWAV("bin/startup.wav"); if (startup_bong == NULL) { fprintf(stderr, "Unable to load startup.wav: %s\n", Mix_GetError()); exit(1); @@ -372,7 +377,7 @@ const char ** argv ledscape_t * const leds = ledscape_init(config, 0); - sprite_sheet.read_file("/root/Invaders.png"); + sprite_sheet.read_file("bin/Invaders.png"); player_controls[0] = new controls_t(1); player_controls[1] = new controls_t(2); diff --git a/src/ledgames/ship_sprite.cc b/src/ledgames/ship_sprite.cc new file mode 100644 index 00000000..1a252eee --- /dev/null +++ b/src/ledgames/ship_sprite.cc @@ -0,0 +1,48 @@ +#include "ship_sprite.hh" + +ship_sprite_t::ship_sprite_t() : +sprite_t(), +frames_in_anim(0) +{ +} + +void ship_sprite_t::draw_onto(Screen *screen) { + if (frame_ == 0) { + frames_in_anim++; + if (frames_in_anim > 3) { + frames_in_anim = 0; + frame_ = 1; + } + } else if (frame_ == 1) { + frames_in_anim++; + if (frames_in_anim > 3) { + frames_in_anim = 0; + frame_ = 0; + } + } else if (frame_ == 2) { + frames_in_anim++; + if (frames_in_anim > 3) { + frames_in_anim = 0; + frame_ = 3; + } + } else if (frame_ == 3) { + frames_in_anim++; + if (frames_in_anim > 3) { + frames_in_anim = 0; + explode_anims++; + if (explode_anims > 10) { + set_active(false); + frame_ = 0; + } else { + frame_ = 2; + } + } + } + sprite_t::draw_onto(screen); +} + +void ship_sprite_t::destroy_sprite(void) { + frame_ = 2; + frames_in_anim = 0; + explode_anims = 0; +} \ No newline at end of file diff --git a/src/ledgames/ship_sprite.hh b/src/ledgames/ship_sprite.hh new file mode 100644 index 00000000..7a5ae358 --- /dev/null +++ b/src/ledgames/ship_sprite.hh @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +#include "sprite.hh" + +class ship_sprite_t : public sprite_t { +public: + ship_sprite_t(); + + virtual void draw_onto(Screen *screen) override; + virtual void destroy_sprite(void); + +private: + uint32_t frames_in_anim; + uint32_t explode_anims; +};