Advertisement
Hirsw0w

new code Hirsw0w

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