Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Adafruit_FT6206.h>
- #include <ILI9341_due.h>
- #include <Adafruit_GFX.h>
- #include "fonts\Arial_bold_14.h"
- /* Defines */
- #define TFT_CS 10
- #define TFT_DC 9
- #define START 0
- #define INGAME 1
- #define END 2
- // Variables
- ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC);
- extern uint8_t SmallFont[];
- extern uint8_t BigFont[];
- /*
- short pageId = 0;
- int buttons[MAX_BUTTONS][5];
- short bCount = 0;
- short startGameButton = -1;
- */
- /* Global Vars */
- float last = 0;
- const float Pi = 3.14159;
- const int gameSpeed = 25;
- const int omega = 180;
- byte gameMode = 0;
- enum sides {
- none,
- left,
- right
- };
- int pCount = 0;
- // X Y theta
- const int player_pos[4][3] = {
- {160,25, 90},
- {160,215, 270},
- {25,120, 0},
- {295,120, 180}
- };
- const int player_color[] = { ILI9341_RED,ILI9341_GREEN,ILI9341_BLUE,ILI9341_YELLOW };
- /* Player Vars */
- bool ingame[4] {false,false,false,false};
- float x[4];
- float y[4];
- float theta[4] = {0,90,180,180};
- sides side[4];
- byte scores[4];
- void setup() {
- Serial.begin(9600);
- tft.begin();
- tft.setRotation(iliRotation270);
- gameRender(START);
- }
- void loop() {
- if(gameMode == START) {
- if (Serial.available() > 0) {
- // read the incoming byte:
- byte incomingByte = Serial.read();
- if(incomingByte == '1') EnterGame(0);
- if(incomingByte == '2') EnterGame(1);
- if(incomingByte == '3') EnterGame(2);
- if(incomingByte == '4') EnterGame(3);
- else if(incomingByte == 's') {
- if(pCount < 2) {
- ErrorMessage("Not enough players.");
- return;
- }
- gameRender(INGAME);
- }
- }
- }
- if(gameMode == INGAME) {
- float mod = millis() - last;
- last = millis();
- mod /= 1000;
- uint16_t color;
- for(byte i=0;i <4;i++) {
- if(ingame[i] != true)
- continue;
- color = tft.readPixel(x[i]+gameSpeed*cos(Pi*theta[i]/omega)*mod, y[i]+gameSpeed*sin(Pi*theta[i]/omega)*mod);
- if(color != 0) ingame[i] = false;
- RenderPlayer(mod,i);
- if(side[i] != none) MovePlayer(side[i],mod,i);
- }
- if (Serial.available() > 0) {
- // read the incoming byte:
- byte incomingByte = Serial.read();
- if(incomingByte == 'a'){
- if(side[0] == left) side[0] = none;
- else side[0] = left;
- }
- else if(incomingByte == 'd') {
- if(side[0] == right) side[0] = none;
- else side[0] = right;
- }
- }
- }
- delay(1000/gameSpeed);
- }
- void MovePlayer(sides sidess,float mod,int id) {
- if(sidess == left)
- theta[id] = theta[id] - omega*mod;
- else if(sidess == right)
- theta[id] = theta[id] + omega*mod;
- }
- void RenderPlayer(float mod,byte id) {
- tft.fillRect(x[id],y[id],1,1,player_color[id]);
- x[id] += gameSpeed*cos(Pi*theta[id]/omega)*mod;
- y[id] += gameSpeed*sin(Pi*theta[id]/omega)*mod;
- }
- void gameRender(int id) {
- tft.fillScreen(ILI9341_BLACK);
- gameMode = id;
- switch(id) {
- case START: {
- tft.setFont(Arial_bold_14);
- tft.setTextLetterSpacing(5);
- tft.setTextColor(ILI9341_WHITE, ILI9341_BLUE);
- tft.printAligned("Start",gTextAlignMiddleCenter);
- for(byte i = 0;i < 4;i++) ingame[i] = false, tft.fillCircle(player_pos[i][0], player_pos[i][1], 20, ILI9341_RED);
- }
- case INGAME: {
- last = millis();
- for(byte i = 0;i <4;i++) {
- scores[i] = 0;
- side[i] = none;
- x[i] = player_pos[i][0];
- y[i] = player_pos[i][1];
- theta[i] = player_pos[i][2];
- }
- }
- }
- }
- void ErrorMessage(char string[]) {
- if(gameMode != START)
- return;
- tft.setTextLetterSpacing(3);
- tft.setTextColor(ILI9341_RED);
- tft.printAligned(string,gTextAlignBottomCenter,50);
- }
- void EnterGame(int player) {
- if(gameMode != START)
- return;
- if(ingame[player]) {
- pCount--;
- tft.fillCircle(player_pos[player][0], player_pos[player][1], 20, ILI9341_RED);
- ingame[player] = false;
- }
- else {
- pCount++;
- tft.fillCircle(player_pos[player][0], player_pos[player][1], 20, ILI9341_GREEN);
- ingame[player] = true;
- }
- }
- /*
- void PrintText(int x,int y) {
- tft.setCursor(x,y);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(3);
- tft.println("Blue Wins");
- }
- */
- /*
- void drawHomeScreen() {
- pageId = 1;
- myGLCD.setBackColor(0,0,0);
- PrintText("CatchMe",CENTER, 10,255,255,255,BigFont);
- startGameButton = CreateButton("Start Game",25,25,100,50, 255,255,255 , 0,0,0);
- }
- void PrintText(text[], x,y,r = 255,g = 255,b = 255, font[] = SmallFont) {
- myGLCD.setColor(r,g,b);
- myGLCD.setFont(font);
- myGLCD.print(text,x,y);
- }
- short CreateButton(text[], x,y, width,height, boxr = 255, boxg = 255,boxb = 255, r = 255, g = 255, b = 255) {
- myGLCD.setColor(boxr, boxg, boxb);
- myGLCD.fillRoundRect (x, y, width, height);
- myGLCD.setFont(BigFont);
- myGLCD.setBackColor(r, g, b);
- myGLCD.print(text, x, y);
- buttons[bCount][0] = x;
- buttons[bCount][1] = y;
- buttons[bCount][2] = width;
- buttons[bCount][3] = height;
- buttons[bCount][4] = pageId;
- return bCount++;
- }
- void loop() {
- if (myTouch.dataAvailable()) {
- myTouch.read();
- x=myTouch.getX(); // X coordinate where the screen has been pressed
- y=myTouch.getY(); // Y coordinates where the screen has been pressed
- short pressedButton = -1;
- for(short i = 0;i < MAX_BUTTONS;i++) {
- if(buttons[i][4] != pageId)
- continue;
- if(x >= buttons[i][0] && x <= (buttons[i][0] + buttons[i][2]) && y >= buttons[i][1] && y <= (buttons[i][1] + buttons[i][3])) {
- pressedButton = i;
- break;
- }
- }
- switch(pressedButton) {
- case startGameButton: {
- // Start Game
- }
- }
- }
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement