Since i already had a pixel moving around my screen i added a frame to it:
Sketchi.bin (56.3 KB)
A-button increases the speed
B-button clears the screen
edit, updated using tips from Pharap. Added a gamecondition to display title, as in the Lost game.
#include "Pokitto.h"
#include "Background.h"
#include "Title.h"
enum class GameState //This sets up a class that limits the possibilities to the items in it.
{
TitleScreen,
Gameplay,
};
// Create an instance of Pokitto::Core
Pokitto::Core pokitto;
//declare variables
int step = 1;
int positionX = 15;
int positionY = 15;
GameState gameState = GameState::TitleScreen; //sets the gamestate to start in the titlescreen ?
const int screenW = Pokitto::Display::getWidth() - 1;//the width of the screen - 1
const int screenH = Pokitto::Display::getHeight() - 1;//the Height of the screen - 1
// Predeclare functions so they can be called as required, create them so they can be called later
void updateTitleScreen(void);
void drawTitleScreen(void);
void updateGameplay(void);
void drawGameplay(void);
int main(void)
{
pokitto.begin();
// When 1 does not clear the screen, hence the pixel leaves a track
pokitto.display.persistence = 1;
pokitto.display.load565Palette(Etch_pal);
//Set the grey colour of the frame as invisible
pokitto.display.setInvisibleColor(0);
//set frame rate to 40 instead of the default 20
pokitto.setFrameRate(40);
while(pokitto.isRunning())
{
if(pokitto.update())
{
// Update logic
switch(gameState)
{
case GameState::TitleScreen: updateTitleScreen(); break;
case GameState::Gameplay: updateGameplay(); break;
}
// Drawing logic
switch(gameState)
{
case GameState::TitleScreen: drawTitleScreen(); break;
case GameState::Gameplay: drawGameplay(); break;
}
}
}
}
void updateTitleScreen(void)
{
//to update the titlescreen, like movement or something ?
}
void drawTitleScreen(void)
{
// Your titlescreen code here
pokitto.display.drawBitmap(0,0,Etch_title);//Title
// Move to gameplay when A is pressed
if (pokitto.buttons.repeat(BTN_A, 0))
{
pokitto.display.clear();//clear the display so the title screen disappears, since the persistence was set to 1
gameState = GameState::Gameplay; //set the gamestate to gameplay
}
}
void updateGameplay()
{
// Input handling
if (pokitto.buttons.repeat(BTN_LEFT, 0))
{
// Don't go below zero
if(positionX >= step)
positionX = positionX - step; //or positionX -=step; // better get used to this..
else
positionX = 0;
}
if (pokitto.buttons.repeat(BTN_RIGHT, 0))
{
const int difference = screenW - positionX;//e.g. 220 - 100 = 120
if(difference >= step)//120 is larger then step so execute next line
positionX = positionX + step;//i like the readability of this better.
else
positionX = screenW;
}
if (pokitto.buttons.repeat(BTN_UP, 0))
{
// Don't go below zero
if(positionY >= step)
positionY = positionY - step;
else
positionY = 0;
}
if (pokitto.buttons.repeat(BTN_DOWN, 0))
{
const int difference = screenH - positionY;
if(difference >= step)
positionY = positionY + step;
else
positionY = screenH;
}
// Pressing the A button increases the speed by increasing the step value
if (pokitto.buttons.repeat(BTN_A, 0))
{
if(step == 1) //if A button pressed check if step equals 1, if yes change it to 5
step = 5;
}
else
{
step = 1;
}
// Brackets are not needed with 1 line being executed when the if is true
if (pokitto.buttons.repeat(BTN_B, 0))
pokitto.display.clear();
}
void drawGameplay(void)
{
// Background first, everything else is drawn on top of that
pokitto.display.drawBitmap(0, 0, Etch);
pokitto.display.setColor(2);
// Draw the pixel
pokitto.display.drawPixel(positionX, positionY);
}