Skip to content

Commit

Permalink
Animated ship and ship destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
AwesomeDolphin committed Mar 29, 2015
1 parent af19f87 commit 69de96f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ledgames/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

19 changes: 12 additions & 7 deletions src/ledgames/invaders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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_sprite_t> invader_sprites[2];
static sprite_t invader_missile_sprite;
Expand Down Expand Up @@ -60,6 +61,7 @@ static uint32_t frames_till_shot;

static std::default_random_engine generator;
static std::uniform_int_distribution<unsigned int> invader_distribution(0,5);
static std::uniform_int_distribution<unsigned int> shot_distribution(45,75);

static void reset_invaders(int for_player) {
invader_sprites[for_player].clear();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions src/ledgames/ship_sprite.cc
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions src/ledgames/ship_sprite.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <iostream>
#include <cstdint>

#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;
};

0 comments on commit 69de96f

Please sign in to comment.