Advertisement
Hirsw0w

Untitled

Nov 20th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Adafruit_FT6206.h>
  3. #include <ILI9341_due.h>
  4. #include <Adafruit_GFX.h>
  5. #include "fonts\Arial_bold_14.h"
  6. #include <Wire.h>
  7. #include <LiquidCrystal_I2C.h>
  8.  
  9. /* Defines */
  10. #define TFT_CS 10
  11. #define TFT_DC 9
  12.  
  13. // Game Status defines
  14. #define START 0
  15. #define INGAME 1
  16. #define END 2
  17.  
  18. ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC); // Screen object
  19. LiquidCrystal_I2C lcd1(0x3F, 16, 2);
  20. LiquidCrystal_I2C lcd2(0x3E, 16, 2);
  21. LiquidCrystal_I2C lcd3(0x3D, 16, 2);
  22. LiquidCrystal_I2C lcd4(0x3B, 16, 2);
  23. LiquidCrystal_I2C lcd[4] = {lcd1,lcd2,lcd3,lcd4};
  24. /*
  25.  
  26. short pageId = 0;
  27.  
  28. int buttons[MAX_BUTTONS][5];
  29. short bCount = 0;
  30. short startGameButton = -1;
  31. */
  32.  
  33. // Unchangable vars
  34.  
  35. const float Pi = 3.14159;
  36. const int gameSpeed = 20;
  37. const int omega = 180;
  38.  
  39. /* Global Vars */
  40. byte gameMode = START; // Game Mode status var
  41. enum sides {
  42. none,
  43. left,
  44. right
  45. }; // Enum for the sides that player move.
  46.  
  47. int pCount = 0; // Player count var
  48. int roundsCount = 0;
  49.  
  50.  
  51. // X Y theta
  52. const int player_pos[4][3] = {
  53. {160,30, 90},
  54. {160,210, 270},
  55. {25,120, 0},
  56. {295,120, 180}
  57. }; // Starting position of players
  58.  
  59. const int player_color[] = { ILI9341_RED,ILI9341_GREEN,ILI9341_BLUE,ILI9341_YELLOW }; // players colors.
  60.  
  61. /* Player Vars */
  62. bool ingame[4];
  63. bool alive[4];
  64. float x[4];
  65. float y[4];
  66. int theta[4] = {90,270,0,180};
  67. sides side[4];
  68. byte scores[4];
  69. float last[4];
  70.  
  71. void setup() {
  72. Serial.begin(9600); // Terminal for debugging.
  73. tft.begin(); // Intalize screen controller
  74. for(byte i = 0;i < 4;i++) {
  75. lcd[i].begin();
  76. lcd[i].backlight();
  77. }
  78. tft.setRotation(iliRotation270); // Rotate screen 270 degrees.
  79. gameRender(START); // Render Start screen.
  80. }
  81.  
  82.  
  83. void loop() {
  84. if(gameMode == START) { // Runs while on start screen.
  85. if (Serial.available() > 0) {
  86. byte incomingByte = Serial.read();
  87. if(incomingByte == '1') EnterGame(0);
  88. if(incomingByte == '2') EnterGame(1);
  89. if(incomingByte == '3') EnterGame(2);
  90. if(incomingByte == '4') EnterGame(3);
  91. else if(incomingByte == 's') {
  92. if(pCount < 2) {
  93. ErrorMessage("Not enough players.");
  94. return;
  95. }
  96. gameRender(INGAME); // Render game screen only when there is atleast 2 players ready.
  97. }
  98. }
  99. }
  100. if(gameMode == INGAME) {
  101. uint16_t color;
  102. for(byte i=0;i <4;i++) { // Run 4 times ( 4 players ) skipping if the player not in game
  103. if(!alive[i])
  104. continue;
  105.  
  106. float mod = millis() - last[i];
  107. last[i] = millis();
  108. mod /= 1000;
  109. 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.
  110. if(color != 0 || x[i] < 0 || x[i] > 320 || y[i] < 0 || y[i] > 240)
  111. 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.
  112.  
  113. if(side[i] != none) MovePlayer(side[i],mod,i); // Move player right/left
  114. RenderPlayer(mod,i); // Render Player trail.
  115. }
  116. if (Serial.available() > 0) {
  117. byte incomingByte = Serial.read();
  118. if(incomingByte == 'a'){
  119. if(side[0] == left) side[0] = none;
  120. else side[0] = left; // Set player side changing to left.
  121.  
  122. }
  123. else if(incomingByte == 'd') {
  124. if(side[0] == right) side[0] = none;
  125. else side[0] = right; // Set player side changing to Right.
  126. }
  127. }
  128. }
  129. delay(1000/gameSpeed);
  130. }
  131.  
  132. void PlayerLose(byte player) {
  133. alive[player] = false;
  134. byte aliveCount = 0, winner = 0;
  135. for(byte i = 0;i < 4;i++) {
  136. if(!alive[i])
  137. continue;
  138.  
  139. aliveCount++;
  140. winner = i;
  141. } // Check for winner and alive player count.
  142.  
  143. scores[player] += 1*(pCount - aliveCount); // Set player score.
  144.  
  145. if(aliveCount == 1) {
  146. scores[winner] += 1*pCount;
  147. if(roundsCount == 0) gameRender(END);
  148. else {
  149. gameRender(INGAME);
  150. roundsCount--;
  151. }
  152. }
  153. }
  154.  
  155. void MovePlayer(sides sidess,float mod,int id) { // Move player to left or right.
  156. if(sidess == left)
  157. theta[id] = theta[id] - omega*mod;
  158. else if(sidess == right)
  159. theta[id] = theta[id] + omega*mod;
  160. }
  161.  
  162. void RenderPlayer(float mod,byte id) { // Render player trail and move him forward.
  163. if(!alive[id])
  164. return;
  165.  
  166. tft.fillRect(x[id],y[id],1,1,player_color[id]);
  167. x[id] += gameSpeed*cos(Pi*theta[id]/omega)*mod;
  168. y[id] += gameSpeed*sin(Pi*theta[id]/omega)*mod;
  169. }
  170.  
  171. void gameRender(int id) { // Render the screens of the game such as the start/in game screen and end screen.
  172. tft.fillScreen(ILI9341_BLACK);
  173. gameMode = id; // set the game mode status in the var.
  174. switch(id) {
  175. case START: { // Start screen + reset rounds of game
  176. tft.setFont(Arial_bold_14);
  177. tft.setTextLetterSpacing(5);
  178. tft.setTextColor(ILI9341_WHITE, ILI9341_BLUE);
  179. tft.printAligned("Start",gTextAlignMiddleCenter);
  180.  
  181. for(byte i = 0;i < 4;i++) {
  182. ingame[i] = false;
  183. tft.fillCircle(player_pos[i][0], player_pos[i][1], 20, ILI9341_RED); // Set every player to not ready.
  184. scores[i] = 0;
  185. }
  186. roundsCount = 5;
  187. }
  188. case INGAME: { // ingame screen
  189. char str[50];
  190. for(byte i = 0;i <4;i++) {
  191. if(!ingame[i])
  192. continue;
  193.  
  194. side[i] = none;
  195. x[i] = float(player_pos[i][0]);
  196. y[i] = float(player_pos[i][1]);
  197. theta[i] = player_pos[i][2];
  198. alive[i] = true;
  199. lcd[i].clear();
  200. sprintf(str,"Your Score: %d",scores[i]);
  201. lcd[i].print(str);
  202. sprintf(str,"Rounds Left: %d",roundsCount-1);
  203. lcd[i].setCursor(0,1);
  204. lcd[i].print(str);
  205. last[i] = millis();
  206. } // Reset every player data.
  207. }
  208. }
  209. }
  210.  
  211. void ErrorMessage(char string[]) { // Write error message * only works on start screen *
  212. if(gameMode != START)
  213. return;
  214.  
  215. tft.setTextLetterSpacing(3);
  216. tft.setTextColor(ILI9341_RED);
  217. tft.printAligned(string,gTextAlignBottomCenter,50);
  218. }
  219.  
  220. void EnterGame(int player) { // Make player ready/unready * on start screen *
  221. if(gameMode != START)
  222. return;
  223.  
  224. if(ingame[player]) {
  225. pCount--;
  226. tft.fillCircle(player_pos[player][0], player_pos[player][1], 20, ILI9341_RED);
  227. ingame[player] = false;
  228. }
  229. else {
  230. pCount++;
  231. tft.fillCircle(player_pos[player][0], player_pos[player][1], 20, ILI9341_GREEN);
  232. ingame[player] = true;
  233. }
  234. }
  235.  
  236. /*
  237. void PrintText(int x,int y) {
  238. tft.setCursor(x,y);
  239. tft.setTextColor(ILI9341_WHITE);
  240. tft.setTextSize(3);
  241. tft.println("Blue Wins");
  242. }
  243. */
  244. /*
  245.  
  246. void drawHomeScreen() {
  247. pageId = 1;
  248. myGLCD.setBackColor(0,0,0);
  249. PrintText("CatchMe",CENTER, 10,255,255,255,BigFont);
  250. startGameButton = CreateButton("Start Game",25,25,100,50, 255,255,255 , 0,0,0);
  251. }
  252.  
  253. void PrintText(text[], x,y,r = 255,g = 255,b = 255, font[] = SmallFont) {
  254. myGLCD.setColor(r,g,b);
  255. myGLCD.setFont(font);
  256. myGLCD.print(text,x,y);
  257. }
  258.  
  259. short CreateButton(text[], x,y, width,height, boxr = 255, boxg = 255,boxb = 255, r = 255, g = 255, b = 255) {
  260. myGLCD.setColor(boxr, boxg, boxb);
  261. myGLCD.fillRoundRect (x, y, width, height);
  262. myGLCD.setFont(BigFont);
  263. myGLCD.setBackColor(r, g, b);
  264. myGLCD.print(text, x, y);
  265. buttons[bCount][0] = x;
  266. buttons[bCount][1] = y;
  267. buttons[bCount][sad2] = width;
  268. buttons[bCount][3] = height;
  269. buttons[bCount][4] = pageId;
  270. return bCount++;
  271. }
  272.  
  273. void loop() {
  274. if (myTouch.dataAvailable()) {
  275. myTouch.read();
  276. x=myTouch.getX(); // X coordinate where the screen has been pressed
  277. y=myTouch.getY(); // Y coordinates where the screen has been pressed
  278.  
  279. short pressedButton = -1;
  280. for(short i = 0;i < MAX_BUTTONS;i++) {
  281. if(buttons[i][4] != pageId)
  282. continue;
  283.  
  284. if(x >= buttons[i][0] && x <= (buttons[i][0] + buttons[i][2]) && y >= buttons[i][1] && y <= (buttons[i][1] + buttons[i][3])) {
  285. pressedButton = i;
  286. break;
  287. }
  288. }
  289.  
  290. switch(pressedButton) {
  291. case startGameButton: {
  292. // Start Game
  293. }
  294. }
  295. }
  296. }
  297. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement