modular games; draft 2048

master
Clyne 4 years ago
parent 245866891a
commit 7c8a16289c

104
2048.c

@ -0,0 +1,104 @@
#include "buttons.h"
#include "dogs.h"
static int values[16];
void g2048_init()
{
for (int i = 0; i < 16; i++)
values[i] = 0;
}
int g2048_loop()
{
if ((button_state & BUTTON_JOYUP) == BUTTON_JOYUP) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
if (values[4 * y + x] != 0)
continue;
}
}
for (int y = 3; y > 0; y--) {
for (int x = 0; x < 4; x++) {
if (values[4 * y + x] != 0) {
if (values[4 * (y - 1) + x] == 0)
values[4 * (y - 1) + x] = values[4 * y + x], values[4 * y + x] = 0;
else if (values[4 * (y - 1) + x] == values[4 * y + x])
values[4 * (y - 1) + x]++, values[4 * y + x] = 0;
}
}
}
for (int i = 15; i >= 0; i--) {
if (values[i] == 0) {
values[i] = 1;
break;
}
}
} else if ((button_state & BUTTON_JOYDOWN) == BUTTON_JOYDOWN) {
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 4; x++) {
if (values[4 * y + x] != 0) {
if (values[4 * (y + 1) + x] == 0)
values[4 * (y + 1) + x] = values[4 * y + x], values[4 * y + x] = 0;
else if (values[4 * (y + 1) + x] == values[4 * y + x])
values[4 * (y + 1) + x]++, values[4 * y + x] = 0;
}
}
}
for (int i = 15; i >= 0; i--) {
if (values[i] == 0) {
values[i] = 1;
break;
}
}
} else if ((button_state & BUTTON_JOYLEFT) == BUTTON_JOYLEFT) {
for (int x = 3; x > 0; x--) {
for (int y = 0; y < 4; y++) {
if (values[4 * y + x] != 0) {
if (values[4 * y + x - 1] == 0)
values[4 * y + x - 1] = values[4 * y + x], values[4 * y + x] = 0;
else if (values[4 * y + x - 1] == values[4 * y + x])
values[4 * y + x - 1]++, values[4 * y + x] = 0;
}
}
}
for (int i = 15; i >= 0; i--) {
if (values[i] == 0) {
values[i] = 1;
break;
}
}
} else if ((button_state & BUTTON_JOYRIGHT) == BUTTON_JOYRIGHT) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 4; y++) {
if (values[4 * y + x] != 0) {
if (values[4 * y + x + 1] == 0)
values[4 * y + x + 1] = values[4 * y + x], values[4 * y + x] = 0;
else if (values[4 * y + x + 1] == values[4 * y + x])
values[4 * y + x + 1]++, values[4 * y + x] = 0;
}
}
}
for (int i = 15; i >= 0; i--) {
if (values[i] == 0) {
values[i] = 1;
break;
}
}
}
dogs_clear();
for (int i = 0; i < 16; i++) {
if (values[i] != 0)
draw_number(31 + 10 * (i % 4), 12 + 10 * (3 - (i / 4)), values[i]);
}
dogs_flush();
return 500;
}

@ -0,0 +1,3 @@
void g2048_init();
int g2048_loop();

@ -123,6 +123,8 @@ CSRC = $(ALLCSRC) \
$(TESTSRC) \
dogs.c \
buttons.c \
flapbird.c \
2048.c \
main.c
# C++ sources that can be compiled in ARM or THUMB mode depending on the global

@ -21,6 +21,11 @@
#define BUTTON_2 (1 << 1)
#define BUTTON_3 (1 << 0)
#define BUTTON_JOYUP (BUTTON_JOYUL | BUTTON_JOYUR)
#define BUTTON_JOYDOWN (BUTTON_JOYDL | BUTTON_JOYDR)
#define BUTTON_JOYLEFT (BUTTON_JOYUL | BUTTON_JOYDL)
#define BUTTON_JOYRIGHT (BUTTON_JOYUR | BUTTON_JOYDR)
extern unsigned char button_state;
void buttons_init();

@ -166,7 +166,7 @@ void dogs_clear()
*ptr++ = 0;
*ptr++ = 0;
}
for (; count >= 0; count--)
for (; --count >= 0;)
*ptr++ = 0;
}

@ -12,6 +12,8 @@
#ifndef DOGS_H_
#define DOGS_H_
#include <stdbool.h>
#define DISP_WIDTH 102
#define DISP_HEIGHT 64

@ -0,0 +1,80 @@
#include "buttons.h"
#include "dogs.h"
static const unsigned char bird[] = {
8, 8,
0b00111100,
0b01100110,
0b01000010,
0b01111110,
0b01100110,
0b01000010,
0b11000000,
0b11000000,
};
static int score;
static int t1x, t1o;
static int t2x, t2o;
static int py;
static int vy;
static int counter;
void flapbird_init()
{
score = 0;
t1x = DISP_WIDTH / 2, t1o = 15;
t2x = DISP_WIDTH, t2o = 49;
py = DISP_HEIGHT / 2 - 4;
vy = 0;
counter = 0;
}
int flapbird_loop()
{
// Player logic
if (py > 0) {
py += vy;
if (vy > -3)
vy--;
} else {
if (py < 0)
py = 0;
if (score > 0)
score = 0;
}
if (button_state & BUTTON_2) {
vy = 5;
if (py <= 0)
py = 1;
}
// Rendering
dogs_clear();
draw_rect(t1x, 0, 4, t1o - 10);
draw_rect(t1x, t1o + 10, 4, DISP_HEIGHT - t1o + 10);
draw_rect(t2x, 0, 4, t2o - 10);
draw_rect(t2x, t2o + 10, 4, DISP_HEIGHT - t2o + 10);
draw_bitmap(4, py, bird);
draw_number(DISP_WIDTH - 25, DISP_HEIGHT - 8, score);
dogs_flush();
// Game logic
if (t1x == 4)
score = (py + 2 > t1o - 10 && py + 6 < t1o + 10) ? score + 1 : 0;
if (t2x == 4)
score = (py + 2 > t2o - 10 && py + 6 < t2o + 10) ? score + 1 : 0;
t1x -= 2;
if (t1x <= -5)
t1x = DISP_WIDTH;
t2x -= 2;
if (t2x <= -5)
t2x = DISP_WIDTH;
return 100;
}

@ -0,0 +1,8 @@
#ifndef FLAPBIRD_H_
#define FLAPBIRD_H_
void flapbird_init();
int flapbird_loop();
#endif // FLAPBIRD_H_

@ -14,6 +14,9 @@
#include "dogs.h"
#include "hal.h"
#include "2048.h"
#include "flapbird.h"
/*
* Progress:
* - Serial through LPUART1 works (38400 baud, takes over swdio pins)
@ -24,6 +27,9 @@
* - Run at 512kHz, only use HSI for ADC: 360uA (jumpy)
* - Drop to 1.2V Vcore (range 3), enable low-V detector: 375uA (steady) (440uA at 1MHz)
* - Run at 4MHz, drop to low-power run/sleep @ 64kHz for idle: 375uA (also lowered contrast)
* - Sleep display for 'pause': ~240uA
*
* - Flappy bird is going, 2048 next
*/
static volatile bool adc_is_complete = false;
@ -76,86 +82,26 @@ THD_FUNCTION(Thread2, arg)
dogs_init();
const unsigned char testbitmap[] = {
8, 8,
0b00111100,
0b01100110,
0b01000010,
0b01111110,
0b01100110,
0b01000010,
0b11000000,
0b11000000,
};
flapbird_init();
bool sleep = false;
int score = 0;
int t1x = DISP_WIDTH / 2, t1o = 15;
int t2x = DISP_WIDTH, t2o = 49;
int py = DISP_HEIGHT / 2 - 4;
int vy = 0;
int counter = 0;
int mv = readVddmv();
while (1) {
if (button_state & BUTTON_1) {
sleep ^= true;
if (sleep) {
draw_number(DISP_WIDTH - 33, 0,
!(PWR->CSR & PWR_CSR_PVDO) ? readVddmv() : 1);
dogs_flush();
}
dogs_set_sleep(sleep);
}
int dtime = 100;
if (!sleep) {
// Player logic
if (py > 0) {
py += vy;
if (vy > -3)
vy--;
} else {
if (py < 0)
py = 0;
if (score > 0)
score = 0;
}
if (button_state & BUTTON_2) {
vy = 5;
if (py <= 0)
py = 1;
}
// Rendering
dogs_clear();
draw_rect(t1x, 0, 4, t1o - 10);
draw_rect(t1x, t1o + 10, 4, DISP_HEIGHT - t1o + 10);
draw_rect(t2x, 0, 4, t2o - 10);
draw_rect(t2x, t2o + 10, 4, DISP_HEIGHT - t2o + 10);
draw_bitmap(4, py, testbitmap);
draw_number(DISP_WIDTH - 33, DISP_HEIGHT - 8, mv);
draw_number(DISP_WIDTH - 33, DISP_HEIGHT - 16, score);
dogs_flush();
// Game logic
if (++counter == 50) {
counter = 0;
mv = !(PWR->CSR & PWR_CSR_PVDO) ? readVddmv() : 1;
}
if (t1x == 4)
score = (py + 2 > t1o - 10 && py + 6 < t1o + 10) ? score + 1 : 0;
if (t2x == 4)
score = (py + 2 > t2o - 10 && py + 6 < t2o + 10) ? score + 1 : 0;
t1x -= 2;
if (t1x <= -5)
t1x = DISP_WIDTH;
t2x -= 2;
if (t2x <= -5)
t2x = DISP_WIDTH;
dtime = flapbird_loop();
}
chThdSleepS(TIME_MS2I(100) / 64);
chThdSleepS(TIME_MS2I(dtime) / 64);
}
}

Loading…
Cancel
Save