aboutsummaryrefslogtreecommitdiffstats
path: root/source/flapbird.c
blob: 800e89280dfdd9a9b9d88d68f498071cbfd9fa15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "buttons.h"
#include "dogs.h"
#include "hal.h"

#include <stdint.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;

static uint32_t next = 31415926;

int rand()
{
    uint32_t tr = RTC->TR;
    uint32_t dr = RTC->DR;
    next = next * (0x197244 + dr) + tr;
    return (unsigned int)(next / 65536) % 32768;
}

#define NEW_BAR()	(rand() % 50 + 5)

void flapbird_init()
{
    score = 0;
    t1x = DISP_WIDTH / 2, t1o = NEW_BAR();
    t2x = DISP_WIDTH, t2o = NEW_BAR();
    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;
	t1o = NEW_BAR();
    }

    t2x -= 2;
    if (t2x <= -5) {
        t2x = DISP_WIDTH;
	t2o = NEW_BAR();
    }

    return 100;
}