Advertisement
Guest User

Snake game

a guest
Nov 26th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Adafruit_GFX.h> // Core graphics library
  3. #include <Adafruit_ST7735.h> // Hardware-specific library
  4. #include <SPI.h>
  5.  
  6. // standard U of A library settings, assuming Atmel Mega SPI pins
  7. #define TFT_CS 6 // Chip select line for TFT display
  8. #define TFT_DC 7 // Data/command line for TFT
  9. #define TFT_RST 8 // Reset line for TFT (or connect to +5V)
  10.  
  11. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  12.  
  13. // specify pins
  14. const int VERT = 0;
  15. const int HORIZ = 1;
  16. const int SEL = 9;
  17.  
  18. void game_over() {
  19. tft.fillScreen(0x0000);
  20. tft.setCursor(64, 80);
  21. tft.setTextColor(0xF800, 0xF800);
  22. tft.setTextSize(3);
  23. tft. setTextWrap(true);
  24. print("GAME OVER!");
  25. }
  26.  
  27.  
  28. int main() {
  29. init();
  30.  
  31. // setup
  32. pinMode(SEL, INPUT);
  33. digitalWrite(SEL, HIGH)
  34.  
  35. Serial.begin(9600);
  36.  
  37. // if analog pin 1 is unconnected it will cause randomSeed to generate
  38. // different seed numbers each time the program runs
  39. randomSeed(analogRead(1));
  40.  
  41. tft.initR(INITR_REDTAB); // initialize a ST7735 chip
  42.  
  43. // set the background to black
  44. tft.fillScreen(0x0000);
  45.  
  46. int init_vert, init_horiz;
  47. init_vert = analogRead(VERT);
  48. init_horiz = analogRead(HORIZ);
  49.  
  50. int cursor_x = 64, cursor_y = 80; // initial cursor position
  51. int old_cursor_x = cursor_x; // useful for storing old cursor position
  52. int old_cursor_y = cursor_y;
  53.  
  54. // loop
  55. while (true) {
  56. int vertical, horizontal, select;
  57.  
  58. vertical = analogRead(VERT); // will be 0-1023
  59. horizontal = analogRead(HORIZ) // will be 0-1023
  60. select = digitalRead(SEL); // HIGH if not pressed, LOW otherwise
  61.  
  62. int delta_vert, delta_horiz;
  63.  
  64. delta_vert = vertical - init_vert; // vertical change in joystick
  65. delta_horiz = horizontal - init_horiz; // horizontal change in joystick
  66.  
  67. cursor_x = cursor_x - delta_horiz/200; // maps to change of cursor
  68. cursor_y = cursor_y - delta_vert/200;
  69.  
  70. // check if the current location is different than the previous
  71. if (cursor_x != old_cursor_x || cursor_y != old_cursor_y) {
  72. tft.fillRect (old_cursor_x, old_cursor_y, 2, 2, 0x0000)
  73. }
  74.  
  75. // draw the cursor in current location
  76. tft.fillRect(cursor_x, cursor_y, 2, 2, 0x07E0);
  77.  
  78. // if the cursor is at the screen edges, show game over screen
  79. if (cursor_x >= 128 || cursor_x <= 0) {
  80. game_over();
  81. }
  82. if (cursor_y >= 160 || cursor_y <= 0) {
  83. game_over();
  84. }
  85.  
  86. // update the point's previous location
  87. old_cursor_x = cursor_x;
  88. old_cursor_y = cursor_y;
  89.  
  90. delay(50); // to make sure the cursor is moving smoothly
  91.  
  92. // need random x and y coordinates for the "apples" appear
  93. rand_apple_x = random(1,128);
  94. rand_apple_y = random(1,160);
  95. tft.fillRect (rand_apple_x, rand_apple_y, 2, 2, 0xF800);
  96.  
  97. // add a trailing rect behind the cursor when it touches the apple
  98. // and make the apple disappear and make another one appear immediately
  99. // in a random place on the screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement