aboutsummaryrefslogtreecommitdiffstats
path: root/flapbird.c
blob: f2dd2cf7452ce6c87306d3217a305daf5a24588d (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
#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;
}