Alien Invasion (Hopeless: The Dark Cave)

Description

Alien Invasion is a very simple game inspired by "Hopeless: The Dark Cave". Our beloved emoji hero must defend earth from the impending doom from the extra-terrestrial creatures. The aliens surround our hero but luckily he is armed with his trusty blaster to defeat the aliens. However, if he does not attack each alien soon enough, they will explode in his face and make him (and our earth) progressively sadder until it's the end... of the world.

This game utilizes the following hardware components: RGB LED, touch keypad, LCD screen, microSD card reader, speaker, and amplifier.

Images

/media/uploads/huck1137/img_20170313_045338.jpg /media/uploads/huck1137/img_20170313_045329.jpg

Wiring

mbedtouch sensor
GNDGND
p9SDA
p10SCL
p26IRQ
VOUTVCC
mbeduLCD
GNDGND
p13RX
p14TX
p11RES
VUVOUT
mbedmicroSD
GNDGND
N/ACD
p6DO
p7SCK
VOUTVCC
p5DI
p8CS
mbedampspeaker
GNDpwr - (GND), in -
VUpwr +
p18in +
out ++
out --

Video

Code

#include "mbed.h"
#include "SDFileSystem.h"
#include "wave_player.h"
#include "uLCD_4DGL.h"
#include "rtos.h"
#include <mpr121.h>
SDFileSystem sd(p5, p6, p7, p8, "sd");
uLCD_4DGL uLCD(p13,p14,p11);
I2C i2c(p9, p10);
AnalogOut DACout(p18);
Serial pc(USBTX, USBRX);
wave_player waver(&DACout);
Mpr121 touch(&i2c, Mpr121::ADD_VSS);
InterruptIn interrupt(p26);
int log2(unsigned v)
{
    unsigned r = 0;             
    while (v >>= 1)
      r++;                
    return r;                        
}

class RGBLed
{
public:
    RGBLed(PinName redpin, PinName greenpin, PinName bluepin);
    void write(float red,float green, float blue);
private:
    PwmOut _redpin;
    PwmOut _greenpin;
    PwmOut _bluepin;
};
RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin)
    : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin)
{
    //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker)
    _redpin.period(0.0005);
}
void RGBLed::write(float red,float green, float blue)
{
    _redpin = red;
    _greenpin = green;
    _bluepin = blue;
}
RGBLed myRGBled(p21, p22, p23);
void playSound(char * wav)
{
    // open wav file
    FILE *wave_file;
    wave_file=fopen(wav,"r");

    if(wave_file == NULL) {
        printf("ERROR_SD");
        return;
    }

    // play wav file
    waver.play(wave_file);

    // close wav file
    fclose(wave_file);
}
//Mutex mymu;
//Semaphore two_slots(2);
//------------------------------------------------------------------------------------------------------------
bool a1 = false;
bool a2 = false;
bool a3 = false;
bool a5 = false;
bool a7 = false;
bool e1 = false;
bool e2 = false;
bool e3 = false;
bool e5 = false;
bool e7 = false;

int score = 0;
int health = 2;

bool hit = false;

int input() {
    int number = touch.read(0x00);
    number = log2(number);
    return number;
}

void draw_smiley() {
    uLCD.filled_circle(64,64,16,0xffff00);
    uLCD.filled_circle(58,60,2,BLACK);
    uLCD.filled_circle(70,60,2,BLACK);
    uLCD.filled_circle(64,70,6,BLACK);
    uLCD.filled_rectangle(56,64,72,69,0xffff00);
}
    
void draw_meh() {
    uLCD.filled_circle(64,64,16,0xffff00);
    uLCD.filled_circle(58,60,2,BLACK);
    uLCD.filled_circle(70,60,2,BLACK);
    uLCD.filled_rectangle(56,70,72,73,BLACK);
}    

void draw_frowny() {
    uLCD.filled_circle(64,64,16,0xffff00);
    uLCD.filled_circle(58,60,2,BLACK);
    uLCD.filled_circle(70,60,2,BLACK);
    uLCD.filled_circle(64,72,5,BLACK);
    uLCD.filled_rectangle(56,73,72,77,0xffff00);
}

void game_over() {
    uLCD.cls();
    uLCD.text_width(4); //4X size text
    uLCD.text_height(4);
    uLCD.color(GREEN);
    uLCD.locate(0,0);
    uLCD.printf("THE END");
    uLCD.text_width(2);
    uLCD.text_height(2);
    uLCD.color(RED);
    uLCD.locate(0,6);
    uLCD.printf("of the\n world...");
    playSound("/sd/wavfiles/music.wav");
}

void show(int pos) {
    if (pos == 1) {a1 = true;}
    else if (pos == 2) {a2 = true;}
    else if (pos == 3) {a3 = true;}
    else if (pos == 5) {a5 = true;}
    else if (pos == 7) {a7 = true;}
    
    if (a1&e1 || a2&e2 || a3&e3 || a5&e5 || a7&e7) { score+=10; hit = true;}
    
    //if (health == 1) {draw_meh(); }
    //else if (health == 0) {draw_frowny(); }
    printf("health = %d\n",health);
    
    int hpos, vpos;
    if (pos % 4 == 1) { hpos = 20; }
    else if (pos % 4 == 2) { hpos = 64; }
    else if (pos % 4 == 3) { hpos = 108; }
    
    if (pos > 0 && pos < 4) { vpos = 20; }
    else if (pos > 4 && pos < 8) { vpos = 64; }
    else if (pos > 8 && pos < 12) { vpos = 108; }
    
    if (pos != 0 && pos != 4 && pos != 6 && pos != 8) {
        uLCD.filled_circle(hpos, vpos, 3, 0xffff00 );
        uLCD.filled_circle(hpos, vpos, 4, 0xffff00 );
        uLCD.filled_circle(hpos, vpos, 5, 0xffff00 );
        uLCD.filled_circle(hpos, vpos, 6, 0xFFA500);
        uLCD.filled_circle(hpos, vpos, 7, 0xFFA500);
        uLCD.filled_circle(hpos, vpos, 8, 0xff0000 );
        uLCD.filled_circle(hpos, vpos, 9, 0xff0000 );
        uLCD.filled_circle(hpos, vpos, 11, 0xff0000 );
        
        uLCD.filled_circle(hpos, vpos, 3, BLACK );
        uLCD.filled_circle(hpos, vpos, 4, BLACK );
        uLCD.filled_circle(hpos, vpos, 5, BLACK );
        uLCD.filled_circle(hpos, vpos, 6, BLACK);
        uLCD.filled_circle(hpos, vpos, 7, BLACK);
        uLCD.filled_circle(hpos, vpos, 8, BLACK );
        uLCD.filled_circle(hpos, vpos, 9, BLACK );
        uLCD.filled_circle(hpos, vpos, 11, BLACK );
        a1 = false; a2 = false; a3 = false; a5 = false; a7 = false;
    }
}  

void draw_enemy_thread() {
    while(1) {
        hit = false;
        int pos = rand() % 10;
        if (pos == 0 || pos == 1) {pos = 1; e1 = true;}
        else if (pos == 2 || pos == 3) {pos = 2; e2 = true;}
        else if (pos == 4 || pos == 5) {pos = 3; e3 = true;}
        else if (pos == 6 || pos == 7) {pos = 5; e5 = true;}
        else if (pos == 8 || pos == 9) {pos = 7; e7 = true;}
        int hpos, vpos;
        if (pos % 4 == 1) { hpos = 20; }
        else if (pos % 4 == 2) { hpos = 64; }
        else if (pos % 4 == 3) { hpos = 108; }
        
        if (pos > 0 && pos < 4) { vpos = 20; }
        else if (pos > 4 && pos < 8) { vpos = 64; }
        else if (pos > 8 && pos < 12) { vpos = 108; }
        
        uLCD.filled_circle(hpos,vpos,10,0x00ff00);
        uLCD.filled_circle(hpos,vpos+5,6,0x00ff00);
        uLCD.filled_circle(hpos-4,vpos-2,4,RED);
        uLCD.filled_circle(hpos+4,vpos-2,4,RED);
        /*uLCD.locate(14,14);
        uLCD.printf("3");
        wait(1);
        uLCD.locate(14,14);
        uLCD.printf("2");
        wait(1);
        uLCD.locate(14,14);
        uLCD.printf("1");
        wait(1);*/
        uLCD.locate(0,14);
        uLCD.printf("%d",score);
        wait(3);
        if (!hit) {
            uLCD.filled_circle(hpos,vpos,11,RED);
            wait(1); 
            health--;
            uLCD.filled_circle(hpos,vpos,11,BLACK);
            if (health == 1) {draw_meh(); myRGBled.write(0,1,1);}
            else if (health == 0) {draw_frowny(); myRGBled.write(1,0,1);}
            else if (health == -1) {myRGBled.write(0,0,1); game_over(); break;}
        }
        e1 = false; e2 = false; e3 = false; e5 = false; e7 = false;
    }
}        
    
void show_input_thread() {
    while(1) {
        show(input());
    }
}

void draw_character() {
    while (1) {
        draw_smiley();
        
    }
}



Thread t1;
Thread t2;
Thread t3;

int main() {
    draw_smiley();
    myRGBled.write(0,1,0);
    wait(1);
    t1.start(draw_enemy_thread);
    t2.start(show_input_thread);
}


Please log in to post comments.