Marco Merlin / Mbed OS rIoTwear-snake

Dependencies:   lib_sx9500 GraphicsDisplay ColorMemLCD Large_fonts

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(ColorMemLCD &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(ColorMemLCD &display)
00058 {
00059 #if(MULTI_UPDATE)
00060         display.window( x*STEPSIZE + BOARDERWIDTH/2 -1, 
00061                 (y+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2, 
00062                  STEPSIZE+1,
00063                 STEPSIZE );
00064 #endif  
00065 
00066     for (int i=0;i<NFOODPIX;i++)
00067     {
00068         display.pixel(x*STEPSIZE + BOARDERWIDTH/2 + static_cast<uint8_t>( food_px_map[i] & 0xF), \
00069                 (y+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2 + static_cast<uint8_t>( (food_px_map[i] >> 4) & 0xF), Black);
00070     }
00071 
00072 #if(MULTI_UPDATE)
00073     display.update();
00074 #endif
00075 
00076 }
00077 
00078 void Food::remove(ColorMemLCD &display)
00079 {
00080 #if(MULTI_UPDATE)
00081     display.window( x*STEPSIZE + BOARDERWIDTH/2 -1, 
00082                 (y+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2, 
00083                  STEPSIZE+1,
00084                 STEPSIZE );
00085 #endif
00086 
00087     for (int i=0;i<NFOODPIX;i++)
00088         {
00089             display.pixel(x*STEPSIZE + BOARDERWIDTH/2 +static_cast<uint8_t>(food_px_map[i] & 0xF), \
00090                     (y+TOPEDGE)*STEPSIZE + BOARDERWIDTH/2 + static_cast<uint8_t>((food_px_map[i] >> 4) & 0xF), BACKGROUND_COLOR);
00091         }
00092 
00093 #if(MULTI_UPDATE)
00094     display.update();
00095 #endif
00096 
00097 }
00098 
00099