Retro Invaders a space invaders clone by Chris Favreau. Written for the RetroMbuino development board from outrageouscircuits.com for the game programming contest.

Dependencies:   mbed

This is a space invaders clone written for the Retro Mbuino from outrageous circuits.

Development board: http://www.outrageouscircuits.com/shop/product/15 ).

The game itself is basic space invaders. Shoot them before they get to the bottom of the screen. It has a UFO saucer which you can shoot for extra points. You get 4 shields and each shield can be hit up to 4 times before it is gone. Hmm... as each level increases the speed of the invaders shots goes up. The invaders only speed up when there is less of them. You complete the level when you shoot all the invaders. The game ends when a) you run out of lives (you start with 3) or the invaders get to the bottom.

The LEDs turned out to be a pretty cool addition to the game. I wrote a class that blinks them and turns them on for a specified amount of time. They add a nice extra to the game. I use them on the intro screen and when the UFO is present.

The sound turned out to be really difficult for a few reasons. The biggest was that I had never written a sound engine before. The interrupt service routine working off the timer was the easier part. I also had a lot of trouble because there is no filter to filter out the PWM frequency to the speaker... so I had to run the PWM frequency way up there 30 kHz.

The graphics turned out to be a bit of a bear too. Thanks to Chris Taylor for his really great LCD API. I picked up a couple of frames per second from that. I had modified the DisplayN18 class for blitting a single line buffer to the LCD panel however his is a little faster for some reason? I used a different approach to doing the graphics (as I have very little experience with anything other than double buffered displays). I have a tile map and a list of sprites. Each tile/sprite is 1 bit 8x8. They could be bigger. I ran out of time. That much is not special. What is different from what I can tell is that I use a 1 line buffer that is 160 shorts long. The render function first adds the tile map data into the line buffer first. Then the sprites are added over the existing data. You can have a great deal of different sprites and maps going to the screen and just have to rewrite the LCD memory once per frame. After each line is composited, the line is then drawn to the LCD. Kind of like an Atari 2600. Each sprite/tile has a foreground and background color and can be different from the other tiles/sprites. There is one color reserved for Transparency.

There are 16 colors to choose from. I chose a palette based on the Macintosh OS 4.1 palette I found on WikiPedia. It is a very nice mix of colors.

I found a sprite editor called SpriteX ( https://code.google.com/p/spritesx-ed/ )... it works nicely except that the 16x16 sprites are in a weird format. Time limited me to 8x8 sprites. Oh well.

I used nokring to make the music. It makes RTTTL formatted ring tones which my sound api can play. Here is a useful site that has lots of arcade/video game ring tones with a link to nokring in the utilities page. http://arcadetones.emuunlim.com/files.htm

Other than all that stuff I used state machines to do most of the game logic. Please excuse the horrible coding as I tried to comment a lot of it however it is not very nice to look at. Lots of long functions...

Game.h

Committer:
cfavreau
Date:
2015-03-03
Revision:
0:c79e1f29f029

File content as of revision 0:c79e1f29f029:

#include "mbed.h"
#include "./Display/display.h"      // LCD Display Driver
#include "./led/led.h"              // LED Driver
#include "./Input/input.h"          // INPUT Driver
#include "./Sound/sound.h"          // SOUND Driver

#pragma once

enum GAME_MODE
{
    eGameTitle,
    eGamePlay,
    eGameOver,
};

#define FRAMES_PER_SECOND   20      // unfortunately this is what we get...
#define MS_PER_FRAME        50

class Game
{   
    int frame_timeout;
    int frame_count;
    int now;

    Timer timer;
    display disp;
    AnalogIn ain;
    led lights;
    input inputs;
    Sound sound;
    
    GAME_MODE mode;
    int game_step;
    bool paused;
    
    void initialize();
    bool dotick();
    
    void DoGameTitle();
    void DoGamePlay();
    void DoGameOver();
    
    uint8_t star_field_timer;
    void SetupStarfield();
    void UpdateStarfield();
    
    void SetupInvaders();
    void UpdateInvaders();
    bool invaders[40];
    int8_t active_invaders;
    int8_t invaders_inc;
    uint8_t invaders_timer;
    uint8_t invaders_wait_time;
    uint8_t invaders_frame;
    uint8_t invaders_x, invaders_y;
    bool invader_active_shot[4];
    uint8_t invader_shot_interval;
    uint8_t invader_shot_x[4];
    uint8_t invader_shot_y[4];
    int8_t invader_shot_timer;
    uint8_t invader_march_note;
    
    
    void SetupPlayer();
    void UpdatePlayer();
    uint8_t player_x, player_y;
    bool player_shot_active;
    uint8_t player_shot_x, player_shot_y;
    
    void SetupSaucer();
    void UpdateSaucer();
    bool saucer_active;
    int saucer_timer;
    int8_t saucer_inc;
    uint8_t saucer_x, saucer_y;
    
    void SetupShields();
    void UpdateShields();
    uint8_t shield_x, shield_y;
    uint8_t shield_points[4];
    
    void CheckCollisions();
    
    void IncScore(int points);
    void UpdateScore();
    void UpdateLives();
    int hiscore;
    int score;
    uint8_t lives;
    uint8_t level;
    
    uint8_t wait_timer;
    
    void UpdateGameOverInvaders();
    uint8_t game_over_msg;
    
public:

    Game();
    
    void tick();
};