#include <LedControl.h>
#include <qrcode.h>
int size=4;
int side=size * 8;
#include <Servo.h>
Servo myservo;
int pos = 0;
int yao=0;
const int buttonPin = 13;
int buttonState = 0; // 按钮的当前状态
LedControl ledControls[] = {
LedControl(0, 1, 2, size),
LedControl(3, 4, 5, size),
LedControl(6, 7, 8, size),
LedControl(12, 10, 11, size)
};
// Define the snake game variables
struct Point {
int x;
int y;
};
Point snake[64]; // Maximum length of the snake
int snakeLength = 3;
Point food;
int direction = 0; // 0=right, 1=down, 2=left, 3=up
// Joystick pins
int joystickX = A0;
int joystickY = A1;
void generateFood() {
food.x = random(side);
food.y = random(side);
}
void setup() {
// Initialize each device
pinMode(buttonPin, INPUT); // 设置按钮引脚为输入
myservo.attach(9);
for (int i = 0; i < size; i++) {
ledControls[i].shutdown(0, false); // Wake up displays
ledControls[i].setIntensity(0, 8); // Set brightness level (0 is min, 15 is max)
ledControls[i].clearDisplay(0); // Clear display register
}
// Initialize the snake's starting position
snake[0] = {side / 2, side / 2};
generateFood();
randomSeed(analogRead(0)); // Seed the random number generator
// Initialize joystick pins
pinMode(joystickX, INPUT);
pinMode(joystickY, INPUT);
}
void drawPixel(int x, int y, bool state) {
int index = y / 8;
int address = size - 1 - (x / 8);
int row = y % 8;
int column = 7 - (x % 8);
ledControls[index].setLed(address, row, column, state);
}
void drawSnake() {
for (int i = 0; i < snakeLength; i++) {
drawPixel(snake[i].x, snake[i].y, true);
}
}
void clearSnake() {
for (int i = 0; i < snakeLength; i++) {
drawPixel(snake[i].x, snake[i].y, false);
}
}
void drawFood() {
drawPixel(food.x, food.y, true);
}
void moveSnake() {
// Shift the snake's body
for (int i = snakeLength - 1; i > 0; i--) {
snake[i] = snake[i - 1];
}
// Move the head based on direction
if (direction == 0) snake[0].x--;
else if (direction == 1) snake[0].y--;
else if (direction == 2) snake[0].x++;
else if (direction == 3) snake[0].y++;
// Wrap around the screen edges
if (snake[0].x >= side || snake[0].x < 0 || snake[0].y >= side || snake[0].y < 0) {
restarGame();
}
}
bool checkCollision() {
for (int i = 1; i < snakeLength; i++) {
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
return true;
}
}
return false;
}
void loop() {
clearSnake();
// Read joystick input
int xVal = analogRead(joystickX);
int yVal = analogRead(joystickY);
// Determine direction based on joystick input
if (xVal < 100) direction = 2; // Left
else if (xVal > 900) direction = 0; // Right
else if (yVal < 100) direction = 3; // Up
else if (yVal > 900) direction = 1; // Down
// Update the snake's position
moveSnake();
// Check for food collision
if (snake[0].x == food.x && snake[0].y == food.y) {
snakeLength++;
generateFood();
}
// Check for self-collision
if (checkCollision()) {
// Reset the game
snakeLength = 3;
snake[0] = {side / 2, side / 2};
}
drawSnake();
drawFood();
if(yao==0&&snakeLength==10)
{xuanzhuan();
yao++;}
buttonState = digitalRead(buttonPin);
delay(200);
if (buttonState == HIGH) { // 按钮按下时状态为 HIGH
clearSnake();
restarGame(); }
}
void xuanzhuan() {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
void restarGame()
{
yao=0;
snakeLength = 3;
snake[0] = {side / 2, side / 2};
}