/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define PRIMARY_COLOR 0x4A11
#define PRIMARY_LIGHT_COLOR 0x7A17
#define PRIMARY_DARK_COLOR 0x4016
#define PRIMARY_TEXT_COLOR 0x7FFF
#define COLOR_FONDO BLACK
#define N_COLORES 6
const uint16_t colores[N_COLORES] = {
RED, BLUE, GREEN, CYAN, MAGENTA, YELLOW,
};
#define BTN_I 2
#define BTN_D 3
#define FPS 60
static const uint16_t pantalla_w = 240;
static const uint16_t pantalla_h = 320;
struct Juego {
uint16_t paleta_w = 70;
uint16_t paleta_h = 15;
uint16_t paleta_x = pantalla_w / 2 - paleta_w / 2;
static const uint16_t paleta_y = 230;
uint16_t paleta_x_old = paleta_x;
static const uint8_t mapa_w = 6;
static const uint8_t mapa_h = 9;
bool mapa[mapa_w * mapa_h];
void dibujar_paleta() {
tft.fillRect(paleta_x, paleta_y, paleta_w, paleta_h, YELLOW);
if (paleta_x != paleta_x_old) {
if (paleta_x < paleta_x_old) {
tft.fillRect(paleta_x + paleta_w + 5, paleta_y, abs(paleta_x - paleta_x_old - 5), paleta_h, COLOR_FONDO);
} else {
tft.fillRect(paleta_x_old - 5, paleta_y, abs(paleta_x - paleta_x_old + 5), paleta_h, COLOR_FONDO);
}
}
}
void dibujar_mapa() {
uint16_t bloque_w = pantalla_w / mapa_w;
uint16_t bloque_h = (pantalla_h - pantalla_h * 0.45) / mapa_h;
for (uint8_t x = 0; x < mapa_w; x++) {
for (uint8_t y = 0; y < mapa_h; y++) {
tft.fillRect(x * bloque_w + 3, y * bloque_h + 3, bloque_w - 3, bloque_h - 3, colores[y % N_COLORES]);
}
}
}
};
Juego juego;
void setup() {
pinMode(BTN_I, INPUT_PULLUP);
pinMode(BTN_D, INPUT_PULLUP);
tft.begin();
for (uint8_t x = 0; x < juego.mapa_w; x++) {
for (uint8_t y = 0; y < juego.mapa_h; y++) {
juego.mapa[x + y * (juego.mapa_h - 1)] = true;
}
}
juego.dibujar_mapa();
juego.dibujar_paleta();
}
void loop() {
if (digitalRead(BTN_I) == LOW) {
juego.paleta_x = juego.paleta_x - 2;
}
if (digitalRead(BTN_D) == LOW) {
juego.paleta_x = juego.paleta_x + 2;
}
juego.dibujar_paleta();
juego.paleta_x_old = juego.paleta_x;
delay(1000 / 60);
}