Академический Документы
Профессиональный Документы
Культура Документы
space: pre-wrap;">
#include <Adafruit_GFX.h> // Adafruit's core graphics library
#include <Adafruit_TFTLCD.h> // Adafruit's hardware-specific
library
#include <TouchScreen.h> //Touchscreen library
#include <Fonts/Org_01.h> //Include a different font
#include <EEPROM.h> //Include the EEPROM library to score
the highscore
#define REDBAR_MINX 80
#define GREENBAR_MINX 130
#define BLUEBAR_MINX 180
#define BAR_MINY 30
#define BAR_HEIGHT 250
#define BAR_WIDTH 30
int currentpcolour;
void drawHome()
{
tft.fillScreen(BLACK);
tft.drawRoundRect(0, 0, 319, 240, 8, WHITE); //Page border
tft.setCursor(60, 20);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(WHITE);
tft.print("Select an example!");
tft.setCursor(70, 50);
tft.setTextSize(2);
tft.setTextColor(LIME);
tft.print("NotesPoint.com");
tft.setTextColor(BLACK);
tft.setCursor(65, 195);
tft.print(" FlappyBird");
tft.setCursor(105, 145);
tft.print("RGB-Mixer");
tft.setCursor(80, 95);
tft.print("Oscilloscope");
// delay(500);
void setup()
{
tft.reset();
tft.begin(tft.readID());
Serial.begin(9600);
Serial.println();
Serial.print("reading id...");
delay(500);
Serial.println(tft.readID(), HEX);
tft.fillScreen(BLACK);
tft.setRotation(1);
currentpage = 0;
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(50, 140);
tft.print("Loading...");
tft.setCursor(30, 100);
tft.print("NotesPoint.com");
tft.fillScreen(BLACK);
drawHome();
}
void loop()
{
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint(); // Read touchscreen
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (currentpage == 0)
{
if (p.z > 10 && p.z < 1000)
{
if (p.x > 736 && p.x < 855 && p.y > 255
&& p.y < 725 && p.z > MINPRESSURE && p.z
< MAXPRESSURE)
{
Serial.println("FlappyBird");
delay(70);
tft.println(" FlappyBird");
delay(70);
currentpage = 1;
startGame();
}
else if (p.x > 563 && p.x < 683 && p.y >
275 && p.y < 750)
{
Serial.println("RGB-MIXER");
tft.print("RGB-Mixer");
delay(70);
currentpage = 2;
x = 0;
y = 0;
p.z = 0;
redpos = BAR_MINY + 12;
greenpos = BAR_MINY + 12;
bluepos = BAR_MINY + 12;
oldrpos = redpos;
oldgpos = greenpos;
oldbpos = bluepos;
drawrgb();
}
if (p.x > 403 && p.x < 525 && p.y > 271
&& p.y < 725)
{
Serial.println("Oscilloscope");
currentpage = 3;
tft.setCursor(80, 95);
tft.print("Oscilloscope");
delay(70);
drawOSC();
}
}
sensereset();
ResetScore();
}
if (x > 40 && x < 80)
{
blueval = map(y, 42, 250, 0, 255);
oldbpos = bluepos;
tft.fillRect(oldbpos, BLUEBAR_MINX + 3, BAR_WIDTH - 10,
BAR_WIDTH - 5, BLACK);
bluepos = y;
tft.fillRect(bluepos, BLUEBAR_MINX + 3, BAR_WIDTH - 10,
BAR_WIDTH - 5, BLUE); //tft.drawRect(BAR_MINY, REDBAR_MINX,
BAR_HEIGHT, BAR_WIDTH, WHITE);
tft.fillCircle(14, BLUEBAR_MINX + 15, 10, tft.color565(0, 0,
blueval));
}
}
if (p.y > 743 && p.x < 247 && p.x > 150
&& p.y < 875)
{
tft.fillRoundRect(5, 5, 50, 30, 8, WHITE);
delay(70);
tft.fillRoundRect(5, 5, 50, 30, 8, BLUE);
tft.drawRoundRect(5, 5, 50, 30, 8, WHITE);
tft.setCursor(15, 15);
tft.print("<-");
delay(70);
tft.fillRoundRect(5, 5, 50, 30, 8, BLACK);
currentpage = 0;
drawHome();
p.x = 160;
p.y = 760;
redval = 0;
blueval = 0;
greenval = 0;
redpos = BAR_MINY + 12;
greenpos = BAR_MINY + 12;
bluepos = BAR_MINY + 12;
oldrpos = redpos;
oldgpos = greenpos;
oldbpos = bluepos;
}
}
if (currentpage == 3) //Oscilloscope
{
for (uint16_t j = 0; j <= tft.width() + 1; j++ )
{
float volt;
float signalY = map(analogRead(A5), 0, 4095, tft.height() - 1,
1 ) ;
// Move items
if (running) {
flY += fallRate;
fallRate++;
pillarPos -= 5;
if (pillarPos == 0) {
score++;
}
else if (pillarPos < -50) {
pillarPos = 320;
gapPosition = random(20, 120); //Random gap position
}
}
}
if (score == EEPROM.read(0) ) //Change the pillar colour when
breaking highscore
currentpcolour = YELLOW;
else
currentpcolour = GREEN;
void checkCollision() {
// Body fill
tft.fillRect(x + 12, y + 2, 6, 2, YELLOW);
tft.fillRect(x + 8, y + 4, 8, 2, YELLOW);
tft.fillRect(x + 6, y + 6, 10, 2, YELLOW);
tft.fillRect(x + 4, y + 8, 12, 2, YELLOW);
tft.fillRect(x + 4, y + 10, 14, 2, YELLOW);
tft.fillRect(x + 4, y + 12, 16, 2, YELLOW);
tft.fillRect(x + 4, y + 14, 14, 2, YELLOW);
tft.fillRect(x + 4, y + 16, 12, 2, YELLOW);
tft.fillRect(x + 6, y + 18, 12, 2, YELLOW);
tft.fillRect(x + 10, y + 20, 10, 2, YELLOW);
// Eye
tft.fillRect(x + 18, y + 2, 2, 2, BLACK);
tft.fillRect(x + 16, y + 4, 2, 6, BLACK);
tft.fillRect(x + 18, y + 10, 2, 2, BLACK);
tft.fillRect(x + 18, y + 4, 2, 6, WHITE);
tft.fillRect(x + 20, y + 2, 4, 10, WHITE);
tft.fillRect(x + 24, y + 4, 2, 8, WHITE);
tft.fillRect(x + 26, y + 6, 2, 6, WHITE);
tft.fillRect(x + 24, y + 6, 2, 4, BLACK);
// Beak
tft.fillRect(x + 20, y + 12, 12, 2, BLACK);
tft.fillRect(x + 18, y + 14, 2, 2, BLACK);
tft.fillRect(x + 20, y + 14, 12, 2, RED);
tft.fillRect(x + 32, y + 14, 2, 2, BLACK);
tft.fillRect(x + 16, y + 16, 2, 2, BLACK);
tft.fillRect(x + 18, y + 16, 2, 2, RED);
tft.fillRect(x + 20, y + 16, 12, 2, BLACK);
tft.fillRect(x + 18, y + 18, 2, 2, BLACK);
tft.fillRect(x + 20, y + 18, 10, 2, RED);
tft.fillRect(x + 30, y + 18, 2, 2, BLACK);
tft.fillRect(x + 20, y + 20, 10, 2, BLACK);
}
// Third wing: up
void drawWing3(int x, int y) {
tft.fillRect(x + 2, y + 6, 8, 2, BLACK);
tft.fillRect(x, y + 8, 2, 6, BLACK);
tft.fillRect(x + 10, y + 8, 2, 2, BLACK);
tft.fillRect(x + 12, y + 10, 2, 4, BLACK);
tft.fillRect(x + 10, y + 14, 2, 2, BLACK);
tft.fillRect(x + 2, y + 14, 2, 2, BLACK);
tft.fillRect(x + 4, y + 16, 6, 2, BLACK);
tft.fillRect(x + 2, y + 8, 8, 6, WHITE);
tft.fillRect(x + 4, y + 14, 6, 2, WHITE);
tft.fillRect(x + 10, y + 10, 2, 4, WHITE);
}
void ResetScore()
{
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint(); // Read touchscreen
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) { //If
sensed a touch
// Serial.print("x = ");
// Serial.println(p.x);
// Serial.print("y = ");
// Serial.println(p.y);
// Serial.print("pressure = ");
// Serial.println(p.z);
}
if (p.x < 199 && p.y < 293 && running == false)
// If reset button pressed while not running, reset the score
{
EEPROM.write(addr, 0); //Reset the score written in the EEPROM
startGame(); //Re-draw everything
}
}
void sensereset()
{
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint(); // Read touchscreen
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.x < 199 && p.y < 293 && !running)
resetsensed = true;
else
resetsensed = false;
}
void startGame() {
// Serial.println("Starting game!");
//Set the default x and y cordinates of the bird
flX = 50;
flY = 125;
fallRate = 1; //Set the fallrate to 1
pillarPos = 320; //Set the pillar on position 320
gapPosition = 60; //Set the pillar's gap on position 60
crashed = false; //Not crashed
score = 0; //Score to 0
highScore = EEPROM.read(addr); //Set the highscore variable to the
data read from the EEPROM position 0
tft.setFont(&Org_01); //Set the font
tft.fillScreen(BLUE); //Fill the screen blue
tft.setTextColor(YELLOW); //Set the text colour to yellow
tft.setTextSize(3); //Text size to 3
tft.setCursor(5, 20); //Cursor on 5,20
tft.println("Flappy Bird");
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.println(" Tap to begin!!");
tft.setTextColor(GREEN);
tft.setCursor(60, 60);
tft.print("Top Score : ");
tft.setTextColor(RED);
tft.setTextSize(3);
tft.print(highScore);
tft.setTextSize(2);
tft.fillRoundRect(240, 5, 250, 15, 8, WHITE);
tft.drawRoundRect(240, 5, 250, 15, 8, RED);
tft.setCursor(250, 15);
tft.println("RESET");
// Draw Ground
int ty = 230; int tx = 0;
for ( tx = 0; tx <= 300; tx += 20) {
tft.fillTriangle(tx, ty, tx + 10, ty, tx, ty + 10, GREEN);
tft.fillTriangle(tx + 10, ty + 10, tx + 10, ty, tx, ty + 10,
YELLOW);
tft.fillTriangle(tx + 10, ty, tx + 20, ty, tx + 10, ty + 10,
YELLOW);
tft.fillTriangle(tx + 20, ty + 10, tx + 20, ty, tx + 10, ty + 10,
GREEN);
}
void senseBack()
{
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint(); // Read touchscreen
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.y < 305 && p.x < 285 && p.x > 239
&& !running)
{
backsensed = true;
}
else
backsensed = false;
}
//RGB
void drawrgb() //RGB mixer function
{
tft.fillScreen(BLACK);
tft.setCursor(90, 20);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("COLOUR : ");