#include "ground.h"
#include <TFT_eSPI.h> // 引入 TFT_eSPI 函式庫
TFT_eSPI tft; // 創建一個 TFT_eSPI 物件
TFT_eSprite sprite = TFT_eSprite(&tft); // 創建一個 TFT_eSprite 物件
void draw(int leftshift = 0){
for(int i = 0; i < 5; i++) {
// 將選擇的圖片添加到 sprite 中,位置從最左邊依序放置
int spriteWidth = 90; // 圖片寬度
int spriteHeight = 38; // 圖片高度
sprite.pushImage(i*spriteWidth - leftshift, 0, spriteWidth, spriteHeight, selectedGround[i]);
// 更新下一張圖片應該放置的位置
}
int spriteWidth = 320;
int spriteHeight = 38;
int spriteX = (tft.width() - spriteWidth) / 2; // 橫向置中
int spriteY = (tft.height() - spriteHeight) / 2; // 垂直置中
sprite.pushSprite(spriteX, spriteY); // 在螢幕上顯示 sprite
}
void setinitialground(){
for (int i = 0; i < 5; i++) {
// 隨機選擇 ground_0 或 ground_1 中的一張圖片
int randomIndex = random(2); // 生成 0 或 1 的隨機數
if (randomIndex == 0) {
selectedGround[i] = ground_0;
} else {
selectedGround[i] = ground_1;
}
}
}
void setnextground(){
selectedGround[0] = selectedGround[1];
selectedGround[1] = selectedGround[2];
selectedGround[2] = selectedGround[3];
selectedGround[3] = selectedGround[4];
// 隨機選擇 ground_0 或 ground_1 中的一張圖片
int randomIndex = random(5); // 生成 0 或 1 的隨機數
if (randomIndex == 0) {
selectedGround[4] = ground_0;
} else if (randomIndex == 1){
selectedGround[4] = ground_1;
} else if (randomIndex == 2){
selectedGround[4] = cactus_0;
} else if (randomIndex == 3){
selectedGround[4] = cactus_1;
} else if (randomIndex == 4){
selectedGround[4] = cactus_2;
}
}
void setup() {
tft.begin(); // 初始化 TFT 螢幕
tft.setRotation(3); // 將螢幕旋轉為橫向顯示
tft.fillScreen(TFT_WHITE); // 將螢幕填滿白色
sprite.setColorDepth(8); // 設定顏色深度為16位色
// 設定 TFT sprite 的位置
int spriteWidth = 320;
int spriteHeight = 38;
// // 初始化 TFT sprite
sprite.createSprite(spriteWidth, spriteHeight);
// // 將 ground_0 圖片放入 sprite 最左邊
// sprite.pushImage(0, 0, 90, 38, ground_0);
// // 將 ground_1 圖片放入 sprite 之後
// sprite.pushImage(90, 0, 90, 38, ground_1);
// 隨機選擇 5 張圖片
setinitialground();
draw();
}
void loop() {
// 程式碼主要在 setup() 中完成,所以 loop() 中不需要任何其他程式碼
static int x = 0;
draw(x++);
if(x==90) {
x = 0;
setnextground();
}
}