#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// Pin definitions for the TFT display
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define TFT_MOSI 11
#define TFT_CLK 13
// Pin definitions for the rotary encoder
const int clkPin = 2;
const int dtPin = 3;
const int swPin = 4;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
int lastStateCLK;
int currentStateCLK;
bool isJumping = false;
int jumpHeight = 0;
int squareY = 120; // Initial Y position of the square
const int groundY = 120;
void setup() {
// Initialize the TFT display
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_WHITE);
tft.drawRect(0, 0, 320, 240, ILI9341_BLACK);
// Initialize rotary encoder pins
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT_PULLUP);
// Read the initial state of the CLK pin
lastStateCLK = digitalRead(clkPin);
// Draw initial square
drawSquare(squareY);
}
void loop() {
// Read the current state of the CLK pin
currentStateCLK = digitalRead(clkPin);
// If the state has changed, then the encoder has been rotated
if (currentStateCLK != lastStateCLK) {
int dtState = digitalRead(dtPin);
// If rotating clockwise, make the square jump
if (dtState != currentStateCLK) {
isJumping = true;
}
}
// If the square is jumping, update jump height
if (isJumping) {
jumpHeight += 5;
if (jumpHeight >= 50) {
isJumping = false;
jumpHeight = 0;
}
}
// Update square Y position
squareY = groundY - jumpHeight;
// Clear screen and redraw
tft.fillScreen(ILI9341_WHITE);
drawSquare(squareY);
// Delay to control game speed
delay(1);
// Update the last states
lastStateCLK = currentStateCLK;
}
// Function to draw square
void drawSquare(int y) {
int x = (tft.width() - 20) / 2; // Center horizontally
tft.fillRect(x, y, 20, 20, ILI9341_RED);
}