Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 mbed PowerControl
GameScreen.cpp
- Committer:
- AppleJuice
- Date:
- 2015-04-27
- Revision:
- 11:c6c88617f7e7
- Parent:
- 10:d8ef8633bd98
- Child:
- 12:9ba4b21f8220
File content as of revision 11:c6c88617f7e7:
#include "mbed.h"
#include "N5110.h"
#include "GameScreen.h"
//need to extend on initialization to set some variables
void GameScreen::Initialize()
{
init();
playerBall.x = maxX_/2;
playerBall.y = maxY_ - (int)(maxY_/3 * 2) - 5;
createAllPlatforms();
}
//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()
{
//more elegant ways to do this...but this is most efficient
setPixel(playerBall.y,playerBall.x+1);
setPixel(playerBall.y,playerBall.x+2);
setPixel(playerBall.y+1,playerBall.x);
setPixel(playerBall.y+1,playerBall.x+1);
setPixel(playerBall.y+1,playerBall.x+2);
setPixel(playerBall.y+1,playerBall.x+3);
setPixel(playerBall.y+2,playerBall.x);
setPixel(playerBall.y+2,playerBall.x+1);
setPixel(playerBall.y+2,playerBall.x+2);
setPixel(playerBall.y+2,playerBall.x+3);
setPixel(playerBall.y+3,playerBall.x+1);
setPixel(playerBall.y+3,playerBall.x+2);
}
//draw the player ball where (x,y) is top right corner.
void GameScreen::eraseBall()
{
clearPixel(playerBall.y,playerBall.x+1);
clearPixel(playerBall.y,playerBall.x+2);
clearPixel(playerBall.y+1,playerBall.x);
clearPixel(playerBall.y+1,playerBall.x+1);
clearPixel(playerBall.y+1,playerBall.x+2);
clearPixel(playerBall.y+1,playerBall.x+3);
clearPixel(playerBall.y+2,playerBall.x);
clearPixel(playerBall.y+2,playerBall.x+1);
clearPixel(playerBall.y+2,playerBall.x+2);
clearPixel(playerBall.y+2,playerBall.x+3);
clearPixel(playerBall.y+3,playerBall.x+1);
clearPixel(playerBall.y+3,playerBall.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::shiftAllPlatforms()
{
for (int n = 0; n < numPlatforms_ ; n++)
{
if (allPlatforms[n]->y > (platThickness_+6))
allPlatforms[n]->y = allPlatforms[n]->y - 1;
else
{
allPlatforms[n]->y = maxY_ - platThickness_ + 1+6; //send back to bottom.
allPlatforms[n]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap!
}
}
}
Platform GameScreen::nextClosestPlatform(int y)
{
Serial serial(USBTX,USBRX);
int closestY = 100;
Platform closestPlatform;
for (int i = 0; i < numPlatforms_; i++)
{
if (((allPlatforms[i]->y - y) < closestY) && ((allPlatforms[i]->y - y) > -1))
{
closestY = allPlatforms[i]->y-y;
closestPlatform = *allPlatforms[i];
}
}
return closestPlatform;
}
//@Override of base class function
//had to override and rewrite since N5110 is written to write horizontal this needs vertical
// this function reads existing font5x7 and rotate matrix to become font7x5 matrix.
//
// for example the number 4 font5x7 = 0x18,0x14,0x12,0x7F,0x10
// font7x5 = 0x02,0x06,0x10,0x12,0x1E,0x2,0x2
//then it is printed!
void GameScreen::printString(const char * str,int x,int y,bool invert)
{
if (invert){
for (int a = 0; a < 48; a ++)
{
for (int b = y; b < (y+9); b++)
{
setPixel(b,a);
}
}
}
while (*str)
{
//each byte in row
for (int i = 0; i < 5; i++)
{
//each bit in byte
for (int b = 0; b < 7; b++)
{
if (invert)
setPixel(y+b+1,x-i+1);
if (font5x7[(*str - 32)*5 + i] & (1<<b)) //bitwise comparison to mask at desired pixel
{
setPixel(y+b+1,x-i+1);
if (invert)
clearPixel(y+b+1,x-i+1);
}
else
{
if (!invert)
clearPixel(y+b+1,x-i+1);
}
}
}
str ++;
x -= 6;
}
refresh();
}
void GameScreen::displayStartScreen()
{
clear();
printString("LET",maxX_ - 14,-1);
printString("THE BALL",maxX_ - 2,7);
printString("DROP!",maxX_ - 12,16);
printString("This way",maxX_ - 2,30);
printString("------->",maxX_ - 2,37);
printString("TO PLAY!",maxX_ - 2,45);
printString("by: ",maxX_ - 2,61);
printString("Thomas",maxX_ - 9,69);
printString("Davies",maxX_ - 9,76);
refresh();
}
void GameScreen::displayInstructionScreen()
{
clear();
printString("Hi Ball,",maxX_ - 2,-1);
printString("You are",maxX_ - 2,7);
printString("TRAPPED!",maxX_ - 2,15);
printString("Use the ",maxX_ - 2,23);
printString("joystick",maxX_ - 2,32);
printString("to move.",maxX_ - 2,39);
printString("Don't",maxX_ - 2,47);
printString("hit the",maxX_ - 2,55);
printString("roof!",maxX_ - 2,63);
printString("------->",maxX_ - 2,75);
refresh();
}
void GameScreen::displayCountdown()
{
clear();
Timer countdown;
char buffer[10];
countdown.start();
printString("Survive!",maxX_ - 2,10);
int prevTime = -1;
while (countdown.read() < 5)
{
if (floor(countdown.read()) > prevTime)
{
sprintf(buffer,"..%1.0f..",(5 - floor(countdown.read())));
printString(buffer,maxX_ - 10,20 + 10*countdown.read());
prevTime = countdown.read();
}
}
refresh();
countdown.stop();
}
void GameScreen::displayEndScreen(int lvl,int *cursor)
{
clear();
char buffer [10];
sprintf(buffer,"lvl: %d",lvl);
printString("GameOver",maxX_ - 2,-1);
printString(" :( ",maxX_ - 2,7);
printString(buffer,maxX_ - 2,23);
printString("Reached!",maxX_ - 2,32);
printString("Record",maxX_ - 8,55,cursor[0]);
printString("View",maxX_ - 15,63,cursor[1]);
printString("Quit",maxX_ - 15,71,cursor[2]);
refresh();
}