summaryrefslogtreecommitdiff
path: root/src/c/shell/game.c
blob: e7c5d1e4ad68a324b8a45db81d75c1189e156acb (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<source/shell/game.h>
#include<types.h>
#include<source/vga.h>
#include<source/tty.h>
#include<source/timer.h>
#include<source/stdio.h>

extern uint16_t* terminal_buffer;
extern uint8_t terminal_color;
extern uint8_t process_id;

#define N 1000

uint16_t x;
uint16_t y;
uint16_t rep[N];

uint32_t game_time;
uint32_t game_tick;
uint8_t gameover;
uint8_t duzina;
uint8_t smer;

uint16_t jabuka;
uint16_t x_jabuke;
uint16_t y_jabuke;

void game_keyboard_handler(uint16_t keycode)
{
    switch(keycode)
    {
    //  space();
        case 0x39:
            process_id = PROCESS_TTY_ID;
            clear();
            prompt();
            break;
    //  keyup();
        case 72:
            smer = 1;
            break;
    //  keydown();
        case 80:
            smer = 2;
            break;
    //  keyleft();
        case 75:
            smer = 3;
            break;
    //  keyright();
        case 77:
            smer = 4;
            break;
    }
}

void game_init(void)
{
    x=VGA_WIDTH/2;
    y=VGA_HEIGHT/2;
    game_time=0;
    game_tick=0;
    duzina=1;
    gameover=0;
    smer=0;

    x_jabuke=5;
    y_jabuke=5;

    process_id = PROCESS_GAME_ID;
    clear();
}

void game_timer_handler()
{
    game_tick++;
    if (game_tick == TICKS_PER_SECOND/8 && !gameover)
    {
        switch(smer)
        {
            case 1:
                if(y>0) y--;
                else y=VGA_HEIGHT-1;
                break;
            case 2:
                if(y<VGA_HEIGHT) y++;
                else y=0;
                break;
            case 3:
                if(x>0) x--;
                else x=VGA_WIDTH-1;
                break;
            case 4:
                if(x<VGA_WIDTH) x++;
                else x=0;
                break;
            default:
                break;
        }
        game_tick=0;
        game_time+=1;

        for(uint8_t i = duzina; i > 0; i--) rep[i]=rep[i-1];
        rep[0]=(uint16_t)(y*80+x);

        if (rep[0] == y_jabuke*80+x_jabuke)
        {
            duzina++;

            uint8_t ind;
            do {
                ind = 1;
                x_jabuke = (uint16_t)((game_time*game_time)%80);
                y_jabuke = (uint16_t)((game_time*game_time)%25);
                jabuka = (uint16_t)(y_jabuke*80+x_jabuke);
                for(uint8_t i = 0; i < duzina; i++) if(rep[i] == jabuka) ind=0;
                game_time++;
            } while(!ind);

            terminal_buffer[jabuka]=(uint16_t)(terminal_color<<8|'J');
        }
        terminal_buffer[rep[duzina]]=(uint16_t)(terminal_color<<8|' ');
        terminal_buffer[rep[0]]=(uint16_t)(terminal_color<<8|'#');

        set_color(VGA_COLOR_RED,VGA_COLOR_BLACK);
        terminal_buffer[y_jabuke*80+x_jabuke]=(uint16_t)(terminal_color<<8|'J');
        set_color(VGA_COLOR_LIGHT_GREY,VGA_COLOR_BLACK);

        for(uint8_t i = 1; i < duzina; i++) if(rep[0] == rep[i])
        {
            printf("GAME OVER\nPress <Space> to return to shell\n");
            gameover=1;
        }
    }
}