arduino uno+LCD12864(ST7735S)+藍芽模組實現貪吃蛇

moonKing發表於2024-05-05

1.前言:
1.1本實驗實現的貪吃蛇能穿越邊界,結束遊戲的唯一條件是貪吃蛇到達指定長度
1.2本實驗所用LCD可能不是LCD12864,LCD12864所用庫為u8glib,筆者在詞庫中並沒有找到型號為ST77355的初始化函式,而是在ucglib中找到,其方法為
Ucglib_ST7735_18x128x160_SWSPI ucg(/*sclk=*/13, /*data=*/11, /*cd=*/9, /*cs=*/10, /*reset=*/8);
1.3程式碼由學長LCD1602修改而來
2.接線方式

藍芽模組 RXD TXD GND VCC
Arduino uno pin7 pin6 5v GND
ST7735S模組 BLK CS DC RST SDA SCL VDD VCC
Arduino uno 3.3v pin10 pin9 pin8 pin11 pin13 未接 GND

3.話不多說上程式碼

點選檢視程式碼
#include <SoftwareSerial.h>
#include <SPI.h>
#include "Ucglib.h"
#define maxlength 4 //蛇最大長度
Ucglib_ST7735_18x128x160_SWSPI ucg(/*sclk=*/13, /*data=*/11, /*cd=*/9, /*cs=*/10, /*reset=*/8);
SoftwareSerial BT(6, 7);
char val;//藍芽接收字元
int snake_length = 1;//蛇目前長度
int snake_x[maxlength], snake_y[maxlength];
int direction = 6;//方向判斷和閃屏控制
int x = 10;
int y = 10;//記錄變化的位置
int food_x;
int food_y;//食物
int gameOver = 0;//結束條件
void setup(void) {
  //delay(1000);
  ucg.begin(UCG_FONT_MODE_TRANSPARENT);
  //ucg.begin(UCG_FONT_MODE_SOLID);
  ucg.clearScreen();
  BT.begin(9600);
  snake_x[0] = 10;
  snake_y[0] = 10;
  food_x = 10 + 10 * random(0, 9);
  food_y = 10 + 10 * random(0, 9);
}

void loop(void) {
  if (direction == 6) {//歡迎介面
    ucg.setFont(ucg_font_ncenR12_tr);//字型設定
    ucg.setPrintPos(40, 80);//設定顯示座標
    ucg.print("welcome");
  } else
    ucg.drawFrame(10, 10, 100, 100);//遊戲區域,有沒有無所謂
  if (BT.available()) {
    val = BT.read();
    if (val == 'w' && direction != 2) direction = 0;
    if (val == 's' && direction != 0) direction = 2;
    if (val == 'a' && direction != 1) direction = 3;
    if (val == 'd' && direction != 3) direction = 1;
  }
  if (direction == 0) y -= 10;  //w
  if (direction == 1) x += 10;  //d
  if (direction == 2) y += 10;  //s
  if (direction == 3) x -= 10;  //a

  if (x < 10) x = 100;
  if (x > 100) x = 10;
  if (y < 10) y = 100;
  if (y > 100) y = 10;//到達邊界後穿過邊界

  if (x == food_x && y == food_y) {//行動後吃到食物
    snake_length++;
    while (1) {
      int flag = 1;//跳出迴圈標誌
      food_x = 10 + 10 * random(0, 9);
      food_y = 10 + 10 * random(0, 9);//重新生成一個食物

      for (int i = snake_length - 1; i >= 0; i--) {//遍歷蛇身看食物是否生成在身上
        if ((food_x == snake_x[i]) && (food_y == snake_y[i]) || ((food_x == x) && (food_y == y))) {
          flag = 0;
          break;
        }
      }
      if (snake_length == maxlength) {//遊戲結束條件
        gameOver = 1;
        direction=5;//結束後顯示gameover時的閃屏控制
        break;
      }
      if (flag) break;
    }
  }

  if (direction != 6) {
    for (int i = snake_length - 1; i > 0; i--) {
      snake_x[i] = snake_x[i - 1];
      snake_y[i] = snake_y[i - 1];
    }
    snake_x[0] = x;
    snake_y[0] = y;
    if (gameOver) {
      if(direction==5){
        ucg.clearScreen();
        direction=1;
      }
      else{
        ucg.setFont(ucg_font_ncenR12_tr);
        ucg.setPrintPos(40, 80);
        ucg.print("gameover");
      }
      
    }
    if (!gameOver) {
      ucg.clearScreen();
      for (int i = 0; i < snake_length; i++) {
        ucg.drawFrame(snake_x[i], snake_y[i], 10, 10);
      }
      ucg.drawFrame(food_x, food_y, 10, 10);
    }
  }
}

4.來人上圖

再上

還上

使用手機app藍芽串列埠除錯,傳送wasd,對應上左下右

5.回顧(複習看)
標頭檔案:<SoftwareSerial.h>、 <Ucglib.h> 、<stdio.h>

5.1LCD相關函式
LCD初始化
Ucglib_ST7735_18x128x160_SWSPI ucg(/*sclk=*/13, /*data=*/11, /*cd=*/9, /*cs=*/10, /*reset=*/8);
顯示矩形,其左上角座標為(snake_x[i], snake_y[i]),長度和寬度為10
ucg.drawFrame(snake_x[i], snake_y[i], 10, 10);
顯示畫素點,其座標座標為(x,y)
ucg.drawPixel(x,y)
清屏
ucg.clearScreen();
初始化,兩者區別不知道
ucg.begin(UCG_FONT_MODE_TRANSPARENT); //ucg.begin(UCG_FONT_MODE_SOLID);
字型、顯示座標、顯示函式
ucg.setFont(ucg_font_ncenR12_tr); ucg.setPrintPos(40, 80); ucg.print("welcome");

5.2藍芽相關函式
SoftwareSerial BT(6, 7);
讀app發來的資訊
if (BT.available()) char val = BT.read();

6.說明
如有同道中人,請刪去註釋

相關文章