Repository navigation
Expand file tree
/
Copy pathSnakeGameArduino.ino
More file actions
123 lines (102 loc) · 2.57 KB
/
Copy pathSnakeGameArduino.ino
File metadata and controls
123 lines (102 loc) · 2.57 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int VRX = A0;
const int VRY = A1;
const int SW = 2;
int snakeX[100], snakeY[100];
int snakeLength = 5;
int foodX, foodY;
int direction = 1; // 0=UP, 1=RIGHT, 2=DOWN, 3=LEFT
bool gameOver = false;
void setup() {
pinMode(SW, INPUT_PULLUP);
randomSeed(analogRead(0));
display.begin(SSD1306_I2C_ADDRESS, 0x3C);
display.display();
delay(2000);
spawnFood();
for (int i = 0; i < snakeLength; i++) {
snakeX[i] = 10 - i;
snakeY[i] = 5;
}
}
void loop() {
if (!gameOver) {
handleJoystick();
moveSnake();
checkCollision();
drawGame();
delay(150);
} else {
displayGameOver();
delay(3000);
resetGame();
}
}
void handleJoystick() {
int xValue = analogRead(VRX);
int yValue = analogRead(VRY);
if (yValue < 300 && direction != 2) direction = 0; // UP
if (yValue > 700 && direction != 0) direction = 2; // DOWN
if (xValue > 700 && direction != 3) direction = 1; // RIGHT
if (xValue < 300 && direction != 1) direction = 3; // LEFT
}
void moveSnake() {
for (int i = snakeLength - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
if (direction == 0) snakeY[0]--;
else if (direction == 1) snakeX[0]++;
else if (direction == 2) snakeY[0]++;
else if (direction == 3) snakeX[0]--;
if (snakeX[0] == foodX && snakeY[0] == foodY) {
snakeLength++;
spawnFood();
}
}
void checkCollision() {
if (snakeX[0] < 0 || snakeX[0] >= SCREEN_WIDTH / 4 || snakeY[0] < 0 || snakeY[0] >= SCREEN_HEIGHT / 4) {
gameOver = true;
}
for (int i = 1; i < snakeLength; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
gameOver = true;
}
}
}
void drawGame() {
display.clearDisplay();
// Draw snake
for (int i = 0; i < snakeLength; i++) {
display.fillRect(snakeX[i] * 4, snakeY[i] * 4, 4, 4, SSD1306_WHITE);
}
// Draw food
display.fillRect(foodX * 4, foodY * 4, 4, 4, SSD1306_WHITE);
display.display();
}
void spawnFood() {
foodX = random(0, SCREEN_WIDTH / 4);
foodY = random(0, SCREEN_HEIGHT / 4);
}
void displayGameOver() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(20, 20);
display.println("GAME OVER");
display.display();
}
void resetGame() {
snakeLength = 5;
for (int i = 0; i < snakeLength; i++) {
snakeX[i] = 10 - i;
snakeY[i] = 5;
}
gameOver = false;
spawnFood();
}