Advertisement
Hirsw0w

NewCode30/10/2017

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