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"
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- /* Defines */
- #define TFT_CS 10
- #define TFT_DC 9
- // Game Status defines
- #define START 0
- #define INGAME 1
- #define END 2
- ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC); // Screen object
- LiquidCrystal_I2C lcd1(0x3F, 16, 2);
- LiquidCrystal_I2C lcd2(0x3E, 16, 2);
- LiquidCrystal_I2C lcd3(0x3D, 16, 2);
- LiquidCrystal_I2C lcd4(0x27, 16, 2);
- LiquidCrystal_I2C lcd[4] = {lcd1,lcd2,lcd3,lcd4};
- // Unchangable vars
- const float Pi = 3.14159;
- const int gameSpeed = 25;
- const int omega = 180;
- /* Global Vars */
- float last = 0;
- byte gameMode = START; // Game Mode status var
- enum sides {
- none,
- left,
- right
- }; // Enum for the sides that player move.
- int pCount = 0; // Player count var
- int roundsCount = 0;
- // X Y theta
- const int player_pos[4][3] = {
- {160,25, 90},
- {160,215, 270},
- {25,120, 0},
- {295,120, 180}
- }; // Starting position of players
- const int player_color[] = { ILI9341_RED,ILI9341_GREEN,ILI9341_BLUE,ILI9341_YELLOW }; // players colors.
- const byte player_joystick[] = { A0, A2, A3, A1 } ;
- const byte player_jsw[] = { 6, 4, 6, 5 };
- /* Player Vars */
- bool justentered[4];
- bool ingame[4];
- bool alive[4];
- float x[4];
- float y[4];
- float theta[4] = {0,90,180,180};
- sides side[4];
- byte scores[4];
- byte cd = 0;
- void setup() {
- tft.begin(); // Intalize screen controller
- for(byte i = 0;i < 4;i++) {
- lcd[i].begin();
- lcd[i].backlight();
- pinMode(player_jsw[i], INPUT_PULLUP);
- }
- tft.setRotation(iliRotation270); // Rotate screen 270 degrees.
- gameRender(START); // Render Start screen.
- }
- void loop() {
- if(gameMode == START) { // Runs while on start screen.
- for(byte i = 0;i < 4;i++) {
- if(!digitalRead(player_jsw[i]) && i < 3 || digitalRead(player_jsw[i]) && i == 3) EnterGame(i);
- justentered[i] = 0;
- }
- if(pCount < 2) {
- Message("Not enough players.");
- cd = 10;
- return;
- }
- else {
- cd--;
- char str[64];
- sprintf(str,"starts in %d seconds",cd);
- Message(str);
- }
- if(cd == 0 && pCount >= 2) gameRender(INGAME); // Render game screen only when there is atleast 2 players ready.
- delay(1000);
- }
- if(gameMode == INGAME) {
- float mod = millis() - last;
- last = millis();
- mod /= 1000;
- uint16_t color;
- for(byte i=0;i <4;i++) { // Run 4 times ( 4 players ) skipping if the player not in game
- if(!alive[i])
- continue;
- color = tft.readPixel(x[i]+gameSpeed*cos(Pi*theta[i]/omega)*mod, y[i]+gameSpeed*sin(Pi*theta[i]/omega)*mod); // Get the pixel color of the player position on screen.
- if(color != 0 || x[i] < 0 || x[i] > 320 || y[i] < 0 || y[i] > 240)
- PlayerLose(i); // Check if the color of the pixel is not black or position on the end of the screen if it it's stop player movement and gives score also check for round end.
- if(side[i] != none) MovePlayer(side[i],mod,i); // Move player right/left
- RenderPlayer(mod,i); // Render Player trail.
- }
- for(byte i = 0;i < 4;i++) {
- if(!alive[i])
- continue;
- int amount = analogRead(player_joystick[i]);
- if(amount < 300) side[i] = left;
- else if(amount > 700) side[i] = right;
- else side[i] = none;
- }
- delay(1000/gameSpeed);
- }
- }
- void PlayerLose(byte player) {
- alive[player] = false;
- byte aliveCount = 0, winner = 0;
- for(byte i = 0;i < 4;i++) {
- if(!alive[i])
- continue;
- aliveCount++;
- winner = i;
- } // Check for winner and alive player count.
- scores[player] += 1*(pCount - aliveCount); // Set player score.
- if(aliveCount == 1) {
- scores[winner] += 1*pCount;
- if(roundsCount == 0) gameRender(END);
- else {
- gameRender(INGAME);
- roundsCount--;
- }
- }
- }
- void MovePlayer(sides sidess,float mod,int id) { // Move player to left or right.
- 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) { // Render player trail and move him forward.
- 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) { // Render the screens of the game such as the start/in game screen and end screen.
- tft.fillScreen(ILI9341_BLACK);
- gameMode = id; // set the game mode status in the var.
- switch(id) {
- case START: { // Start screen + reset rounds of game
- tft.setFont(Arial_bold_14);
- tft.setTextLetterSpacing(5);
- tft.setTextColor(ILI9341_WHITE, ILI9341_BLUE);
- tft.printAligned("Not enough players.",gTextAlignMiddleCenter);
- for(byte i = 0;i < 4;i++) {
- ingame[i] = false;
- tft.fillCircle(player_pos[i][0], player_pos[i][1], 20, ILI9341_RED); // Set every player to not ready.
- scores[i] = 0;
- }
- roundsCount = 5;
- }
- case INGAME: { // ingame screen
- last = millis();
- char str[50];
- for(byte i = 0;i <4;i++) {
- if(!ingame[i])
- continue;
- side[i] = none;
- x[i] = player_pos[i][0];
- y[i] = player_pos[i][1];
- theta[i] = player_pos[i][2];
- alive[i] = true;
- lcd[i].clear();
- sprintf(str,"Your Score: %d",scores[i]);
- lcd[i].print(str);
- lcd[i].setCursor(0,1);
- sprintf(str,"Rounds left: %d", roundsCount);
- lcd[i].print(str);
- } // Reset every player data.
- }
- }
- }
- void ErrorMessage(char string[]) { // Write error message * only works on start screen *
- if(gameMode == INGAME)
- return;
- tft.setTextLetterSpacing(3);
- tft.setTextColor(ILI9341_RED);
- tft.printAligned(string,gTextAlignBottomCenter,50);
- }
- void Message(char string[]) {
- if(gameMode == INGAME)
- return;
- tft.setFont(Arial_bold_14);
- tft.setTextLetterSpacing(5);
- tft.setTextColor(ILI9341_WHITE, ILI9341_BLUE);
- tft.printAligned(string,gTextAlignMiddleCenter);
- }
- void EnterGame(int player) { // Make player ready/unready * on start screen *
- if(gameMode != START || justentered[player])
- 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;
- }
- justentered[player] = 1;
- }
- /*
- 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][sad2] = 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