在这个Arduino项目中,我们将使用Arduino和TFT触摸屏制作一个很酷的Arduino游戏,实际上是流行的智能手机Flappy Bird游戏的复制品。 您可以通过观看以下视频或阅读下面的书面文字来了解其工作原理。
概述
游戏非常简单,但有趣且令人上瘾。使用触摸屏,我们可以控制鸟类,并尽量避免碰到随着我们前进而速度增加的移动支柱。即使拔下电源,游戏也可以存储您的最高分。
源代码
由于代码较长,为了更好地理解,我将在各节中发布程序的源代码,并为每个部分提供说明。在本文结尾处,我将发布完整的源代码。
我们将使用Henning Karlsen制造的UTFT和URTouch库。 您可以从他的网站下载这些库。 另外,我们将使用EEPROM库将最高分存储在EEPROM中。 EEPROM是即使在电路板关闭时也可以存储数据的存储器。
包含库之后,我们需要创建UTFT和URTouch对象以及定义游戏所需的变量。 在设置部分,我们需要启动显示和触摸,从EEPROM读取最高分,并使用initializeGame()自定义函数启动游戏。
#include <UTFT.h>
#include <URTouch.h>
#include <EEPROM.h>
//==== Creating Objects
UTFT myGLCD(SSD1289,38,39,40,41); //Parameters should be adjusted to your Display/Schield model
URTouch myTouch( 6, 5, 4, 3, 2);
//==== Defining Fonts
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
extern unsigned int bird01[0x41A]; // Bird Bitmap
int x, y; // Variables for the coordinates where the display has been pressed
// Floppy Bird
int xP = 319;
int yP = 100;
int yB = 50;
int movingRate = 3;
int fallRateInt = 0;
float fallRate = 0;
int score = 0;
int lastSpeedUpScore = 0;
int highestScore;
boolean screenPressed = false;
boolean gameStarted = false;
void setup() {
// Initiate display
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
highestScore = EEPROM.read(0); // Read the highest score from the EEPROM
initiateGame(); // Initiate the game
}
详情参阅 - 亚图跨际