Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Mengqi Sun
Published

Fruit Harvest Game Using Arduino IDE and Wio Terminal

Train multitasking playfully with Fruit Harvest—an Arduino game blending work tasks & fruit collection!

BeginnerProtip10 hours41
Fruit Harvest Game Using Arduino IDE and Wio Terminal

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Fruit Harvest Game Using Arduino IDE and Wio Terminal

Arduino
<TFT_eSPI.h>
#include <Seeed_FS.h>
#include "SD/Seeed_SD.h"
#include "TFT_eSPI.h"

TFT_eSPI tft;

// 游戏变量
int basketX = 100;
int fruitX, fruitY;
int fruitSpeed = 3;
int score = 0;
int lives = 3;
int gameLevel = 1;
bool gameRunning = false;
int fruitType;  // 0=苹果, 1=香蕉, 2=草莓

// 颜色定义
#define FRUIT_RED 0xFB00
#define FRUIT_YELLOW 0xFFE0
#define FRUIT_PINK 0xFDDF

void setup() {
  Serial.begin(115200);
  tft.begin();
  tft.setRotation(3);
  
  // 初始化摇杆和按钮
  pinMode(WIO_5S_LEFT, INPUT);
  pinMode(WIO_5S_RIGHT, INPUT);
  pinMode(WIO_KEY_A, INPUT);

  
  // 显示开始界面
  showStartScreen();
}

void loop() {
  if (!gameRunning) {
    if (digitalRead(WIO_KEY_A) == LOW) {
      startGame();
    }
    return;
  }
  
  // 游戏主循环
  tft.fillScreen(TFT_BLACK);
  
  // 1. 绘制游戏信息
  drawGameInfo();
  
  // 2. 绘制篮子
  drawBasket();
  
  // 3. 绘制水果
  drawFruit();
  
  // 4. 水果掉落逻辑
  fruitY += fruitSpeed;
  if (fruitY > 240) {
    missFruit();
    createNewFruit();
  }
  
  // 5. 碰撞检测
  if (fruitY >= 200 && abs(fruitX - basketX) < 30) {
    catchFruit();
    createNewFruit();
  }
  
  // 6. 控制篮子移动
  if (digitalRead(WIO_5S_LEFT) == LOW && basketX > 0) {
    basketX -= 6;
  }
  if (digitalRead(WIO_5S_RIGHT) == LOW && basketX < 200) {
    basketX += 6;
  }
  
  // 7. 游戏难度调整
  if (score > gameLevel * 5) {
    gameLevel++;
    fruitSpeed += 1;
  }
  
  delay(30);
}

// 显示开始界面
void showStartScreen() {
  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_WHITE);
  tft.drawString("快乐接果果", 60, 50);
  tft.drawString("用摇杆左右移动", 50, 100);
  tft.drawString("接住掉落的水果", 60, 130);
  tft.drawString("按A键开始游戏", 60, 180);
}

// 开始新游戏
void startGame() {
  score = 0;
  lives = 3;
  gameLevel = 1;
  fruitSpeed = 3;
  gameRunning = true;
  createNewFruit();
}

// 创建新水果
void createNewFruit() {
  fruitX = random(20, 220);
  fruitY = 0;
  fruitType = random(0, 3);  // 随机选择水果类型
}

// 绘制游戏信息
void drawGameInfo() {
  tft.setTextColor(TFT_WHITE);
  tft.drawString("分数: " + String(score), 10, 10);
  
  // 绘制生命值
  for (int i = 0; i < lives; i++) {
    tft.fillCircle(200 + i * 20, 15, 5, TFT_RED);
  }
}

// 绘制篮子
void drawBasket() {
  tft.fillRoundRect(basketX, 210, 60, 10, 3, TFT_GREEN);
  tft.fillTriangle(basketX+10, 210, basketX+20, 200, basketX+30, 210, TFT_GREEN);
  tft.fillTriangle(basketX+30, 210, basketX+40, 200, basketX+50, 210, TFT_GREEN);
}

// 绘制水果
void drawFruit() {
  switch(fruitType) {
    case 0: // 苹果
      tft.fillCircle(fruitX, fruitY, 10, FRUIT_RED);
      tft.fillCircle(fruitX+3, fruitY-8, 3, TFT_GREEN);
      break;
    case 1: // 香蕉
      tft.fillRoundRect(fruitX-8, fruitY-5, 16, 10, 5, FRUIT_YELLOW);
      break;
    case 2: // 草莓
      tft.fillRoundRect(fruitX-6, fruitY-8, 12, 16, 3, FRUIT_PINK);
      tft.fillTriangle(fruitX-6, fruitY-8, fruitX, fruitY-12, fruitX+6, fruitY-8, TFT_GREEN);
      break;
  }
}

// 接住水果
void catchFruit() {
  score++;
}

// 错过水果
void missFruit() {
  lives--;
  
  if (lives <= 0) {
    gameOver();
  }
}

// 游戏结束
void gameOver() {
  gameRunning = false;
  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_RED);
  tft.drawString("游戏结束", 70, 80);
  tft.setTextColor(TFT_WHITE);
  tft.drawString("最终分数: " + String(score), 80, 120);
  tft.drawString("按A键重新开始", 60, 160);
}

Credits

Mengqi Sun; Qianxi Fu

Posted by Mengqi Sun

Comments

Please log in or sign up to comment.