Thomas Davies / Mbed 2 deprecated LetTheBallDrop

Dependencies:   N5110 mbed PowerControl

GameScreen.cpp

Committer:
AppleJuice
Date:
2015-03-07
Revision:
2:d4402bc3dd45
Parent:
1:3305d7e44880
Child:
3:00c0f63f4408

File content as of revision 2:d4402bc3dd45:

#include "mbed.h"
#include "N5110.h"
#include "GameScreen.h"

//need to extend on initialization to set some variables
void GameScreen::Initialize()
{
    init();

}

//draw platform
//  ___________  ________  < y
//               ^ x       
void GameScreen::drawPlatform(int x,int y)
{    
    for (int a = 0; a < 48; a ++)
    {
        for (int b = y; b < (y+platThickness_); b++)
        {
            //skip pixels of gap
            if (a > x && a < (x + platGapSize_))
                break;
                
            
            
            //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
            if (b > (maxY_ - 1))
                break;
            
            setPixel(b,a);    
        }
    }
}

//erase platform
void GameScreen::erasePlatform(int y)
{
    for (int a = 0; a < 48; a ++)
    {
        for (int b = y; b < (y+platThickness_); b++)
        {            
            //skip pixels below maxY (lets platforms 'emerge' instead of 'appear')
            if (b > (maxY_ - 1))
                break;
            
            clearPixel(b,a);    
        }
    }
}

//draw the player ball where (x,y) is top right corner.
//      XXP <-- P(x,y) 
//     XXXX
//     XXXX
//      XX    
void GameScreen::drawBall(int x, int y)
{   
    //more elegant ways to do this...but this is most efficient
    setPixel(y,x+1);
    setPixel(y,x+2);
    setPixel(y+1,x);
    setPixel(y+1,x+1);
    setPixel(y+1,x+2);
    setPixel(y+1,x+3);
    setPixel(y+2,x);
    setPixel(y+2,x+1);
    setPixel(y+2,x+2);
    setPixel(y+2,x+3);
    setPixel(y+3,x+1);
    setPixel(y+3,x+2);    
}

//draw the player ball where (x,y) is top right corner.
void GameScreen::eraseBall(int x, int y)
{   
    //more elegant ways to do this...but this is most efficient
    clearPixel(y,x+1);
    clearPixel(y,x+2);
    clearPixel(y+1,x);
    clearPixel(y+1,x+1);
    clearPixel(y+1,x+2);
    clearPixel(y+1,x+3);
    clearPixel(y+2,x);
    clearPixel(y+2,x+1);
    clearPixel(y+2,x+2);
    clearPixel(y+2,x+3);
    clearPixel(y+3,x+1);
    clearPixel(y+3,x+2);    
}

void GameScreen::createAllPlatforms() 
{            
    for (int n = 0; n < numPlatforms_ ; n++)
    {
        Platform *newPlatform = new Platform;
        newPlatform->id = n;
        newPlatform->x = rand() % (maxX_-platGapSize_ + 1) - 1;     // 'randomlly' place gap somewhere on platform *NOTE: need RTC for this game to be unpredictable...
        newPlatform->y = maxY_ - n*platSpacing_ - platThickness_ + 1;   
        allPlatforms[n] = newPlatform;
    }
}

void GameScreen::freeAllPlatforms()
{
    for (int n = 0; n < numPlatforms_ ; n++)
    {
        delete allPlatforms[n];
    }    
}

void GameScreen::drawAllPlatforms()
{
    for (int n = 0; n < numPlatforms_ ; n++)
    {
        drawPlatform (allPlatforms[n]->x,allPlatforms[n]->y);
    } 
}

void GameScreen::eraseAllPlatforms()
{
    for (int n = 0; n < numPlatforms_ ; n++)
    {
        erasePlatform (allPlatforms[n]->y);
    } 
}

void GameScreen::shiftAllPlatformsUp()
{
    for (int n = 0; n < numPlatforms_ ; n++)
    {
        if (allPlatforms[n]->y > (platThickness_))
            allPlatforms[n]->y = allPlatforms[n]->y - 1;
        else
            allPlatforms[n]->y =  maxY_ - platThickness_ + 1; //send back to bottom.
    }
}