This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSnake.cpp
94 lines (87 loc) · 2.38 KB
/
Snake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "Snake.h"
void Snake::run() {
if (gameRunning) {
if ((millis()-lastSnakeMoveTime)>((80-snakeLength)*(80-snakeLength)/20)) {
if (!moveSnake()) gameOver();
else lastSnakeMoveTime=millis();
}
}
}
void Snake::init() {
for (uint8_t y=0;y<16;y++) {
for (uint8_t x=0;x<8;x++) {
snakeBoard[y][x]=0;
}
}
snakeLength=3;
snakeHeadX=random(4)+2;
snakeHeadY=random(12)+2;
uint8_t moveDirection=random(4);
switch (moveDirection) {
case 0: // up
snakeHeadDX=0; snakeHeadDY=-1; break;
case 1: // down
snakeHeadDX=0; snakeHeadDY=1; break;
case 2: // left
snakeHeadDX=-1; snakeHeadDY=0; break;
case 3: // right
snakeHeadDX=1; snakeHeadDY=0;
}
// Draw the snake of length=3
snakeBoard[snakeHeadY][snakeHeadX]=3;
snakeBoard[snakeHeadY-snakeHeadDY][snakeHeadX-snakeHeadDX]=2;
snakeBoard[snakeHeadY-snakeHeadDY*2][snakeHeadX-snakeHeadDX*2]=1;
placeFood();
gameRunning=true;
}
void Snake::gameOver() {
gameRunning=false;
Serial.println("Game Over");
}
void Snake::placeFood() {
foodX = random(8);
foodY = random(16);
while(snakeBoard[foodY][foodX]!=0) {
foodX = random(8);
foodY = random(16);
}
snakeBoard[foodY][foodX]=snakeLength+1;
}
bool Snake::moveSnake() {
snakeHeadX = (snakeHeadX+snakeHeadDX+8) % 8;
snakeHeadY = (snakeHeadY+snakeHeadDY+16)%16;
uint8_t temp = snakeBoard[snakeHeadY][snakeHeadX];
// if the new snakeHead is already occupied by part of the snake body, then return false, game over
if (temp>1 && temp<=snakeLength) return false;
if (snakeHeadX == foodX && snakeHeadY == foodY) {
snakeLength+=1;
placeFood();
}
else {
for (uint8_t y=0; y<16; y++) {
for (uint8_t x=0; x<8; x++) {
if (snakeBoard[y][x]>0 && snakeBoard[y][x]<=snakeLength) snakeBoard[y][x]-=1;
}
}
snakeBoard[snakeHeadY][snakeHeadX]=snakeLength;
}
allowToChangeDirection=true;
return true;
}
void Snake::changeDirection(int8_t dx, int8_t dy) {
if (!allowToChangeDirection) return;
if ((snakeHeadDX+dx==0 && dx!=0) || (snakeHeadDY+dy==0 && dy!=0)) return;
snakeHeadDX=dx;
snakeHeadDY=dy;
allowToChangeDirection=false;
}
uint8_t* Snake::getActiveBoard(){
for (int8_t y=0; y<16; y++) {
uint8_t temp=0;
for (int8_t x=0; x<8; x++) {
if (snakeBoard[y][x]) temp |= _BV(7-x);
}
activeSnakeBoard[y]=temp;
}
return activeSnakeBoard;
}