#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define BLACK 0x0000
#define BLUE 0xF800
#define RED 0x001F
#define GREEN 0x07E0
#define DARKGREEN 0x2666
#define CYAN 0xFFE0
#define ORANGE 0x2C1C
#define YELLOW 0x07FF
#define YELLOW2 0x371C
#define WHITE 0xFFFF
float ox = 240;
int oy1 = random(10, 100);
int oy2 = 0;
float vx = 0.3;
int oysweep;
float posx = 40;
float posy = 108;
int pos;
int tiempo;
bool tick;
bool salto;
float caida = 0.05;
bool perdio = false;
unsigned long millis1;
unsigned long millis2;
int puntaje = 0;
int juego;
int start=1;
int jsel=1;
int direccion;
int comida;
bool snakecomida;
int largo[32];
const unsigned int pajaro [] PROGMEM =
{ CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN,
CYAN, CYAN, CYAN, YELLOW, YELLOW, YELLOW, WHITE, WHITE, WHITE, CYAN, CYAN, CYAN,
CYAN, CYAN, YELLOW, YELLOW, YELLOW, YELLOW, WHITE, WHITE, BLACK, CYAN, CYAN, CYAN,
YELLOW2, YELLOW2, YELLOW2, YELLOW, YELLOW, YELLOW, WHITE, WHITE, BLACK, CYAN, CYAN, CYAN,
YELLOW2, YELLOW, YELLOW, YELLOW2, YELLOW, YELLOW, YELLOW, WHITE, WHITE, YELLOW, CYAN, CYAN,
YELLOW2, YELLOW, YELLOW, YELLOW, YELLOW2, YELLOW, YELLOW, YELLOW, ORANGE, ORANGE, ORANGE, CYAN,
YELLOW2, YELLOW, YELLOW, YELLOW2, YELLOW2, YELLOW, YELLOW, YELLOW, ORANGE, ORANGE, ORANGE, ORANGE,
CYAN, YELLOW2, YELLOW2, YELLOW, YELLOW, YELLOW, YELLOW, YELLOW, YELLOW, YELLOW, CYAN, CYAN,
CYAN, CYAN, YELLOW, YELLOW, YELLOW, YELLOW, YELLOW, YELLOW, YELLOW, CYAN, CYAN, CYAN,
CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN, CYAN,
};
void fondo(){
tft.fillScreen(BLACK);
tft.fillRect(0, 40, 240, 160, CYAN);
tft.fillRect(0, 200, 240, 40, GREEN);
}
void setup(){
juego=0;
pinMode(2, INPUT); //ARRIBA
pinMode(3, INPUT); //SELECT
pinMode(4, INPUT); //DERECHA
pinMode(5, INPUT); //IZQUIERDA
pinMode(6, INPUT); //ABAJO
tft.begin();
tft.fillScreen(BLACK);
tft.setCursor(10, 10);
tft.setTextSize(2);
tft.setRotation(0); //setRotation(3)
tft.fillCircle(123, 121, 50, WHITE);
}
void flappy(){
tft.fillScreen(BLACK);
tft.setCursor(10, 10);
tft.setTextSize(2);
tft.print("Puntaje:");
tft.setTextColor(WHITE);
tft.setCursor(110, 10);
tft.setTextSize(2);
tft.print(puntaje);
fondo();
}
void pj(){
if(posy >= 188 || posy <= 41){
perdio = true;
}
if((posx >= ox-13 && posx <= ox+30) && (posy >= oy2-12)){
perdio = true;
}
if((posx >= ox-13 && posx <= ox+30) && (posy <= oy2-39)){
perdio = true;
}
if(digitalRead(2) == HIGH && salto == false){
salto = true;
}
if(digitalRead(2) == LOW){
salto = false;
}
if(salto == true){
millis1 = millis();
caida=-0.3;
salto = false;
} else {
caida+=0.002;
}
if(perdio == true){
caida = 0;
}
}
void sprite_pj(){
tft.drawRGBBitmap(posx, posy, pajaro, 12, 10);
}
void tubo(){
ox -= vx;
oy2 = oy1 + 80;
oysweep = 120 - oy1;
if(ox <= -32){
vx + 0.1;
ox = 240;
oy1 = random(10,100);
tft.fillRect(-40, 40, 50, 120, CYAN);
}
if((millis() % 10 == 0) && (perdio == false)){
tft.fillRect(ox, 40, 30, oy1, DARKGREEN);
tft.fillRect(ox+30, 40, 10, oy1, CYAN);
tft.fillRect(ox, oy2, 30, oysweep, DARKGREEN);
tft.fillRect(ox+30, oy2, 10, oysweep, CYAN);
}
}
void puntos(){
if((ox >= 39.7 && ox <= 40) && (perdio == false)){
puntaje++;
tft.fillRect(110,10,20,20,BLACK);
tft.setTextColor(WHITE);
tft.setCursor(110, 10);
tft.setTextSize(2);
tft.print(puntaje);
}
}
void menu() {
tft.setCursor(40, 35);
tft.setTextSize(4);
tft.setTextColor(WHITE);
tft.print ("OKTUBOY");
trollface();
if(digitalRead(2)==1){
delay(200);
if(digitalRead(2)==0){
jsel=jsel-1;
if(jsel<1){
jsel=1;
}
}
}
if(digitalRead(6)==1){
delay(200);
if(digitalRead(6)==0){
if(jsel<3){
jsel=jsel+1;
if(jsel>3){
jsel=3;
}
}
}
}
if(jsel==1){
tft.setTextSize(2);
tft.setCursor(57, 200);
tft.setTextColor(RED);
tft.print ("Flappy Bird");
if(digitalRead(3)==1){
juego=1;
}
}
else {
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(57, 200);
tft.print ("Flappy Bird");
}
if(jsel==2){
tft.setTextSize(2);
tft.setCursor(87, 225);
tft.setTextColor(RED);
tft.print ("Tetris");
if(digitalRead(3)==1){
juego=2;
}
}
else {
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(87, 225);
tft.print ("Tetris");
}
if(jsel==3){
tft.setTextSize(2);
tft.setCursor(92, 250);
tft.setTextColor(RED);
tft.print ("Snake");
if(digitalRead(3)==1){
juego=3;
}
}
else {
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(92, 250);
tft.print ("Snake");
}
}
void(* resetFunc) (void) = 0;
void tetrissetup() {
tft.fillScreen(BLUE);
tft.setCursor(10, 50);
tft.setTextSize(3);
tft.print("TETRIS");
}
//SNAKE********************
void snakesetup(){
tft.fillScreen(BLUE);
tft.setTextColor(WHITE);
tft.setCursor(72, 20);
tft.setTextSize(3);
tft.print("SNAKE");
tft.fillRect(12,60,216,216,WHITE);
tft.fillRect(24,72,192,192,BLUE);
comida=random(1,255);
pos=20;
start=0;
delay(1000);
direccion=1;
tft.setTextSize(2);
largo[0]=pos;
}
void snakeloop(){
delay(250);
tft.fillRect(50,290,220,50,BLUE);
tft.setCursor(50,290);
tft.print("Puntaje: ");
tft.setCursor(150,290);
tft.print(puntaje);
while(comida==pos){
comida=random(0,255);
}
if(snakecomida==false){
tft.fillRect(24+(comida%16)*12,((comida-comida%16)/16)*12+72,12,12,GREEN);
snakecomida=true;
}
if(digitalRead(2)==1){
direccion=3;
}
if(digitalRead(4)==1){
direccion=1;
}
if(digitalRead(5)==1){
direccion=2;
}
if(digitalRead(6)==1){
direccion=4;
}
switch(direccion){
case 1:
pos=pos+1; break;
case 2:
pos=pos-1; break;
case 3:
pos=pos-16; break;
case 4:
pos=pos+16; break;
}
if(pos==comida){
snakecomida=false;
puntaje++;
}
if(pos<0 || pos>255 ||(pos%16==0 && largo[0]==pos-1) || (pos%16==15 && largo[0]==pos+1)){
resetFunc();
}
tft.fillRect(24+(largo[puntaje]%16)*12,((largo[puntaje]-largo[puntaje]%16)/16)*12+72,12,12,BLUE);
tft.fillRect(24+(pos%16)*12,((pos-pos%16)/16)*12+72,12,12,RED);
for(int i=puntaje;i>=0;i--){
largo[i+1]=largo[i];
if(pos==largo[i]){
resetFunc();
}
}
largo[0]=pos;
}
//Loop***************************
void loop(){
if(digitalRead(4)==HIGH && digitalRead(5)==HIGH) {
resetFunc();
}
if(juego==0){
menu();
}
if(juego==1){
if(start==1){
fondo();
}
start=0;
pj();
sprite_pj();
posy+=caida;
tubo();
puntos();
if(perdio==1){
resetFunc();
}
}
if(juego==2){
tetrissetup();
}
if(juego==3){
if(start==1){
snakesetup();
}
if(start==0){
snakeloop();
}
}
}
void trollface(){
tft.drawRect(100, 90, 24, 0, BLACK); tft.drawRect(124, 92, 20, 0, BLACK); tft.drawRect(98, 91, 2, 2, BLACK);
tft.drawRect(96, 93, 2, 2, BLACK); tft.drawRect(144, 93, 4, 2, BLACK); tft.drawRect(148, 95, 2, 2, BLACK);
tft.drawRect(94, 95, 2, 2, BLACK); tft.drawRect(92, 97, 2, 2, BLACK); tft.drawRect(150, 97, 2, 2, BLACK);
tft.drawRect(90, 99, 2, 4, BLACK); tft.drawRect(152, 99, 2, 4, BLACK); tft.drawRect(154, 103, 2, 2, BLACK);
tft.drawRect(88, 103, 2, 4, BLACK); tft.drawRect(156, 105, 2, 2, BLACK); tft.drawRect(158, 107, 2, 10, BLACK);
tft.drawRect(84, 107, 4, 2, BLACK); tft.drawRect(82, 109, 2, 2, BLACK); tft.drawRect(80, 111, 2, 8, BLACK);
tft.drawRect(156, 117, 2, 20, BLACK); tft.drawRect(82, 119, 2, 2, BLACK); tft.drawRect(84, 121, 2, 2, BLACK);
tft.drawRect(86, 123, 2, 2, BLACK); tft.drawRect(88, 125, 2, 4, BLACK); tft.drawRect(90, 129, 2, 4, BLACK);
tft.drawRect(92, 133, 2, 2, BLACK); tft.drawRect(94, 135, 2, 2, BLACK); tft.drawRect(96, 137, 2, 2, BLACK);
tft.drawRect(98, 139, 2, 2, BLACK); tft.drawRect(100, 141, 4, 2, BLACK); tft.drawRect(104, 143, 6, 2, BLACK);
tft.drawRect(110, 145, 4, 2, BLACK); tft.drawRect(114, 147, 4, 2, BLACK); tft.drawRect(118, 149, 4, 2, BLACK);
tft.drawRect(122, 151, 10, 2, BLACK); tft.drawRect(132, 153, 20, 2, BLACK); tft.drawRect(152, 151, 2, 2, BLACK);
tft.drawRect(154, 149, 2, 2, BLACK); tft.drawRect(156, 145, 2, 4, BLACK); tft.drawRect(158, 137, 2, 8, BLACK);
tft.drawRect(138, 141, 8, 2, BLACK); tft.drawRect(120, 143, 18, 2, BLACK); tft.drawRect(146, 139, 2, 2, BLACK);
tft.drawRect(148, 129, 2, 10, BLACK); tft.drawRect(150, 121, 2, 14, BLACK); tft.drawRect(146, 121, 2, 14, BLACK);
tft.drawRect(140, 127, 2, 2, BLACK); tft.drawRect(140, 129, 6, 2, BLACK); tft.drawRect(140, 137, 2, 4, BLACK);
tft.drawRect(142, 123, 4, 2, BLACK); tft.drawRect(136, 125, 6, 2, BLACK); tft.drawRect(148, 117, 2, 4, BLACK);
tft.drawRect(116, 141, 4, 2, BLACK); tft.drawRect(112, 139, 4, 2, BLACK); tft.drawRect(108, 137, 4, 2, BLACK);
tft.drawRect(104, 135, 4, 2, BLACK); tft.drawRect(102, 133, 4, 2, BLACK); tft.drawRect(100, 131, 2, 2, BLACK);
tft.drawRect(98, 129, 2, 2, BLACK); tft.drawRect(96, 125, 2, 4, BLACK); tft.drawRect(94, 123, 6, 2, BLACK);
tft.drawRect(94, 117, 2, 6, BLACK); tft.drawRect(100, 125, 4, 2, BLACK); tft.drawRect(88, 117, 2, 4, BLACK);
tft.drawRect(86, 113, 2, 4, BLACK); tft.drawRect(88, 111, 2, 2, BLACK); tft.drawRect(90, 109, 2, 2, BLACK);
tft.drawRect(92, 107, 4, 2, BLACK); tft.drawRect(96, 109, 6, 2, BLACK); tft.drawRect(102, 111, 10, 2, BLACK);
tft.drawRect(112, 109, 2, 2, BLACK); tft.drawRect(106, 107, 12, 2, BLACK); tft.drawRect(104, 105, 2, 2, BLACK);
tft.drawRect(108, 105, 6, 2, BLACK); tft.drawRect(120, 105, 2, 2, BLACK); tft.drawRect(122, 107, 2, 2, BLACK);
tft.drawRect(118, 109, 4, 2, BLACK); tft.drawRect(118, 115, 2, 4, BLACK); tft.drawRect(120, 113, 2, 2, BLACK);
tft.drawRect(120, 119, 2, 2, BLACK); tft.drawRect(122, 117, 4, 2, BLACK); tft.drawRect(130, 119, 4, 2, BLACK);
tft.drawRect(134, 117, 2, 2, BLACK); tft.drawRect(136, 115, 2, 2, BLACK); tft.drawRect(134, 113, 2, 2, BLACK);
tft.drawRect(132, 107, 2, 6, BLACK); tft.drawRect(130, 105, 2, 2, BLACK); tft.drawRect(134, 105, 6, 2, BLACK);
tft.drawRect(136, 107, 8, 2, BLACK); tft.drawRect(136, 103, 8, 2, BLACK); tft.drawRect(144, 105, 2, 2, BLACK);
tft.drawRect(142, 111, 4, 2, BLACK); tft.drawRect(146, 109, 8, 2, BLACK); tft.drawRect(106, 103, 14, 2, BLACK);
tft.drawRect(104, 119, 2, 14, BLACK); tft.drawRect(106, 127, 2, 2, BLACK); tft.drawRect(108, 129, 4, 2, BLACK);
tft.drawRect(112, 131, 34, 2, BLACK); tft.drawRect(116, 133, 30, 2, BLACK); tft.drawRect(124, 135, 2, 8, BLACK);
tft.drawRect(126, 127, 2, 4, BLACK); tft.drawRect(132, 127, 2, 4, BLACK); tft.drawRect(134, 137, 2, 6, BLACK);
tft.drawRect(134, 135, 14, 2, BLACK); tft.drawRect(114, 125, 2, 6, BLACK); tft.drawRect(114, 133, 2, 6, BLACK);
tft.drawRect(118, 125, 18, 2, BLACK); tft.drawRect(114, 123, 4, 2, BLACK); tft.drawRect(110, 121, 4, 2, BLACK);
tft.drawRect(106, 119, 4, 2, BLACK); tft.drawRect(102, 117, 4, 2, BLACK); tft.drawRect(98, 115, 4, 2, BLACK);
tft.drawRect(94, 113, 4, 2, BLACK); tft.drawRect(94, 111, 2, 2, BLACK); tft.drawRect(90, 115, 4, 2, BLACK);
tft.drawRect(96, 101, 2, 2, BLACK); tft.drawRect(98, 99, 2, 2, BLACK); tft.drawRect(100, 97, 2, 2, BLACK);
tft.drawRect(102, 95, 4, 2, BLACK); tft.drawRect(106, 93, 14, 2, BLACK); tft.drawRect(102, 101, 2, 2, BLACK);
tft.drawRect(100, 103, 2, 2, BLACK); tft.drawRect(104, 99, 4, 2, BLACK);
tft.drawRect(108, 97, 10, 2, BLACK); tft.drawRect(120, 97, 2, 4, BLACK); tft.drawRect(122, 99, 2, 4, BLACK);
tft.drawRect(132, 101, 2, 2, BLACK); tft.drawRect(134, 99, 10, 2, BLACK); tft.drawRect(130, 95, 14, 2, BLACK);
tft.drawRect(144, 101, 4, 2, BLACK); tft.drawRect(144, 97, 4, 2, BLACK); tft.drawRect(148, 103, 2, 2, BLACK);
tft.drawRect(140, 147, 8, 2, BLACK); tft.drawRect(148, 145, 2, 2, BLACK); tft.drawRect(150, 143, 2, 2, BLACK);
}