Add touch support to Snake game by dividing the screen into Up/Down/Left/Right zones
Main / Build (Brainfuck) (push) Has been cancelled
Main / Build (Breakout) (push) Has been cancelled
Main / Build (Calculator) (push) Has been cancelled
Main / Build (Diceware) (push) Has been cancelled
Main / Build (EpubReader) (push) Has been cancelled
Main / Build (GPIO) (push) Has been cancelled
Main / Build (GraphicsDemo) (push) Has been cancelled
Main / Build (HelloWorld) (push) Has been cancelled
Main / Build (M5UnitTest) (push) Has been cancelled
Main / Build (Magic8Ball) (push) Has been cancelled
Main / Build (MediaKeys) (push) Has been cancelled
Main / Build (MystifyDemo) (push) Has been cancelled
Main / Build (SerialConsole) (push) Has been cancelled
Main / Build (Snake) (push) Has been cancelled
Main / Build (TamaTac) (push) Has been cancelled
Main / Build (TodoList) (push) Has been cancelled
Main / Build (TwoEleven) (push) Has been cancelled
Main / Bundle (push) Has been cancelled

This commit is contained in:
Adolfo Reyna
2026-06-23 12:16:33 -04:00
parent 23e969c1b6
commit 3c6acf47a5
2 changed files with 37 additions and 1 deletions
+36
View File
@@ -245,6 +245,41 @@ static void game_play_event(lv_event_t* e) {
default:
break;
}
} else if (code == LV_EVENT_CLICKED) {
lv_indev_t* indev = lv_indev_active();
if (indev) {
lv_point_t point;
lv_indev_get_point(indev, &point);
lv_area_t coords;
lv_obj_get_coords(game->container, &coords);
lv_coord_t w = coords.x2 - coords.x1 + 1;
lv_coord_t h = coords.y2 - coords.y1 + 1;
lv_coord_t cx = coords.x1 + w / 2;
lv_coord_t cy = coords.y1 + h / 2;
lv_coord_t rx = point.x - cx;
lv_coord_t ry = point.y - cy;
if (rx != 0 || ry != 0) {
if (abs(rx) * h > abs(ry) * w) {
// Horizontal touch
if (rx > 0) {
snake_set_direction(game, SNAKE_DIR_RIGHT);
} else {
snake_set_direction(game, SNAKE_DIR_LEFT);
}
} else {
// Vertical touch
if (ry > 0) {
snake_set_direction(game, SNAKE_DIR_DOWN);
} else {
snake_set_direction(game, SNAKE_DIR_UP);
}
}
}
}
} else if (code == LV_EVENT_KEY) {
uint32_t key = lv_event_get_key(e);
// Arrow keys, WASD, and punctuation keys for cardputer
@@ -394,6 +429,7 @@ lv_obj_t* snake_create(lv_obj_t* parent, uint16_t cell_size, bool wall_collision
// Add event callbacks for touch gestures and keyboard
lv_obj_add_event_cb(game->container, game_play_event, LV_EVENT_GESTURE, obj);
lv_obj_add_event_cb(game->container, game_play_event, LV_EVENT_KEY, obj);
lv_obj_add_event_cb(game->container, game_play_event, LV_EVENT_CLICKED, obj);
lv_obj_add_event_cb(obj, delete_event, LV_EVENT_DELETE, NULL);
// Set up keyboard focus if available