Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Snake.ino
- Created: 12/29/2016 5:15:15 AM
- Author: beefviper
- */
- // Include U8g2 library for JHD529M1-12864F (ST7920) 128X64 monochrome LCD display
- #include <U8g2lib.h>
- // u8g2 LCD configuration line: _1 (single page) _6800 (comm mode) _R2 (180rotate), 8 data lines, 3 control lines
- U8G2_ST7920_128X64_1_6800 u8g2(U8G2_R2, 8, 9, 10, 11, 4, 5, 6, 7, /*enable=*/ 18 /* A4 */, /*cs=*/ U8X8_PIN_NONE, /*dc/rs=*/ 17 /* A3 */, /*reset=*/ 15 /* A1 */); // Remember to set R/W to 0
- // Pin Assignments
- const int xAxisPin = 16;
- const int yAxisPin = 19;
- const int buttonPin = 2;
- const int speakerPin = 3;
- const int floatingPin = 14;
- // Hardware Constants
- const int screenWidth = 128;
- const int screenHeight = 64;
- // Joystick Variables
- int joyX = 0;
- int joyY = 0;
- int joyB = 0;
- // Game Variables
- const int frameSkip = 1;
- int frameLimit = frameSkip;
- const int maxLength = 250;
- const int screenScale = 4;
- const int screenColumns = screenWidth / screenScale;
- const int screenRows = screenHeight / screenScale;
- bool buttonState = false;
- bool gameOver = true;
- bool firstGame = true;
- int score = 0;
- class D2Point {
- public:
- int x;
- int y;
- };
- class Snake {
- public:
- Snake();
- ~Snake();
- int velocityX;
- int velocityY;
- int length;
- D2Point position[maxLength];
- void reset();
- void grow();
- void move();
- void draw();
- void left() { velocityX = -1; velocityY = 0; };
- void right() { velocityX = 1; velocityY = 0; };
- void up() { velocityX = 0; velocityY = -1; };
- void down() { velocityX = 0; velocityY = 1; };
- private:
- };
- class Food {
- public:
- Food();
- ~Food();
- D2Point position;
- void place();
- void draw();
- };
- class Game {
- public:
- void draw();
- void draw(Snake &s, Food &f);
- };
- void Game::draw() {
- u8g2.firstPage();
- do {
- u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font
- if (!firstGame) {
- u8g2.drawStr(40, 14, "Game Over");
- u8g2.drawStr(40, 54, "Score:");
- u8g2.setCursor(75, 54);
- u8g2.print(score);
- }
- u8g2.drawStr(50, 24, "Snake");
- u8g2.drawStr(34, 34, "Press Button");
- } while (u8g2.nextPage());
- }
- void Game::draw(Snake &s, Food &f) {
- u8g2.firstPage();
- do {
- /* all graphics commands have to appear within the loop body. */
- u8g2.setFont(u8g2_font_5x7_tf); // choose a suitable font
- s.draw();
- f.draw();
- } while (u8g2.nextPage());
- }
- Snake::Snake() {
- velocityX = 0;
- velocityY = -1;
- length = 0;
- position[0].x = screenColumns / 2 * screenScale;
- position[0].y = screenRows / 2 * screenScale;
- }
- Snake::~Snake() {
- }
- void Snake::reset() {
- length = 0;
- position[0].x = screenColumns / 2 * screenScale;
- position[0].y = screenRows / 2 * screenScale;
- velocityX = 0;
- velocityY = -1;
- }
- void Snake::grow() {
- if ( length < (maxLength - 2 ) ) {
- length++;
- }
- }
- void Snake::move() {
- for (int i = length; i >= 0; i--) {
- position[i + 1].x = position[i].x;
- position[i + 1].y = position[i].y;
- }
- position[0].x = position[0].x + (velocityX * screenScale);
- position[0].y = position[0].y + (velocityY * screenScale);
- }
- void Snake::draw() {
- for (int i = length; i >= 0; i--) {
- u8g2.drawBox(position[i].x, position[i].y, screenScale, screenScale);
- }
- }
- Food::Food() {
- }
- Food::~Food() {
- }
- void Food::place() {
- position.x = random(screenColumns) * screenScale;
- position.y = random(screenRows) * screenScale;
- }
- void Food::draw() {
- u8g2.drawBox(position.x, position.y, screenScale, screenScale);
- }
- Game game;
- Snake snake;
- Food food;
- // The setup function runs once when you press reset or power the board
- void setup() {
- u8g2.begin();
- pinMode(xAxisPin, INPUT);
- pinMode(yAxisPin, INPUT);
- pinMode(buttonPin, INPUT_PULLUP);
- pinMode(speakerPin, OUTPUT);
- randomSeed(analogRead(floatingPin));
- food.place();
- Serial.begin(115200);
- }
- // The loop function runs over and over again until power down or reset
- void loop() {
- // gameOver separates the game into two states; running and attract
- if (!gameOver) {
- // Running state
- frameLimit--;
- if (frameLimit < 1) {
- frameLimit = frameSkip;
- joyX = analogRead(xAxisPin);
- joyY = analogRead(yAxisPin);
- joyB = digitalRead(buttonPin);
- // Test buttonState. If joyB is HIGH the button is not pressed
- if (joyB) {
- buttonState = false;
- }
- else {
- buttonState = true;
- }
- if ((joyX < 255) && (snake.velocityX == 0)) {
- snake.left();
- }
- else if ((joyX > 767) && (snake.velocityX == 0)) {
- snake.right();
- }
- else if ((joyY < 255) && (snake.velocityY == 0)) {
- snake.up();
- }
- else if ((joyY > 767) && (snake.velocityY == 0)) {
- snake.down();
- }
- if ( snake.position[0].x < 0 || snake.position[0].x > 127 || snake.position[0].y < 0 || snake.position[0].y > 63 ) {
- gameOver = true;
- beep(250);
- }
- for (int i = 1; i <= snake.length; i++) {
- if ((snake.position[0].x == snake.position[i].x) && (snake.position[0].y == snake.position[i].y)) {
- gameOver = true;
- beep(250);
- }
- }
- if ((snake.position[0].x == food.position.x) && (snake.position[0].y == food.position.y)) {
- snake.grow();
- food.place();
- score++;
- beep(50);
- }
- if (!gameOver) {
- snake.move();
- }
- // Print out joystick information to serial out
- print_status(joyX, joyY, joyB);
- }
- game.draw(snake, food);
- }
- else {
- // Attract state
- game.draw();
- joyB = digitalRead(buttonPin);
- // Test buttonState. If joyB is HIGH the button is not pressed
- if (joyB) {
- buttonState = false;
- }
- else {
- buttonState = true;
- }
- if (buttonState) {
- gameOver = false;
- firstGame = false;
- snake.reset();
- score = 0;
- beep(100);
- }
- }
- // Transfer internal memory to the display
- //u8g2.sendBuffer();
- // General Delay
- //delay(100);
- }
- void beep(int timeout) {
- digitalWrite(speakerPin, HIGH);
- delay(timeout);
- digitalWrite(speakerPin, LOW);
- }
- void print_status(int x, int y, int b) {
- //Serial.print("label");
- //Serial.print(variable);
- //Serial.print(", ");
- //Serial.print("label");
- //Serial.print(variable);
- //Serial.print(", ");
- Serial.print("X: ");
- Serial.print(x);
- Serial.print(", ");
- Serial.print("Y: ");
- Serial.print(y);
- Serial.print(", ");
- Serial.print("B: ");
- Serial.println(b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement