Play snake using mbed! A snake-like game that runs on the memoryLCD display on Happy Gecko.

Dependencies:   mbed MemoryLCD

Hungry Gecko Game on Memory LCD

This game is meant to run on a Silicon labs EFM32 Happy Gecko Starter Kit, and demonstrate the use of the Memory LCD. User controls the push buttons on the kit PB1, and PB0 to let the gecko turn left and right respectively in order to eat the food. User should avoid running into the body of the gecko, otherwise game is over. It is allowed to hit the wall and go from the other side of it. Have fun!

Information

All examples in this repo are considered EXPERIMENTAL QUALITY, meaning this code has been created as one-off proof-of-concept and is suitable as a demonstration for experimental purposes only. This code will not be regularly maintained by Silicon Labs and there is no guarantee that these projects will work across all environments, SDK versions and hardware.

Libraries

gecko.h

This class creates a gecko object, draws it on the memory LCD according to its movement, and keeps track of the length of the gecko

gecko.h

#include "LS013B7DH03.h"
#include "settings.h"
#include "asymmetricPart.h"
 
class Gecko{
  private:
      uint8_t _position[MAXLENGTH];
      uint8_t _last;
      uint8_t _length;
 
      asymmetricPart _head;
 
      void drawPart(silabs::LS013B7DH03 &display, uint8_t x, uint8_t y) const;
      void removePart(silabs::LS013B7DH03 &display, uint8_t x, uint8_t y) const;
    public:
        Gecko();
 
        /* Moves the snake and redraws it on the display */
        void move(silabs::LS013B7DH03 &display, Direction dir);
 
        /* Redraw the entire snake */
        void draw(silabs::LS013B7DH03 &display) const;
 
        /* Check if the snake has collides with itself */
        bool selfCollision() const;
 
        /* Increases the length of the snake by one STEPSIZE x STEPSIZE tile */
        void increaseLength(silabs::LS013B7DH03 &display, Direction dir);
 
        /* Checks if the head of the snake occupies a STEP */
        bool headOccupiesTile(uint8_t x, uint8_t y) const;
 
        /* Chech if the snake occupies a STEPSIZE x STEPSIZE tile */
        bool occupiesTile(uint8_t x, uint8_t y) const;
 
        /* Get coordinates */
        uint8_t getX(uint8_t indx) const;
        uint8_t getY(uint8_t indx) const;
};

food.h

This class creates a food object, displays it on memory LCD and updates its location every time it is eaten by gecko.

food.h

#include "settings.h"
#include "LS013B7DH03.h"
 
class Gecko; // Forward declaration
#ifndef FOOD_H_
#define FOOD_H_
/* Pixel map for the food
 * 4 most significant bits y-crd, 4 least significant bits x-crd
 * The upper left corner of the part is asigned coordinates (x,y)=(0,0)
 */
class Food{
public:
    Food();
    bool isEaten(Gecko &gck);
    void reset(silabs::LS013B7DH03 &display, const Gecko &gck);
    void draw(silabs::LS013B7DH03 &display);
 
private:
    uint8_t x;
    uint8_t y;
    void remove(silabs::LS013B7DH03 &display);
};
 
#endif /* FOOD_H_ */

settings.h

This file defines the settings including the wall location, board height and width, and enum for gecko movements.

settings.h

#ifndef SETTINGS_H_
#define SETTINGS_H_
#include "LCDSettings.h"
 
#define MAXLENGTH 255
#define STEPSIZE 7
 
/*  Define board limits, note that the display height and display width is 128
 * This particlular choice leads to 255 STEPSIZE x STEPSIZE tiles. Thus,
 * the tile number can be stored in 1 uint8_t
 * */
#define TOPEDGE 2
#define BOARD_HEIGHT 15
#define BOARD_WIDTH 17
#define BOARDERWIDTH (DISPLAY_WIDTH - STEPSIZE*BOARD_WIDTH)
 
/* Define allowed direction to move */
typedef enum{
    LEFT=0, RIGHT, UP, DOWN
} Direction;
 
#endif /* SETTINGS_H_ */

asymmetricPart.h

This class creates the object of the asymmetric part of the gecko body, and updates its location on the memory LCD.

asymmetricPart.h

#include "settings.h"
#include "LS013B7DH03.h"
 
/* Bitfield containing the position of the top left corner of the part and the orientation
 * The minimum number of bits to store x, y and orientation is 10 bits (there are 255 tiles).
 * Since this will be expanded to 16 bits the x and y values are stored in 7 bits each.
 * */
 
struct positionAndDirection{
    uint8_t x:7;
    uint8_t y:7;
    uint8_t direction:2;
};
 
class asymmetricPart{
private:
    positionAndDirection _posAndDir;
    const uint8_t *_px_map;
    uint8_t _nPix;
 
    /* Private member functions */
    void draw(silabs::LS013B7DH03 &display, uint8_t color) const;
public:
    asymmetricPart();
    asymmetricPart(uint8_t x, uint8_t y, Direction dir,  const uint8_t *px_map, uint8_t nPix);
 
    /* Set all member variables */
    void init(uint8_t x, uint8_t y, Direction dir,  const uint8_t *px_map, uint8_t nPix);
 
    /* Draw the part on the screen */
    void draw(silabs::LS013B7DH03 &display) const {draw(display, Black);};
 
    /* Erase the part from the screen */
    void remove(silabs::LS013B7DH03 &display) const {draw(display, White);};
 
    /* Get member variables */
    uint8_t getX() const {return _posAndDir.x;};
    uint8_t getY() const {return _posAndDir.y;};
    uint8_t getDir() const {return _posAndDir.direction;};
 
    /* Set member variables */
    void setX(uint8_t x) {_posAndDir.x = x&0x7F;};
    void setY(uint8_t y) {_posAndDir.y = y&0x7F;};
    void setDir(Direction dir) {_posAndDir.direction = static_cast<int>(dir)&0x0003;};
};

Full Example

mian.cpp

#include "LS013B7DH03.h"
#include "gecko.h"
#include "food.h"
#include "settings.h"
 
/**************************** Define I/O **************************************/
 
InterruptIn in(SW1);
InterruptIn inB1(SW0);
#define SCK     PE12
#define MOSI    PE10
 
DigitalOut CS(PA10);
DigitalOut EXTCOM(PF3);
DigitalOut DISP(PA8);
 
SPI displaySPI(MOSI, NC, SCK);
silabs::LS013B7DH03 display(&displaySPI, &CS, &EXTCOM);
 
/**************************** Define Timers ***********************************/
 
LowPowerTicker ticker;
 
/**************************** Global variables ********************************/
 
/* Flag that is set to true when the display is refreshed */
volatile bool refreshed = false;
 
/* Flag that is set to true by the ticker. Makes the gecko move at regular time intervals */
volatile bool updateDisplay = true;
 
/* A flag that ensures the controller to only read one click per frame */
volatile bool PBenabled = true;
 
/* Direction in which the gecko moves */
Direction dir = UP;
 
uint8_t score = 0;
 
/**************************** Define callback handlers ************************/
void tickerCallback(void);
 
/* Push button handlers */
void in_handler_B0();
void in_handler_B1();
 
/* Define game modes */
typedef enum {
    PLAY, STOP
} Modes;
 
/* Set the game mode */
Modes mode = PLAY;
 
void in_handler_B0() {
    /* Only change the direction if push button is enabled */
    if (PBenabled)
    {
        switch (dir) {
        case (UP):
                dir = LEFT;
            break;
        case (DOWN):
                dir = RIGHT;
            break;
        case (RIGHT):
                dir = UP;
            break;
        case (LEFT):
                dir = DOWN;
            break;
        }
        PBenabled = false;
    }
}
 
void in_handler_B1() {
    /* Only change the direction if push button is enabled */
    if (PBenabled)
    {
        switch (dir) {
        case UP:
            dir = RIGHT;
            break;
        case DOWN:
            dir = LEFT;
            break;
        case RIGHT:
            dir = DOWN;
            break;
        case LEFT:
            dir = UP;
            break;
        }
        PBenabled = false;
    }
}
 
 
/* Callback functions */
void tickerCallback(void) {
    updateDisplay = true;
 
    /* Enable push buttons if the display is refreshed */
    PBenabled = refreshed;
}
 
 
void refreshCallback(void) {
    refreshed = true;
}
 
/**************************** Fill the boarder ********************************/
 
void fillBoarder(silabs::LS013B7DH03 &display){
    display.fill(0, 0, DISPLAY_WIDTH, TOPEDGE*STEPSIZE, Black);
 
    /* Fill right edge */
    display.fill(BOARD_WIDTH*STEPSIZE + BOARDERWIDTH/2, TOPEDGE*STEPSIZE + BOARDERWIDTH/2, 1, BOARD_HEIGHT*STEPSIZE, Black);
    for (uint8_t i=0;i<BOARD_HEIGHT;i++){
        for (uint8_t j=0;j<(DISPLAY_WIDTH-BOARD_WIDTH*STEPSIZE - BOARDERWIDTH/2);j++){
            display.pixel(BOARD_WIDTH*STEPSIZE + BOARDERWIDTH/2 +j, (i+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2+j, Black);
        }
    }
 
    /* Fill bottom edge */
    display.fill(BOARDERWIDTH/2, (BOARD_HEIGHT+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2, BOARD_WIDTH*STEPSIZE, 1, Black);
 
    for (uint8_t i=0;i<=BOARD_WIDTH;i++){
        for (uint8_t j=0;j<(DISPLAY_WIDTH-BOARD_WIDTH*STEPSIZE - BOARDERWIDTH/2);j++){
            display.pixel(i*STEPSIZE + BOARDERWIDTH/2 +j, (BOARD_HEIGHT+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2+j, Black);
        }
    }
 
    /* Fill left edge */
    display.fill(BOARDERWIDTH/2-1, TOPEDGE*STEPSIZE + BOARDERWIDTH/2, 1, BOARD_HEIGHT*STEPSIZE, Black);
    for (uint8_t i=0;i<BOARD_HEIGHT;i++){
        for (uint8_t j=0;j<(DISPLAY_WIDTH-BOARD_WIDTH*STEPSIZE - BOARDERWIDTH/2 - 1);j++){
            display.pixel(j, (i+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2+j, Black);
        }
    }
 
    /* Fill top edge */
    display.fill(BOARDERWIDTH/2, TOPEDGE*STEPSIZE + BOARDERWIDTH/2 - 1, BOARD_WIDTH*STEPSIZE, 1, Black);
 
    for (uint8_t i=0;i<=BOARD_WIDTH;i++){
        for (uint8_t j=0;j<(DISPLAY_WIDTH-BOARD_WIDTH*STEPSIZE - BOARDERWIDTH/2 - 1);j++){
            display.pixel(i*STEPSIZE + BOARDERWIDTH/2 +j, TOPEDGE*STEPSIZE + j, Black);
        }
    }
 
}
 
/**************************** MAIN ********************************************/
int main() {
 
    /* Initialize pushbutton handlers */
    in.fall(in_handler_B0);
    inB1.fall(in_handler_B1);
 
    /* Enable the LCD */
    DISP = 1;
 
    /* Start generating the 3Hz call */
    ticker.attach(&tickerCallback, 0.3333f);
 
    /* Reset the LCD to a blank state. (All white) */
    refreshed = false;
    if (display.clearImmediate(refreshCallback) == LS013B7DH03_OK){
        while (refreshed == false) sleep();
    }
 
    fillBoarder(display);
    refreshed = false;
    if (display.update(refreshCallback) == LS013B7DH03_OK)
    {
        while (refreshed == false) sleep();
    }
    Gecko gck;
    Food fd;
    gck.draw(display);
    fd.draw(display);
 
    /* Push update to the display */
    refreshed = false;
    if (display.update(refreshCallback) == LS013B7DH03_OK)
    {
        while (refreshed == false) sleep();
    }
    display.foreground(White);
    display.background(Black);
    display.locate(4,0);
    display.printf("Score: ");
 
    display.locate(11,0);
    display.printf("%d", score);
 
    /* Main loop */
    while (1) {
        sleep();
        if (updateDisplay && refreshed && (mode==PLAY)) {
            updateDisplay = false;
 
            gck.move(display, dir);
 
            if (fd.isEaten(gck))
            {
                fd.reset(display, gck);
                gck.increaseLength(display, dir);
 
                /* Redraw gecko */
                gck.draw(display);
                /* Update the score */
                score++;
                display.locate(11,0);
                display.printf("%d", score);
            }
 
            /* Update display */
            refreshed = false;
            display.update(refreshCallback);
 
 
            if (gck.selfCollision()) {
                mode = STOP;
                gck.move(display, dir);
                display.locate(3, 6);
                display.printf("GAME OVER!");
                refreshed = false;
                display.update(refreshCallback);
            }
        }
    }
}
Committer:
stevew817
Date:
Mon Sep 12 10:52:53 2016 +0000
Revision:
3:db0ad94fc6aa
Parent:
2:5a41b935bc86
Pull latest libraries

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lichang 2:5a41b935bc86 1 /***************************************************************************//**
lichang 2:5a41b935bc86 2 * @file gecko.h
lichang 2:5a41b935bc86 3 * @brief class for creating a gecko-like object and keeps track of its momvement
lichang 2:5a41b935bc86 4 *******************************************************************************
lichang 2:5a41b935bc86 5 * @section License
lichang 2:5a41b935bc86 6 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
lichang 2:5a41b935bc86 7 *******************************************************************************
lichang 2:5a41b935bc86 8 *
lichang 2:5a41b935bc86 9 * Permission is granted to anyone to use this software for any purpose,
lichang 2:5a41b935bc86 10 * including commercial applications, and to alter it and redistribute it
lichang 2:5a41b935bc86 11 * freely, subject to the following restrictions:
lichang 2:5a41b935bc86 12 *
lichang 2:5a41b935bc86 13 * 1. The origin of this software must not be misrepresented; you must not
lichang 2:5a41b935bc86 14 * claim that you wrote the original software.
lichang 2:5a41b935bc86 15 * 2. Altered source versions must be plainly marked as such, and must not be
lichang 2:5a41b935bc86 16 * misrepresented as being the original software.
lichang 2:5a41b935bc86 17 * 3. This notice may not be removed or altered from any source distribution.
lichang 2:5a41b935bc86 18 *
lichang 2:5a41b935bc86 19 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
lichang 2:5a41b935bc86 20 * obligation to support this Software. Silicon Labs is providing the
lichang 2:5a41b935bc86 21 * Software "AS IS", with no express or implied warranties of any kind,
lichang 2:5a41b935bc86 22 * including, but not limited to, any implied warranties of merchantability
lichang 2:5a41b935bc86 23 * or fitness for any particular purpose or warranties against infringement
lichang 2:5a41b935bc86 24 * of any proprietary rights of a third party.
lichang 2:5a41b935bc86 25 *
lichang 2:5a41b935bc86 26 * Silicon Labs will not be liable for any consequential, incidental, or
lichang 2:5a41b935bc86 27 * special damages, or any other relief, or for any claim by any third party,
lichang 2:5a41b935bc86 28 * arising from your use of this Software.
lichang 2:5a41b935bc86 29 *
lichang 2:5a41b935bc86 30 ******************************************************************************/
lichang 2:5a41b935bc86 31
dakleive 0:008c379fb5e6 32 #include "LS013B7DH03.h"
dakleive 0:008c379fb5e6 33 #include "settings.h"
lichang 2:5a41b935bc86 34 #include "asymmetricPart.h"
dakleive 0:008c379fb5e6 35
dakleive 0:008c379fb5e6 36 class Gecko{
dakleive 0:008c379fb5e6 37 private:
dakleive 0:008c379fb5e6 38 uint8_t _position[MAXLENGTH];
dakleive 0:008c379fb5e6 39 uint8_t _last;
dakleive 0:008c379fb5e6 40 uint8_t _length;
dakleive 0:008c379fb5e6 41
lichang 2:5a41b935bc86 42 asymmetricPart _head;
dakleive 0:008c379fb5e6 43
dakleive 0:008c379fb5e6 44 void drawPart(silabs::LS013B7DH03 &display, uint8_t x, uint8_t y) const;
dakleive 0:008c379fb5e6 45 void removePart(silabs::LS013B7DH03 &display, uint8_t x, uint8_t y) const;
dakleive 0:008c379fb5e6 46 public:
dakleive 0:008c379fb5e6 47 Gecko();
dakleive 0:008c379fb5e6 48
dakleive 0:008c379fb5e6 49 /* Moves the snake and redraws it on the display */
dakleive 0:008c379fb5e6 50 void move(silabs::LS013B7DH03 &display, Direction dir);
dakleive 0:008c379fb5e6 51
dakleive 0:008c379fb5e6 52 /* Redraw the entire snake */
dakleive 0:008c379fb5e6 53 void draw(silabs::LS013B7DH03 &display) const;
dakleive 0:008c379fb5e6 54
dakleive 0:008c379fb5e6 55 /* Check if the snake has collides with itself */
dakleive 0:008c379fb5e6 56 bool selfCollision() const;
dakleive 0:008c379fb5e6 57
dakleive 0:008c379fb5e6 58 /* Increases the length of the snake by one STEPSIZE x STEPSIZE tile */
dakleive 0:008c379fb5e6 59 void increaseLength(silabs::LS013B7DH03 &display, Direction dir);
dakleive 0:008c379fb5e6 60
dakleive 0:008c379fb5e6 61 /* Checks if the head of the snake occupies a STEP */
dakleive 0:008c379fb5e6 62 bool headOccupiesTile(uint8_t x, uint8_t y) const;
dakleive 0:008c379fb5e6 63
dakleive 0:008c379fb5e6 64 /* Chech if the snake occupies a STEPSIZE x STEPSIZE tile */
dakleive 0:008c379fb5e6 65 bool occupiesTile(uint8_t x, uint8_t y) const;
dakleive 0:008c379fb5e6 66
dakleive 0:008c379fb5e6 67 /* Get coordinates */
dakleive 0:008c379fb5e6 68 uint8_t getX(uint8_t indx) const;
dakleive 0:008c379fb5e6 69 uint8_t getY(uint8_t indx) const;
dakleive 0:008c379fb5e6 70 };
dakleive 0:008c379fb5e6 71
dakleive 0:008c379fb5e6 72