Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int snakeLength = 3;
- SnakeValues[snakelength*2];
- // buttons to change direction of snake
- int btnx1 = 2;
- int btnx0 = 3;
- int btny1 = 4;
- int btny0 = 5;
- xl = 8;
- yl = 8;
- // These values are used to check which direction we are moving in.
- int x1 = 1;
- int x0 = 0;
- int y1 = 1;
- int y0 = 0;
- void movement(){
- // Arrayet skubber alle værdier en plads ned
- for (int i = snakeLength*2;i>0;i--){
- snakeValues[i] = snakeValues[i+2];
- snakeValues[i+1] = snakeValues[i+3];
- }
- snakeValues[0] = topPosX;
- snakeValues[1] = topPosY;
- }
- void printscr(){
- for (int i = snakeLength*2;i > 0;i-2){
- println(i);
- println(i+1);
- }
- }
- // changeDir checks which button is pressed down and makes the topPos equal to the direction of the button
- void changeDir(){
- // reset the directions
- x1 = 0;
- x0 = 0;
- y1 = 0;
- y0 = 0;
- //check which button is pressed down
- if(digitalRead(btnx1)==!HIGH){
- topPosX = topPosX + 1;
- x1 = 1;
- }
- if(digitalRead(btnx0)==!HIGH){
- topPosX = topPosX - 1;
- x0 = 1;
- }
- if(digitalRead(btny1)==!HIGH){
- topPosY = topPosY + 1;
- y1 = 1;
- }
- if(digitalRead(btny0)==!HIGH){
- topPosY = topPosY - 1;
- y0 = 1;
- }
- }
- void noPress(){
- switch(){
- case x1 == 1:
- topPosX = topPosX + 1;
- break;
- case x0 == 1:
- topPosX = topPosX - 1;
- break;
- case y1 == 1:
- topPosY = topPosY + 1;
- break;
- case y0 == 1:
- topPosY = topPosY - 1;
- break;
- }
- }
- void loop(){
- printscr();
- if(digitalRead(btnx1)==!HIGH || digitalRead(btnx0)==!HIGH ||digitalRead(btny1)==!HIGH||digitalRead(btny0)==!HIGH){
- changeDir();
- }
- else{
- // noPress makes sure to keep the snake moving, even when no button is pressed.
- noPress();
- }
- movement();
- delay(500);
- }
- 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