Commit cdba83fe authored by Grzegorz Daniluk's avatar Grzegorz Daniluk

who wants a game for f*watch ?

parent 2a9d5974
......@@ -329,6 +329,52 @@ static const uint8_t battery_data[] = {
0x00};
const struct rle_bitmap battery = { 15, 12, battery_data };
static const uint8_t game_ico_data[] = {
0x8f,
0x0c,
0x81,
0x0c,
0x81,
0x0c,
0x81,
0x0c,
0x81,
0x02,
0x83,
0x05,
0x81,
0x01,
0x85,
0x04,
0x81,
0x00,
0x87,
0x03,
0x81,
0x00,
0x87,
0x03,
0x81,
0x00,
0x87,
0x03,
0x81,
0x00,
0x87,
0x03,
0x81,
0x01,
0x85,
0x04,
0x81,
0x02,
0x83,
0x05,
0x81,
0x0c,
0x8f};
const struct rle_bitmap game_ico = { 15, 15, game_ico_data };
static const uint8_t battery_charging_data[] = {
0x8d,
0x00,
......
......@@ -12,6 +12,7 @@ struct rle_bitmap
extern const struct rle_bitmap comp_circle;
extern const struct rle_bitmap battery;
extern const struct rle_bitmap game_ico;
extern const struct rle_bitmap battery_charging;
extern const struct rle_bitmap gps_searching;
extern const struct rle_bitmap comp_ico;
......
......@@ -192,6 +192,7 @@ src/apps/application.c \
src/apps/clock.c \
src/apps/example_app.c \
src/apps/compass.c \
src/apps/game.c \
src/apps/menu.c \
src/apps/menu_struct.c \
src/main.c \
......
......@@ -34,6 +34,7 @@ extern application menu;
extern application clock_app;
extern application example;
extern application compass;
extern application game;
#endif /* APP_LIST_H */
/*
* Copyright (C) 2014 Julian Lewis
* @author Grzegorz Daniluk <grzegorz.daniluk@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* Very simple "game"
*/
#include "application.h"
#include "widgets/status_bar.h"
#include <drivers/LSM303C/lsm303c.h>
#include <sincos.h>
#include <usbdesc.h>
#define S_WIN_X0 5
#define S_WIN_Y0 25
#define S_WIN_X1 122
#define S_WIN_Y1 122
#define BALL_R 5
#define STEP 10
#define FACT_MOD 4
#define SCSTEP 500
#define MODE_NORM 0
#define MODE_HIGH 1
unsigned int win_x0, win_y0, win_x1, win_y1;
unsigned int score, highscore;
int mode;
int ball_x, ball_y;
static void game_redraw(struct ui_widget *w)
{
char buf[30];
gfx_clear(&w->dc, 0);
if(mode == MODE_HIGH) {
sprintf(buf, "Highscore:");
gfx_centered_text(&w->dc, &font_helv17b, 30, buf, COLOR_BLACK);
sprintf(buf, "%u", highscore);
gfx_centered_text(&w->dc, &font_helv17, 50, buf, COLOR_BLACK);
} else {
sprintf(buf, "Score: %u", score);
gfx_centered_text(&w->dc, &font_helv17, 5, buf, COLOR_BLACK);
gfx_line(&w->dc, win_x0, win_y0, win_x0, win_y1, COLOR_BLACK);
gfx_line(&w->dc, win_x0, win_y0, win_x1, win_y0, COLOR_BLACK);
gfx_line(&w->dc, win_x0, win_y1, win_x1, win_y1, COLOR_BLACK);
gfx_line(&w->dc, win_x1, win_y0, win_x1, win_y1, COLOR_BLACK);
gfx_fill_circle(&w->dc, ball_x, ball_y, BALL_R, COLOR_BLACK);
}
}
static void game_event(struct ui_widget *w, const struct event *evt)
{
w->flags |= WF_DIRTY;
}
struct ui_widget game_widget = {
game_redraw,
game_event,
{ 0, 0, 127, 127 },
0,
WF_ACTIVE | WF_VISIBLE
};
struct ui_widget game_screen = {
NULL,
NULL,
{ 0, 0, 127, 127},
0,
WF_ACTIVE | WF_VISIBLE
};
void game_main(void *params)
{
(void)(params);
struct event evt;
lsm303_smpl acc;
int fakap = 0;
char buf[30];
int old_x, old_y;
int dir = 0, fact;
lsm303_init();
USBD_Init(&initstruct);
buzzer_set_freq(4000);
ball_x = 64;
ball_y = 64;
old_x = 64;
old_y = 64;
score = 0;
highscore = 0;
win_x0 = S_WIN_X0;
win_x1 = S_WIN_X1;
win_y0 = S_WIN_Y0;
win_y1 = S_WIN_Y1;
ui_clear();
ui_init_widget(&game_screen);
ui_init_widget(&game_widget);
ui_add_widget(&game_widget);
ui_add_child(&game_screen, &game_widget);
ui_add_widget(&game_screen);
/*draw screen for the first time*/
ui_update(NULL);
/*main loop*/
while(1) {
if(xQueueReceive(appQueue, &evt, 0)) {
switch(evt.type) {
case BUTTON_PRESSED:
if(evt.data.button == BUT_TR) {
vibra_disable();
buzzer_disable();
return;
}
if(evt.data.button == BUT_BR)
mode = MODE_HIGH;
if(evt.data.button == BUT_BL)
mode = MODE_NORM;
}
}
lsm303_get_sample(DEV_ACC, &acc);
ball_x -= acc.x/100;
ball_y -= acc.y/100;
if(ball_x >= win_x1 - BALL_R) {
ball_x = win_x1 - BALL_R;
fakap = 1;
}
if(ball_x <= win_x0 + BALL_R) {
ball_x = win_x0 + BALL_R;
fakap = 1;
}
if(ball_y >= win_y1 - BALL_R) {
ball_y = win_y1 - BALL_R;
fakap = 1;
}
if(ball_y <= win_y0 + BALL_R) {
ball_y = win_y0 + BALL_R;
fakap = 1;
}
if(ball_x > win_x0+BALL_R && ball_x < win_x1-BALL_R &&
ball_y > win_y0+BALL_R && ball_y < win_y1-BALL_R )
fakap = 0;
if(fakap) {
/* uhh.. that's bad, PUNISHMENT !!! */
score = 0;
win_x0 = S_WIN_X0;
win_x1 = S_WIN_X1;
win_y0 = S_WIN_Y0;
win_y1 = S_WIN_Y1;
vibra_enable();
buzzer_enable();
vTaskDelay(100);
vibra_disable();
buzzer_disable();
} else {
/*1. the more you move the more points you get*/
/*2. the smaller box is, the more points you get */
fact = (score/SCSTEP)%FACT_MOD;
score += (fact+1)*(ABS(ball_x-old_x) + ABS(ball_y-old_y));
if(score > highscore)
highscore = score;
vibra_disable();
buzzer_disable();
/*update box based on score*/
win_x0 = S_WIN_X0 + fact*STEP;
win_y0 = S_WIN_Y0 + fact*STEP;
win_x1 = S_WIN_X1 - fact*STEP;
win_y1 = S_WIN_Y1 - fact*STEP;
}
old_x = ball_x;
old_y = ball_y;
ui_update(&evt);
}
}
application game = {
.name = "Ball game",
.main = game_main
};
......@@ -51,6 +51,7 @@ menu_list main_menu = {
{
{ APP, &example_icon, { .app = &example } },
{ APP, &comp_ico, { .app = &compass } },
{ APP, &game_ico, { .app = &game} },
{ SUBMENU, &settings_icon, { .submenu = &settings_menu } },
{ END, NULL, { NULL } }
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment