Skip to content

Commit

Permalink
Add the first game! 2048 game!
Browse files Browse the repository at this point in the history
Actual game implementation added as a submodule from https://github.com/100askTeam/lv_lib_100ask.
  • Loading branch information
jakkra committed Jun 16, 2023
1 parent d2cc8be commit 8a71f5d
Show file tree
Hide file tree
Showing 8 changed files with 580 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "app/src/ext_drivers/bmp581/BMP5-Sensor-API"]
path = app/src/ext_drivers/bmp581/BMP5-Sensor-API
url = https://github.com/boschsensortec/BMP5-Sensor-API.git
[submodule "app/src/applications/2048/2048_lib"]
path = app/src/applications/2048/2048_lib
url = https://github.com/100askTeam/lv_lib_100ask.git
1 change: 1 addition & 0 deletions app/src/applications/2048/2048_lib
Submodule 2048_lib added at e39c80
6 changes: 6 additions & 0 deletions app/src/applications/2048/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FILE(GLOB app_sources *.c)
target_sources(app PRIVATE ${app_sources})
set(LV_USE_100ASK_2048 "1")
target_include_directories(app PRIVATE 2048_lib/src/lv_100ask_2048)
target_sources(app PRIVATE 2048_lib/src/lv_100ask_2048/lv_100ask_2048.c)

37 changes: 37 additions & 0 deletions app/src/applications/2048/game_2048_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <2048/game_2048_ui.h>
#include <application_manager.h>
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <lv_100ask_2048.h>

// Functions needed for all applications
static void game_2048_app_start(lv_obj_t *root, lv_group_t *group);
static void game_2048_app_stop(void);

LV_IMG_DECLARE(icon_2048);

static application_t app = {
.name = "2048",
.icon = &icon_2048,
.start_func = game_2048_app_start,
.stop_func = game_2048_app_stop
};

static void game_2048_app_start(lv_obj_t *root, lv_group_t *group)
{
game_2048_ui_show(root);
}

static void game_2048_app_stop(void)
{
game_2048_ui_remove();
}

static int game_2048_app_add(const struct device *arg)
{
application_manager_add_application(&app);

return 0;
}

SYS_INIT(game_2048_app_add, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
61 changes: 61 additions & 0 deletions app/src/applications/2048/game_2048_ui.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <2048/game_2048_ui.h>
#include <lvgl.h>
#include <lv_100ask_2048.h>

static void game_2048_event_cb(lv_event_t * e);

static lv_obj_t *root_page = NULL;

void game_2048_ui_show(lv_obj_t *root)
{
assert(root_page == NULL);
lv_obj_t* obj_2048;

// Create the root container
root_page = lv_obj_create(root);
// Remove the default border
lv_obj_set_style_border_width(root_page, 0, LV_PART_MAIN);
// Make root container fill the screen
lv_obj_set_size(root_page, LV_PCT(100), LV_PCT(100));
// Don't want it to be scollable. Putting anything close the edges
// then LVGL automatically makes the page scrollable and shows a scroll bar.
// Does not look very good on the round display.
lv_obj_set_scrollbar_mode(root_page, LV_SCROLLBAR_MODE_OFF);

lv_obj_set_style_bg_color(root_page, lv_color_hex(0xb3a397), LV_PART_MAIN); // Match 2048 game background color

obj_2048 = lv_100ask_2048_create(root_page);
lv_obj_set_size(obj_2048, 200, 200);
lv_obj_center(obj_2048);
lv_obj_set_style_outline_width(obj_2048, 0, LV_STATE_FOCUS_KEY | LV_STATE_DEFAULT);

// Score
lv_obj_t * label = lv_label_create(root_page);
lv_label_set_recolor(label, true);
lv_label_set_text_fmt(label, "#ff00ff %d #", lv_100ask_2048_get_score(obj_2048));
lv_obj_align_to(label, obj_2048, LV_ALIGN_OUT_TOP_MID, -20, 0);
lv_obj_add_event_cb(obj_2048, game_2048_event_cb, LV_EVENT_ALL, label);
}

void game_2048_ui_remove(void)
{
lv_obj_del(root_page);
root_page = NULL;
}

static void game_2048_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj_2048 = lv_event_get_target(e);
lv_obj_t * label = lv_event_get_user_data(e);

if (code == LV_EVENT_VALUE_CHANGED) {
if (lv_100ask_2048_get_best_tile(obj_2048) >= 2048) {
lv_label_set_text(label, "#00b329 YOU WIN! #");
} else if(lv_100ask_2048_get_status(obj_2048)) {
lv_label_set_text(label, "#ff0000 GAME OVER! #");
} else {
lv_label_set_text_fmt(label, "#ff00ff %d #", lv_100ask_2048_get_score(obj_2048));
}
}
}
8 changes: 8 additions & 0 deletions app/src/applications/2048/game_2048_ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <inttypes.h>
#include <lvgl.h>

void game_2048_ui_show(lv_obj_t *root);

void game_2048_ui_remove(void);
Loading

0 comments on commit 8a71f5d

Please sign in to comment.