Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_ST7735.h> // Hardware-specific library
- #include <SPI.h>
- // standard U of A library settings, assuming Atmel Mega SPI pins
- #define TFT_CS 6 // Chip select line for TFT display
- #define TFT_DC 7 // Data/command line for TFT
- #define TFT_RST 8 // Reset line for TFT (or connect to +5V)
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
- // specify pins
- const int VERT = 0;
- const int HORIZ = 1;
- const int SEL = 9;
- void game_over() {
- tft.fillScreen(0x0000);
- tft.setCursor(64, 80);
- tft.setTextColor(0xF800, 0xF800);
- tft.setTextSize(3);
- tft. setTextWrap(true);
- print("GAME OVER!");
- }
- int main() {
- init();
- // setup
- pinMode(SEL, INPUT);
- digitalWrite(SEL, HIGH)
- Serial.begin(9600);
- // if analog pin 1 is unconnected it will cause randomSeed to generate
- // different seed numbers each time the program runs
- randomSeed(analogRead(1));
- tft.initR(INITR_REDTAB); // initialize a ST7735 chip
- // set the background to black
- tft.fillScreen(0x0000);
- int init_vert, init_horiz;
- init_vert = analogRead(VERT);
- init_horiz = analogRead(HORIZ);
- int cursor_x = 64, cursor_y = 80; // initial cursor position
- int old_cursor_x = cursor_x; // useful for storing old cursor position
- int old_cursor_y = cursor_y;
- // loop
- while (true) {
- int vertical, horizontal, select;
- vertical = analogRead(VERT); // will be 0-1023
- horizontal = analogRead(HORIZ) // will be 0-1023
- select = digitalRead(SEL); // HIGH if not pressed, LOW otherwise
- int delta_vert, delta_horiz;
- delta_vert = vertical - init_vert; // vertical change in joystick
- delta_horiz = horizontal - init_horiz; // horizontal change in joystick
- cursor_x = cursor_x - delta_horiz/200; // maps to change of cursor
- cursor_y = cursor_y - delta_vert/200;
- // check if the current location is different than the previous
- if (cursor_x != old_cursor_x || cursor_y != old_cursor_y) {
- tft.fillRect (old_cursor_x, old_cursor_y, 2, 2, 0x0000)
- }
- // draw the cursor in current location
- tft.fillRect(cursor_x, cursor_y, 2, 2, 0x07E0);
- // if the cursor is at the screen edges, show game over screen
- if (cursor_x >= 128 || cursor_x <= 0) {
- game_over();
- }
- if (cursor_y >= 160 || cursor_y <= 0) {
- game_over();
- }
- // update the point's previous location
- old_cursor_x = cursor_x;
- old_cursor_y = cursor_y;
- delay(50); // to make sure the cursor is moving smoothly
- // need random x and y coordinates for the "apples" appear
- rand_apple_x = random(1,128);
- rand_apple_y = random(1,160);
- tft.fillRect (rand_apple_x, rand_apple_y, 2, 2, 0xF800);
- // add a trailing rect behind the cursor when it touches the apple
- // and make the apple disappear and make another one appear immediately
- // in a random place on the screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement