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

Dependencies:   mbed MemoryLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers food.cpp Source File

food.cpp

Go to the documentation of this file.
00001 /***************************************************************************//**
00002  * @file food.cpp
00003  * @brief class for creating a food-like object and keeps track of its momvement
00004  *******************************************************************************
00005  * @section License
00006  * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
00007  *******************************************************************************
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  * 1. The origin of this software must not be misrepresented; you must not
00014  *    claim that you wrote the original software.
00015  * 2. Altered source versions must be plainly marked as such, and must not be
00016  *    misrepresented as being the original software.
00017  * 3. This notice may not be removed or altered from any source distribution.
00018  *
00019  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
00020  * obligation to support this Software. Silicon Labs is providing the
00021  * Software "AS IS", with no express or implied warranties of any kind,
00022  * including, but not limited to, any implied warranties of merchantability
00023  * or fitness for any particular purpose or warranties against infringement
00024  * of any proprietary rights of a third party.
00025  *
00026  * Silicon Labs will not be liable for any consequential, incidental, or
00027  * special damages, or any other relief, or for any claim by any third party,
00028  * arising from your use of this Software.
00029  *
00030  ******************************************************************************/
00031 
00032 #include"food.h"
00033 #include "gecko.h"
00034 #define NFOODPIX 18
00035 
00036 const uint8_t food_px_map[NFOODPIX] = {0x02, 0x03, 0x04, 0x11, 0x13, 0x15, 0x20, 0x23, 0x26, 0x30, 0x36, 0x40, 0x46, 0x51, 0x55, 0x62, 0x63, 0x64};
00037 Food::Food(): x(5), y(5){};
00038 
00039 bool Food::isEaten(Gecko &gck){
00040     return gck.headOccupiesTile(x, y);
00041 }
00042 
00043 void Food::reset(silabs::LS013B7DH03 &display, const Gecko &gck)
00044 {
00045     this->remove(display);
00046     x = rand()%BOARD_WIDTH;
00047     y = rand()%BOARD_HEIGHT;
00048 
00049     while (gck.occupiesTile(x,y))
00050     {
00051         x = rand()%BOARD_WIDTH;
00052         y = rand()%BOARD_HEIGHT;
00053     }
00054     this->draw(display);
00055 }
00056 
00057 void Food::draw(silabs::LS013B7DH03 &display)
00058 {
00059     for (int i=0;i<NFOODPIX;i++)
00060     {
00061         display.pixel(x*STEPSIZE + BOARDERWIDTH/2 + static_cast<uint8_t>( food_px_map[i] & 0xF), \
00062                 (y+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2 + static_cast<uint8_t>( (food_px_map[i] >> 4) & 0xF), Black);
00063     }
00064 }
00065 
00066 void Food::remove(silabs::LS013B7DH03 &display)
00067 {
00068     for (int i=0;i<NFOODPIX;i++)
00069         {
00070             display.pixel(x*STEPSIZE + BOARDERWIDTH/2 +static_cast<uint8_t>(food_px_map[i] & 0xF), \
00071                     (y+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2 + static_cast<uint8_t>((food_px_map[i] >> 4) & 0xF), White);
00072         }
00073 }
00074 
00075 
00076