#include "Display.h"
#include "Sprites.h"
#include "Game.h"
#define PIN_BUTTON_DOWN 2
#define PIN_BUTTON_RIGHT 3
#define PIN_BUTTON_UP 4
#define PIN_BUTTON_LEFT 5
enum eDIRECTIONS {
DOWN,
RIGHT,
UP,
LEFT
};
void drawSprite(uint16_t *spriteArray, byte width, byte height, int16_t xCoord, int16_t yCoord, byte phase = 0, bool erase = false) {
// If erase parameter is true, draw black box with width/height of the sprite at coordinates
if (erase) {
tft.fillRect(xCoord, yCoord, width, height, ILI9341_BLACK);
} else {
// Skip variable for jumping to a certain point in a sprite array (to have multiple animation phases)
uint16_t skip = phase * width * height;
// Iterate over sprite array and display pixels on the display
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
uint16_t color = spriteArray[x + y * width + skip];
if (color != 0) {
tft.drawPixel(xCoord + x, yCoord + y, color);
}
}
}
}
}
int readButtons() {
for (int i = 0; i < 4; i++) {
if (!digitalRead(i + 2)) { //liest Knöpfe 2, 3 ,4 ,5 pins
return i;
}
}
return -1;
}
uint16_t arrowX = 136;
String userInput;
uint16_t petX = 150;
byte petSpeed = 5;
unsigned long systime, timestamp, waitTime;
// 0 = dog, 1 = cat
int pet;
String name;
void choosePet(){
tft.fillRect(100, 20, 320, 10, ILI9341_BLACK);
if(arrowX<100) {
drawSprite(dogSprite, 48, 52, 120, 94, 0);
pet = 0;
namePet();
}else if(arrowX>100) {
drawSprite(catSprite, 48, 52, 120, 94, 0);
pet = 1;
namePet();
}
}
void namePet(){
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.setCursor(100, 20);
tft.print("Menu ");
tft.setTextColor(ILI9341_WHITE);
tft.println("Name your pet");
tft.setCursor(60, 200);
tft.println("Use the serial monitor for input!");
bool chooseName = false;
while(chooseName == false){
if(Serial.available()>0){
userInput = Serial.readStringUntil('\n');
name = userInput;
if(userInput>" "){
tft.fillRect(60, 200, 320, 10, ILI9341_BLACK);
tft.fillRect(50, 190, 320, 10, ILI9341_BLACK);
tft.setCursor(50, 170);
tft.println("Are you sure you want to name your pet ");
tft.setCursor(50, 180);
tft.println(userInput + "?");
tft.setCursor(50, 200);
tft.println("Confirm your choice with the Up-button.");
tft.setCursor(50, 220);
tft.println("Reenter the name with the down-Button.");
//pause zum drücken vom Up-Button einbauen //keine Zeit Up-button zu drücken um funktion confirmGame() zu beginnen // kehrt wieder an den Anfang der while Schleife zurück
bool confirmed = false;
bool reenterName = false;
while(confirmed == false && reenterName == false){
systime = millis();
if(systime - timestamp >= 50) {
timestamp = systime;
int button = readButtons();
switch (button) {
case DOWN:
tft.fillRect(50, 170, 320, 60, ILI9341_BLACK);
namePet();
chooseName = true;
break;
case RIGHT:
break;
case UP:
chooseName = true;
Serial.println(button);
confirmed = true;
break;
case LEFT:
break;
}
}
}
}
//wenn up button gepresst dann while schleife verlassen
}
}
confirmGame();
}
void confirmGame() {
tft.fillRect(0, 200, 320, 210, ILI9341_BLACK);
tft.fillRect(0, 170, 320, 190, ILI9341_BLACK);
tft.setCursor(50, 180);
tft.println("Congrats your Pet's name from now on is "+ userInput);
tft.setCursor(50, 200);
tft.println("Press Up-Button to START the GAME!");
bool upPressed = false;
while(upPressed == false) {
systime = millis();
if (systime - timestamp >= 100) {
timestamp = systime;
int buttonRead = readButtons();
if (buttonRead > -1) {
// Increase / decrease coords depending on button pressed
switch (buttonRead) {
case DOWN:
Serial.println("Please presse the Up-Button to continue!");
break;
case RIGHT:
Serial.println("Please presse the Up-Button to continue!");
break;
case UP:
upPressed = true;
break;
case LEFT:
Serial.println("Please presse the Up-Button to continue!");
break;
}
}
}
}
startGame();
}
void startGame() {
tft.fillRect(1, 20, 318, 238, ILI9341_BLACK);
tft.drawRect(0, 0, tft.width(), tft.height(), tft.color565(80, 80, 80));
// Draw pet once before game starts
if(pet<1){
drawSprite(dogSprite, 48, 52, petX, 187, 0);
}else{drawSprite(catSprite, 48, 52, petX, 187, 0);}
bool runGame = true;
while(runGame == true) {
systime = millis();
if (systime - timestamp >= 50) {
timestamp = systime;
int button = readButtons();
if (button > -1) {
// Delete sprite at old coords
if(pet<1){
drawSprite(dogSprite, 48, 52, petX, 187, 0, true);
}else{drawSprite(catSprite, 48, 52, petX, 187, 0, true);}
//Border
if(petX<5) {
petX += petSpeed;
}else if(petX>269){
petX -= petSpeed;
}
// Increase / decrease coords depending on button pressed
switch (button) {
case DOWN:
Serial.println("Use the left or right buttons");
if(pet<1){
drawSprite(dogSprite, 48, 52, petX, 187, 0);
}else{drawSprite(catSprite, 48, 52, petX, 187, 0);}
break;
case RIGHT: //1
petX += petSpeed;
if(pet<1){
drawSprite(dogSprite, 48, 52, petX, 187, 0);
}else{drawSprite(catSprite, 48, 52, petX, 187, 0);}
break;
case UP:
Serial.println("Use the left or right buttons");
if(pet<1){
drawSprite(dogSprite, 48, 52, petX, 187, 1);
}else{drawSprite(catSprite, 48, 52, petX, 187, 1);}
break;
case LEFT: //3
petX -= petSpeed;
if(pet<1){
drawSprite(dogSprite, 48, 52, petX, 187, 1);
}else{drawSprite(catSprite, 48, 52, petX, 187, 1);}
break;
}
}
}
}
}
void setup()
{
Serial.begin(9600);
Serial.println("Welcome to Digital Pet");
randomSeed(analogRead(A0));
tft.begin();
tft.setRotation(1);
pinMode(PIN_BUTTON_UP, INPUT_PULLUP);
pinMode(PIN_BUTTON_DOWN, INPUT_PULLUP);
pinMode(PIN_BUTTON_LEFT, INPUT_PULLUP);
pinMode(PIN_BUTTON_RIGHT, INPUT_PULLUP);
// Draw border and text
tft.drawRect(0, 0, tft.width(), tft.height(), tft.color565(80, 80, 80));
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.setCursor(100, 20);
tft.print("Menu");
tft.setTextColor(ILI9341_WHITE);
tft.println(" Choose your pet");
drawSprite(dogSprite, 48, 52, 72, 94, 0);
drawSprite(catSprite, 48, 52, 200, 94, 0);
drawSprite(arrowSprite, 48, 52, arrowX, 160);
bool choosen = false;
while(choosen == false) {
systime = millis();
if (systime - timestamp >= 100) {
timestamp = systime;
int button = readButtons();
if (button > -1) {
// Delete sprite at old coords
drawSprite(arrowSprite, 48, 52, arrowX, 160, 0, true);
// Increase / decrease coords depending on button pressed
switch (button) {
case DOWN:
if(arrowX != 136) {
drawSprite(arrowSprite, 48, 52, 200, 160, 0, true);
drawSprite(arrowSprite, 48, 52, 72, 160, 0, true);
drawSprite(dogSprite, 48, 52, 72, 94, 0, true);
drawSprite(catSprite, 48, 52, 200, 94, 0, true);
choosen = true;
choosePet();
}else{
Serial.println("invalid please choose a pet!");
drawSprite(arrowSprite, 48, 52, arrowX, 160);
}
break;
case RIGHT:
arrowX =200;
drawSprite(arrowSprite, 48, 52, arrowX, 160);
break;
case UP:
if(arrowX != 136) {
drawSprite(arrowSprite, 48, 52, 200, 160, 0, true);
drawSprite(arrowSprite, 48, 52, 72, 160, 0, true);
drawSprite(dogSprite, 48, 52, 72, 94, 0, true);
drawSprite(catSprite, 48, 52, 200, 94, 0, true);
choosen = true;
choosePet();
}else{
Serial.println("invalid please choose a pet!");
drawSprite(arrowSprite, 48, 52, arrowX, 160);
}
break;
case LEFT:
arrowX = 72;
drawSprite(arrowSprite, 48, 52, arrowX, 160);
break;
}
}
}
}
}
void loop(){
}
/*
drawSprite(arrowSprite, 48, 52, arrowX, 160, 0, true);
drawSprite(dogSprite, 48, 52, 72, 94, 0, true);
drawSprite(catSprite, 48, 52, 200, 94, 0, true);
//delete all sprites
//Start actual game
//Choose Pet name
*/