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-03-10
- Revision:
- 6:b1c54f8b28fe
- Parent:
- 5:d395a7134278
- Child:
- 8:ebddb721f1ee
File content as of revision 6:b1c54f8b28fe:
#include "mbed.h" #include "N5110.h" #include "GameScreen.h" //need to extend on initialization to set some variables void GameScreen::Initialize(Ball ball) { init(); playerBall = ball; } //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_)) allPlatforms[n]->y = allPlatforms[n]->y - 1; else { allPlatforms[n]->y = maxY_ - platThickness_ + 1; //send back to bottom. //allPlatforms[n]->x = rand() % (maxX_-platGapSize_ + 1) - 1; //select a new random position of gap! } } } Platform GameScreen::nextClosestPlatform(int y) { int closestY = 15; Platform closestPlatform; for (int i = 0; i < numPlatforms_; i++) { if ((allPlatforms[i]->y - y) < closestY && (allPlatforms[i]->y - y) >= -1) { closestY = allPlatforms[i]->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) { while (*str) { //each byte in row for (int i = 0; i < 5; i++) { //each bit in byte for (int b = 1; i < 8; b++) { if (font5x7[(*str - 32)*5 + i] & (2^b)) //bitwise comparison { setPixel(y+b-1,x-i); } else clearPixel(y+b-1,x-i); } } str++; } refresh(); }