Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pm:prj2022:arosca:snakegamewithgyroscope [2022/05/27 17:38]
gabriel.saru [Cod Sursa]
pm:prj2022:arosca:snakegamewithgyroscope [2022/05/27 19:29] (current)
gabriel.saru [Download]
Line 64: Line 64:
 În codul meu am setat lungimea maximă a șarpelui la 64, care este întreaga matrice LED. Am făcut asta pentru că am vrut ca jocul să se termine doar dacă șarpele a murit și nu dacă a ajuns la o anumită lungime. În codul meu am setat lungimea maximă a șarpelui la 64, care este întreaga matrice LED. Am făcut asta pentru că am vrut ca jocul să se termine doar dacă șarpele a murit și nu dacă a ajuns la o anumită lungime.
  
 +Un mic rezumat in ceea ce fac functiile:
  
 +  *    void playGame() ​ --> această funcție va porni jocul
 +  *    void draw() ​ --> aceasta functie va apela funcțiile pentru a desena șarpele și ținta
 +  *    void drawSnake() --> aceasta functie va desena sarpele
 +  *    void drawTarget() --> aceasta functie va atrage ținta
 +  *    boolean inPlayField(int x, int y) --> va stabili terenul de joc pentru țintă și șarpe pentru a se spawna pe harta
 +  *    void makeTarget() --> aceasta functie va alege o valoare aleatorie și va stabili ținta
 +  *    boolean isPartOfSnake(int x, int y) --> ​ aceasta functie se va citi dacă valoarea face parte sau nu din șarpe.
 +  *    void makeSnake() --> aceasta functie va stabili valorile șarpelui
 +  *    void moveSnake() --> această funcție va muta șarpele în funcție de accelerometru
 +  *    void gameOver() --> aceasta functie va afișa spectacolul de lumini și va nota după terminarea jocului
  
 ==== Cod Sursa ==== ==== Cod Sursa ====
Line 70: Line 81:
 <​code> ​ <​code> ​
  
-//First we need to set up some libraries so that we can use all the parts we want +#include <SparkFun_MMA8452Q.h>
-#include <Wire.h> ​// we need this to use I2C which is a requirement of the acceleromter and LED matrix+
  
-#include <​SFE_MMA8452Q.h>​ // this will include the library so we can use the accelerometer+ 
 +//First we need to set up some libraries so that we can use all the parts we want 
 +#include <​Wire.h>​ // we need this to use I2C which is a requirement of the  
 +// acceleromter and LED matrix 
 +//#include <​SFE_MMA8452Q.h>​ // this will include the library so we can use the accelerometer
  
 //these two libraries are so we can use the LED matrix //these two libraries are so we can use the LED matrix
Line 79: Line 93:
 #include "​Adafruit_GFX.h"​ #include "​Adafruit_GFX.h"​
  
-// these three libraries are needed to connect the Arduino with the internet and Twitter(optional).+// these three libraries are needed to connect the Arduino with the internet and Twitter.
 #include <​SPI.h>​ // needed in Arduino 0019 or later #include <​SPI.h>​ // needed in Arduino 0019 or later
 #include <​Ethernet.h>​ #include <​Ethernet.h>​
-#include <​Twitter.h>​ 
- 
 #include <​EEPROM.h>​ // this will allow us to store memory on the Arduino. #include <​EEPROM.h>​ // this will allow us to store memory on the Arduino.
  
- +//Now we need to set up some features for our accelerometer 
-//Now we need to set up some features for our accelerometer ​H3LIS331DL ​accel;+MMA8452Q ​accel;
  
 //Now we need to set up some features for our LED matrix //Now we need to set up some features for our LED matrix
Line 105: Line 117:
 //You can get your token from (http://​arduino-tweet.appspot.com/​) //You can get your token from (http://​arduino-tweet.appspot.com/​)
 //this is my token, make sure to use your own for it to show up on your Twitter news feed //this is my token, make sure to use your own for it to show up on your Twitter news feed
-Twitter twitter("​3196797122-2jESNtWDAvddc1zYJkhaQqUjokUpVwraJ1pAbiS"​);​ 
  
-//This will set up a message which we will use later when we want to post to Twitter+//this will set up a message which we will use later when we want to post to Twitter
 char msg[70]; char msg[70];
  
Line 114: Line 125:
 const int sendButton = 6; const int sendButton = 6;
 int resetButton;​ int resetButton;​
- 
 const int maxSnake = 64; //this is the maximum length the snake can reach const int maxSnake = 64; //this is the maximum length the snake can reach
- 
 int snakeX[maxSnake];​ // this is the x coordinate of the snake int snakeX[maxSnake];​ // this is the x coordinate of the snake
 int snakeY[maxSnake];​ // this is the y coordinate of the snake int snakeY[maxSnake];​ // this is the y coordinate of the snake
- 
 int snakeLength = 1; // this is the starting length of the snake int snakeLength = 1; // this is the starting length of the snake
 int targetX; //this is the x coordinate of the item we are going after int targetX; //this is the x coordinate of the item we are going after
 int targetY; //the y coordinate of the item we are going after int targetY; //the y coordinate of the item we are going after
- 
 unsigned long targetPrevTime = 0; //these will be used to help with the timing of the target unsigned long targetPrevTime = 0; //these will be used to help with the timing of the target
 unsigned long targetBlinkTime = 1000 / 250; unsigned long targetBlinkTime = 1000 / 250;
- 
 int targetLED = LED_GREEN; int targetLED = LED_GREEN;
- 
 unsigned long prevTime = 0; unsigned long prevTime = 0;
 unsigned long delayTime = 500; unsigned long delayTime = 500;
Line 144: Line 149:
  
 void setup() { void setup() {
- 
   //Set up our pins as inputs   //Set up our pins as inputs
   pinMode(powerButton,​ INPUT);   pinMode(powerButton,​ INPUT);
Line 163: Line 167:
  
 void loop() { void loop() {
- 
   // this will clear the display just in case something else was on it before   // this will clear the display just in case something else was on it before
   matrix.clear();​   matrix.clear();​
Line 170: Line 173:
   static int prevPowerState = 0; //this will ensure we read only the button push, and not how long it is pushed.   static int prevPowerState = 0; //this will ensure we read only the button push, and not how long it is pushed.
   int powerButtonState = digitalRead(powerButton);​ //this will moniter the state of the button   int powerButtonState = digitalRead(powerButton);​ //this will moniter the state of the button
-  ​ 
   if ((powerButtonState == HIGH) && (prevPowerState == LOW)) {   if ((powerButtonState == HIGH) && (prevPowerState == LOW)) {
     mode = (mode + 1) % NUMMODES;     mode = (mode + 1) % NUMMODES;
   }   }
   prevPowerState = powerButtonState;​   prevPowerState = powerButtonState;​
- 
-  //​Serial.println(mode);​ //debug to make sure the modes were working porperly 
  
   switch (mode) {   switch (mode) {
-  ​ 
     case 0: //if the button has not been pushed nothing happens     case 0: //if the button has not been pushed nothing happens
       //​Serial.println("​OFF"​);​ debug to make sure switch works properly       //​Serial.println("​OFF"​);​ debug to make sure switch works properly
Line 194: Line 193:
   int sendButtonState = digitalRead(sendButton);​ //this will send a Tweet of the score by pushing the red button   int sendButtonState = digitalRead(sendButton);​ //this will send a Tweet of the score by pushing the red button
   if (sendButtonState == HIGH) {   if (sendButtonState == HIGH) {
-    sendTweet();​ 
   }   }
 } }
Line 209: Line 207:
  
 void nextstep() { void nextstep() {
- 
   for (int i = snakeLength - 1; i > 0; i--) {   for (int i = snakeLength - 1; i > 0; i--) {
     snakeX[i] = snakeX[i - 1];     snakeX[i] = snakeX[i - 1];
Line 215: Line 212:
   }   }
  
-  for (int8_t h = 3; h <= snakeLength;​ h++) {  +  for (int8_t h = 3; h <= snakeLength;​ h++) { // this will monitor if the snake'​s head is colliding with another part
-    ​// this will monitor if the snake'​s head is colliding with another part+
     // of the snake. we use 3 because it is basically impossible for the snake to hit itself before then.     // of the snake. we use 3 because it is basically impossible for the snake to hit itself before then.
-    ​ 
     if ((snakeX[0] == snakeX[h]) && (snakeY[0] == snakeY[h]))     if ((snakeX[0] == snakeX[h]) && (snakeY[0] == snakeY[h]))
     {     {
Line 224: Line 219:
     }     }
   }   }
-  ​ 
   Serial.print("​Collision:"​);​ //used for debugging to ensure the collision detection worked   Serial.print("​Collision:"​);​ //used for debugging to ensure the collision detection worked
   Serial.println(collision);​   Serial.println(collision);​
  
   if ((inPlayField(snakeX[0],​ snakeY[0])) && collision == 0) { // if the snake is in the playing field   if ((inPlayField(snakeX[0],​ snakeY[0])) && collision == 0) { // if the snake is in the playing field
-  ​ 
     // or not colliding with itself then we will be able to continue to play     // or not colliding with itself then we will be able to continue to play
     moveSnake();​ //this will call a function to move the snake     moveSnake();​ //this will call a function to move the snake
  
     if ((snakeX[0] == targetX) && (snakeY[0] == targetY)) { //this will read if the snake is on the target     if ((snakeX[0] == targetX) && (snakeY[0] == targetY)) { //this will read if the snake is on the target
-      
       snakeLength++;​ //if it is it will gain a length of one       snakeLength++;​ //if it is it will gain a length of one
       score++; // and the score will increase by one       score++; // and the score will increase by one
-      ​ 
       Serial.print("​Score:​ "); // the score will then be written on the serial monitor       Serial.print("​Score:​ "); // the score will then be written on the serial monitor
       Serial.println(score);​       Serial.println(score);​
Line 286: Line 277:
   int x;   int x;
   int y;   int y;
-  ​ 
   x = random(0, 8);   x = random(0, 8);
   y = random(0, 8);   y = random(0, 8);
-  ​ 
   while (isPartOfSnake(x,​ y)) { //here it will check if the target is part of the snake   while (isPartOfSnake(x,​ y)) { //here it will check if the target is part of the snake
     x = random(0, 8); // if it is it will choose a new random point and thus continue the game     x = random(0, 8); // if it is it will choose a new random point and thus continue the game
Line 309: Line 298:
  
 void makeSnake() { //this will set up the values of the snake void makeSnake() { //this will set up the values of the snake
 +  ​
   snakeX[0] = 3;   snakeX[0] = 3;
   snakeY[0] = 4;   snakeY[0] = 4;
 +  ​
   for (int i = 1; i < maxSnake; i++) {   for (int i = 1; i < maxSnake; i++) {
     snakeX[i] = snakeY[i] = -1;     snakeX[i] = snakeY[i] = -1;
Line 318: Line 309:
 void moveSnake() { // this function will move the snake according to the accelerometer void moveSnake() { // this function will move the snake according to the accelerometer
   if (accel.available()) {   if (accel.available()) {
-  ​ 
     accel.read();​ //this will read the values of the accelerometer     accel.read();​ //this will read the values of the accelerometer
     Serial.print("​x:​ ");     Serial.print("​x:​ ");
Line 457: Line 447:
   matrix.setTextColor(LED_YELLOW);​   matrix.setTextColor(LED_YELLOW);​
   matrix.setRotation(3);​   matrix.setRotation(3);​
 +  ​
   for (int8_t x = 7; x >= -36; x--) {   for (int8_t x = 7; x >= -36; x--) {
     matrix.clear();​     matrix.clear();​
Line 465: Line 456:
   }   }
 } }
 +
  
 </​code>​ </​code>​
Line 480: Line 472:
 ===== Download ===== ===== Download =====
  
-* {{ :​pm:​prj2022:​arosca:​codsursasnakeaccelerometru.zip | Arhiva cod sursa + comentarii }}+* {{ :​pm:​prj2022:​arosca:​arhivaplusreadme_sarugabriel333cb.zip | Arhiva cod sursa + comentarii ​+ README ​}} 
 + 
 +In aceasta sectiune de DOWNLOAD putem gasi arhiva care contine urmatoarele:​ 
 + 
 +  * Codul sursa al proiectului;​ 
 +  * README, pentru o explicare mai succinta a functiilor din proiect; 
 +  * Libraria accelerometrului;​
  
  
pm/prj2022/arosca/snakegamewithgyroscope.1653662291.txt.gz · Last modified: 2022/05/27 17:38 by gabriel.saru
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0