Advertisement
Hirsw0w

CodeArduinoProject

Oct 23rd, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 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. /* Defines */
  7. #define TFT_CS 10
  8. #define TFT_DC 9
  9.  
  10. ILI9341_due tft = ILI9341_due(TFT_CS, TFT_DC);
  11.  
  12. extern uint8_t SmallFont[];
  13. extern uint8_t BigFont[];
  14. /*
  15.  
  16. short pageId = 0;
  17.  
  18. int buttons[MAX_BUTTONS][5];
  19. short bCount = 0;
  20. short startGameButton = -1;
  21. */
  22.  
  23. /* Global Vars */
  24. float last = 0;
  25. const float Pi = 3.14159;
  26. const int gameSpeed = 25;
  27. const int omega = 180;
  28. byte gameMode = 0;
  29. enum sides {
  30. none,
  31. left,
  32. right
  33. };
  34.  
  35.  
  36. // X Y theta
  37. const int player_pos[4][3] = {
  38. {160,5, 90},
  39. {160,235, 270},
  40. {5,120, 0},
  41. {315,120, 180}
  42. };
  43.  
  44. const int player_color[] = { ILI9341_RED,ILI9341_GREEN,ILI9341_BLUE,ILI9341_YELLOW };
  45.  
  46. /* Player Vars */
  47. bool ingame[4] {false,false,false,false};
  48. float x[4];
  49. float y[4];
  50. float theta[4] = {0,90,180,180};
  51. sides side[4];
  52. byte scores[4];
  53.  
  54.  
  55. void setup() {
  56. Serial.begin(9600);
  57. tft.begin();
  58. tft.setRotation(iliRotation270);
  59. tft.fillScreen(ILI9341_BLACK);
  60. last = millis();
  61. ingame[0] = true;
  62.  
  63. tft.setFont(Arial_bold_14);
  64. tft.setTextLetterSpacing(5);
  65. tft.setTextColor(ILI9341_WHITE, ILI9341_BLUE);
  66. tft.printAligned("Start",gTextAlignMiddleCenter);
  67. }
  68.  
  69.  
  70. void loop() {
  71. if(gameMode == 0) {
  72. if (Serial.available() > 0) {
  73. // read the incoming byte:
  74. byte incomingByte = Serial.read();
  75. if(incomingByte == '1') ingame[0] = true;
  76. if(incomingByte == '2') ingame[1] = true;
  77. if(incomingByte == '3') ingame[2] = true;
  78. if(incomingByte == '4') ingame[3] = true;
  79. else if(incomingByte == 's') startGame();
  80.  
  81. }
  82. }
  83. if(gameMode == 1) {
  84. float mod = millis() - last;
  85. last = millis();
  86. mod /= 1000;
  87.  
  88. uint16_t color;
  89. for(byte i=0;i <4;i++) {
  90. if(ingame[i] != true)
  91. continue;
  92.  
  93.  
  94. color = tft.readPixel(x[i]+gameSpeed*cos(Pi*theta[i]/omega)*mod, y[i]+gameSpeed*sin(Pi*theta[i]/omega)*mod);
  95. if(color != 0) ingame[i] = false;
  96.  
  97. RenderPlayer(mod,i);
  98. if(side[i] != none) MovePlayer(side[i],mod,i);
  99. }
  100. if (Serial.available() > 0) {
  101.  
  102. // read the incoming byte:
  103. byte incomingByte = Serial.read();
  104. if(incomingByte == 'a'){
  105. if(side[0] == left) side[0] = none;
  106. else side[0] = left;
  107.  
  108. }
  109. else if(incomingByte == 'd') {
  110. if(side[0] == right) side[0] = none;
  111. else side[0] = right;
  112. }
  113. }
  114. }
  115. delay(1000/gameSpeed);
  116. }
  117.  
  118. void startGame() {
  119. tft.fillScreen(ILI9341_BLACK);
  120. last = millis();
  121. gameMode = 1;
  122. for(byte i = 0;i <4;i++) {
  123. scores[i] = 0;
  124. side[i] = none;
  125. x[i] = player_pos[i][0];
  126. y[i] = player_pos[i][1];
  127. theta[i] = player_pos[i][2];
  128. }
  129. }
  130.  
  131. void MovePlayer(sides sidess,float mod,int id) {
  132. if(sidess == left)
  133. theta[id] = theta[id] - omega*mod;
  134. else if(sidess == right)
  135. theta[id] = theta[id] + omega*mod;
  136. }
  137.  
  138. void RenderPlayer(float mod,byte id) {
  139. tft.fillRect(x[id],y[id],1,1,player_color[id]);
  140. x[id] += gameSpeed*cos(Pi*theta[id]/omega)*mod;
  141. y[id] += gameSpeed*sin(Pi*theta[id]/omega)*mod;
  142. }
  143. /*
  144. void PrintText(int x,int y) {
  145. tft.setCursor(x,y);
  146. tft.setTextColor(ILI9341_WHITE);
  147. tft.setTextSize(3);
  148. tft.println("Blue Wins");
  149. }
  150. */
  151. /*
  152.  
  153. void drawHomeScreen() {
  154. pageId = 1;
  155. myGLCD.setBackColor(0,0,0);
  156. PrintText("CatchMe",CENTER, 10,255,255,255,BigFont);
  157. startGameButton = CreateButton("Start Game",25,25,100,50, 255,255,255 , 0,0,0);
  158. }
  159.  
  160. void PrintText(text[], x,y,r = 255,g = 255,b = 255, font[] = SmallFont) {
  161. myGLCD.setColor(r,g,b);
  162. myGLCD.setFont(font);
  163. myGLCD.print(text,x,y);
  164. }
  165.  
  166. short CreateButton(text[], x,y, width,height, boxr = 255, boxg = 255,boxb = 255, r = 255, g = 255, b = 255) {
  167. myGLCD.setColor(boxr, boxg, boxb);
  168. myGLCD.fillRoundRect (x, y, width, height);
  169. myGLCD.setFont(BigFont);
  170. myGLCD.setBackColor(r, g, b);
  171. myGLCD.print(text, x, y);
  172. buttons[bCount][0] = x;
  173. buttons[bCount][1] = y;
  174. buttons[bCount][2] = width;
  175. buttons[bCount][3] = height;
  176. buttons[bCount][4] = pageId;
  177. return bCount++;
  178. }
  179.  
  180. void loop() {
  181. if (myTouch.dataAvailable()) {
  182. myTouch.read();
  183. x=myTouch.getX(); // X coordinate where the screen has been pressed
  184. y=myTouch.getY(); // Y coordinates where the screen has been pressed
  185.  
  186. short pressedButton = -1;
  187. for(short i = 0;i < MAX_BUTTONS;i++) {
  188. if(buttons[i][4] != pageId)
  189. continue;
  190.  
  191. if(x >= buttons[i][0] && x <= (buttons[i][0] + buttons[i][2]) && y >= buttons[i][1] && y <= (buttons[i][1] + buttons[i][3])) {
  192. pressedButton = i;
  193. break;
  194. }
  195. }
  196.  
  197. switch(pressedButton) {
  198. case startGameButton: {
  199. // Start Game
  200. }
  201. }
  202. }
  203. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement