Advertisement
Guest User

SnakeMatrix1

a guest
Jul 27th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. int snakeLength = 3;
  2. SnakeValues[snakelength*2];
  3. // buttons to change direction of snake
  4. int btnx1 = 2;
  5. int btnx0 = 3;
  6. int btny1 = 4;
  7. int btny0 = 5;
  8. xl = 8;
  9. yl = 8;
  10.  
  11. // These values are used to check which direction we are moving in.
  12. int x1 = 1;
  13. int x0 = 0;
  14. int y1 = 1;
  15. int y0 = 0;
  16.  
  17.  
  18.  
  19.  
  20. void movement(){
  21.     // Arrayet skubber alle værdier en plads ned
  22.     for (int i = snakeLength*2;i>0;i--){
  23.         snakeValues[i] = snakeValues[i+2];
  24.         snakeValues[i+1] = snakeValues[i+3];
  25.  
  26.     }
  27.     snakeValues[0] = topPosX;
  28.     snakeValues[1] = topPosY;
  29.  
  30. }
  31.  
  32. void printscr(){
  33.     for (int i = snakeLength*2;i > 0;i-2){
  34.         println(i);
  35.         println(i+1);
  36.     }
  37. }
  38.  
  39. // changeDir checks which button is pressed down and makes the topPos equal to the direction of the button
  40. void changeDir(){
  41.     // reset the directions
  42.     x1 = 0;
  43.     x0 = 0;
  44.     y1 = 0;
  45.     y0 = 0;
  46.     //check which button is pressed down
  47.  
  48.     if(digitalRead(btnx1)==!HIGH){
  49.     topPosX = topPosX + 1;
  50.     x1 = 1;
  51.     }
  52.     if(digitalRead(btnx0)==!HIGH){
  53.     topPosX = topPosX - 1;
  54.     x0 = 1;
  55.     }
  56.     if(digitalRead(btny1)==!HIGH){
  57.     topPosY = topPosY + 1;
  58.     y1 = 1;
  59.     }
  60.     if(digitalRead(btny0)==!HIGH){
  61.     topPosY = topPosY - 1;
  62.     y0 = 1;
  63.     }
  64. }
  65.  
  66. void noPress(){
  67.     switch(){
  68.     case x1 == 1:
  69.         topPosX = topPosX + 1;
  70.         break;
  71.     case x0 == 1:
  72.         topPosX = topPosX - 1;
  73.         break;
  74.     case y1 == 1:
  75.         topPosY = topPosY + 1;
  76.         break;
  77.     case y0 == 1:
  78.         topPosY = topPosY - 1;
  79.         break;
  80.     }
  81. }
  82. void loop(){
  83.     printscr();
  84.     if(digitalRead(btnx1)==!HIGH || digitalRead(btnx0)==!HIGH ||digitalRead(btny1)==!HIGH||digitalRead(btny0)==!HIGH){
  85.     changeDir();
  86.     }
  87.     else{
  88.     // noPress makes sure to keep the snake moving, even when no button is pressed.
  89.     noPress();
  90.     }
  91.     movement();
  92.     delay(500);
  93. }
  94.  
  95.  
  96.  
  97.  
  98. Alle gamle værdier er i arrayet, vi skal derfor fjerne den sidste gamle værdi, rykke alle andre værdier en ned i arrayet og derefter smide den nye værdi på den øverste plads
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement