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...

Committer:
cfavreau
Date:
Tue Mar 03 04:26:01 2015 +0000
Revision:
0:c79e1f29f029
Retro Invaders by Chris Favreau for the RetroMbuino Platform - outrageouscircuits.com game programming contest.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cfavreau 0:c79e1f29f029 1 #include "mbed.h"
cfavreau 0:c79e1f29f029 2 #include "BurstSPI.h" // Fast SPI class
cfavreau 0:c79e1f29f029 3 #include "font8x8.h" // Default 8x8 Font
cfavreau 0:c79e1f29f029 4
cfavreau 0:c79e1f29f029 5 // Colors
cfavreau 0:c79e1f29f029 6 #define RGB2SHORT(r, g, b) (((r & 0xF8) << 8) | ((b & 0xFC) << 3) | (g >> 3))
cfavreau 0:c79e1f29f029 7 #define BLUE RGB2SHORT(0, 0, 255)
cfavreau 0:c79e1f29f029 8 #define GREEN RGB2SHORT(0, 255, 0)
cfavreau 0:c79e1f29f029 9 #define RED RGB2SHORT(255, 0, 0)
cfavreau 0:c79e1f29f029 10 #define WHITE RGB2SHORT(255, 255, 255)
cfavreau 0:c79e1f29f029 11 #define BLACK RGB2SHORT(0, 0, 0)
cfavreau 0:c79e1f29f029 12 #define MAGENTA RGB2SHORT(255, 0, 255)
cfavreau 0:c79e1f29f029 13 #define CYAN RGB2SHORT(0, 255, 255)
cfavreau 0:c79e1f29f029 14 #define YELLOW RGB2SHORT(255, 255, 0)
cfavreau 0:c79e1f29f029 15
cfavreau 0:c79e1f29f029 16 #define LCD_WIDTH 160
cfavreau 0:c79e1f29f029 17 #define LCD_HEIGHT 128
cfavreau 0:c79e1f29f029 18
cfavreau 0:c79e1f29f029 19 #pragma once
cfavreau 0:c79e1f29f029 20
cfavreau 0:c79e1f29f029 21 class DisplayN18 {
cfavreau 0:c79e1f29f029 22 protected:
cfavreau 0:c79e1f29f029 23 static const unsigned char STEP = 4;
cfavreau 0:c79e1f29f029 24
cfavreau 0:c79e1f29f029 25 DigitalOut resetPin;
cfavreau 0:c79e1f29f029 26 DigitalOut backlightPin;
cfavreau 0:c79e1f29f029 27 DigitalOut rsPin;
cfavreau 0:c79e1f29f029 28 DigitalOut csPin;
cfavreau 0:c79e1f29f029 29 //SPI spi;
cfavreau 0:c79e1f29f029 30 BurstSPI spi;
cfavreau 0:c79e1f29f029 31
cfavreau 0:c79e1f29f029 32 unsigned char *buffer;
cfavreau 0:c79e1f29f029 33
cfavreau 0:c79e1f29f029 34 void writeCommand(unsigned char command);
cfavreau 0:c79e1f29f029 35 void writeData(unsigned char data);
cfavreau 0:c79e1f29f029 36 void writeData(const unsigned char* data, unsigned int length);
cfavreau 0:c79e1f29f029 37
cfavreau 0:c79e1f29f029 38 void reset();
cfavreau 0:c79e1f29f029 39 void initialize();
cfavreau 0:c79e1f29f029 40 void setClippingArea(unsigned char x, unsigned char y, unsigned char width, unsigned char height);
cfavreau 0:c79e1f29f029 41
cfavreau 0:c79e1f29f029 42 public:
cfavreau 0:c79e1f29f029 43 DisplayN18();
cfavreau 0:c79e1f29f029 44
cfavreau 0:c79e1f29f029 45 // Either rgbToShort is backwards OR these were backwards.. so I reorganized the byte order.
cfavreau 0:c79e1f29f029 46 /*
cfavreau 0:c79e1f29f029 47 static const unsigned short BLUE = 0xF800;
cfavreau 0:c79e1f29f029 48 static const unsigned short GREEN = 0x07E0;
cfavreau 0:c79e1f29f029 49 static const unsigned short RED = 0x001F;
cfavreau 0:c79e1f29f029 50 static const unsigned short WHITE = 0xFFFF;
cfavreau 0:c79e1f29f029 51 static const unsigned short BLACK = 0x0000;
cfavreau 0:c79e1f29f029 52 */
cfavreau 0:c79e1f29f029 53
cfavreau 0:c79e1f29f029 54 unsigned short *getLineBuffer();
cfavreau 0:c79e1f29f029 55 void blitLine(unsigned char line);
cfavreau 0:c79e1f29f029 56
cfavreau 0:c79e1f29f029 57 static unsigned short rgbToShort(unsigned char r, unsigned char g, unsigned char b);
cfavreau 0:c79e1f29f029 58
cfavreau 0:c79e1f29f029 59 void clear(unsigned short backColor = BLACK);
cfavreau 0:c79e1f29f029 60 void draw(const unsigned short* data, int x, int y, int width, int height);
cfavreau 0:c79e1f29f029 61 void setPixel(int x, int y, unsigned short foreColor);
cfavreau 0:c79e1f29f029 62
cfavreau 0:c79e1f29f029 63 void drawBitmap(int x, int y, int w, int h, unsigned char *bitmap, unsigned short foreColor, unsigned short backColor);
cfavreau 0:c79e1f29f029 64
cfavreau 0:c79e1f29f029 65 void fillRect(int x, int y, int width, int height, unsigned short foreColor);
cfavreau 0:c79e1f29f029 66 void drawRect(int x, int y, int width, int height, unsigned short foreColor);
cfavreau 0:c79e1f29f029 67
cfavreau 0:c79e1f29f029 68 void fillCircle(int x, int y, int radius, unsigned short foreColor);
cfavreau 0:c79e1f29f029 69 void drawCircle(int x, int y, int radius, unsigned short foreColor);
cfavreau 0:c79e1f29f029 70
cfavreau 0:c79e1f29f029 71 void drawLine(int x0, int y0, int x1, int y1, unsigned short foreColor);
cfavreau 0:c79e1f29f029 72
cfavreau 0:c79e1f29f029 73 //unsigned char *getCharBuffer();
cfavreau 0:c79e1f29f029 74 //void drawCharacter(int x, int y, const char character, unsigned short foreColor, unsigned short backColor, unsigned char fontSize = 1);
cfavreau 0:c79e1f29f029 75 //void drawString(int x, int y, const char* str, unsigned short foreColor, unsigned short backColor, unsigned char fontSize = 1);
cfavreau 0:c79e1f29f029 76 };