moved code from header to source files
This commit is contained in:
@@ -13,16 +13,16 @@
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
i32 x;
|
||||
i32 y;
|
||||
};
|
||||
int64_t xy;
|
||||
i64 xy;
|
||||
} point;
|
||||
|
||||
static struct {
|
||||
int state; // game state (NEW RUN PAUSE OVER)
|
||||
int64_t tick; // updates every 10 ms
|
||||
int len; // snake length
|
||||
i32 state; // game state (NEW RUN PAUSE OVER)
|
||||
i64 tick; // updates every 10 ms
|
||||
i32 len; // snake length
|
||||
point body[200]; // snake body
|
||||
point food; // food position
|
||||
point look; // active direction
|
||||
@@ -38,7 +38,7 @@ static void start(void) {
|
||||
|
||||
static void game(void) {
|
||||
// update game state about every 10 ms
|
||||
int64_t tick = time_us() / 100000;
|
||||
i64 tick = tim_time_usec() / 100000;
|
||||
if (snek.tick != tick) {
|
||||
snek.tick = tick;
|
||||
// move one unit
|
||||
@@ -47,7 +47,7 @@ static void game(void) {
|
||||
snek.body[0].y = snek.body[1].y + snek.look.y;
|
||||
// self crash
|
||||
bool crash = false;
|
||||
for (int i = 1; i < snek.len; i++) {
|
||||
for (i32 i = 1; i < snek.len; i++) {
|
||||
crash |= snek.body[0].xy == snek.body[i].xy;
|
||||
}
|
||||
// border crash
|
||||
@@ -56,7 +56,7 @@ static void game(void) {
|
||||
snek.state = crash ? OVER : snek.state;
|
||||
// food
|
||||
if (snek.food.xy == snek.body[0].xy) {
|
||||
snek.len = MIN(snek.len + 2, (int)ARRAY_SIZE(snek.body));
|
||||
snek.len = MIN(snek.len + 2, (i32)ARRAY_SIZE(snek.body));
|
||||
snek.food.x = rand() % (tim.w / 2 - 2) + 1;
|
||||
snek.food.y = rand() % (tim.h - 2) + 1;
|
||||
}
|
||||
@@ -65,22 +65,22 @@ static void game(void) {
|
||||
// draw
|
||||
if (tim.event.type == TimEvent_Draw) {
|
||||
// food
|
||||
draw_chr(cell(" ", 0, 0xc5), snek.food.x * 2 + 0, snek.food.y);
|
||||
draw_chr(cell(" ", 0, 0xc5), snek.food.x * 2 + 1, snek.food.y);
|
||||
tim_draw_chr(tim_cell(" ", 0, 0xc5), snek.food.x * 2 + 0, snek.food.y);
|
||||
tim_draw_chr(tim_cell(" ", 0, 0xc5), snek.food.x * 2 + 1, snek.food.y);
|
||||
// snek
|
||||
TimCell_t s = cell(" ", 0, 0);
|
||||
for (int i = 0; i < snek.len; i++) {
|
||||
TimCell s = tim_cell(" ", 0, 0);
|
||||
for (i32 i = 0; i < snek.len; i++) {
|
||||
s.bg = (i / 2) % 2 ? 0xe3 : 0xea;
|
||||
int x = snek.body[i].x * 2;
|
||||
int y = snek.body[i].y;
|
||||
draw_chr(s, x + 0, y);
|
||||
draw_chr(s, x + 1, y);
|
||||
i32 x = snek.body[i].x * 2;
|
||||
i32 y = snek.body[i].y;
|
||||
tim_draw_chr(s, x + 0, y);
|
||||
tim_draw_chr(s, x + 1, y);
|
||||
}
|
||||
}
|
||||
|
||||
// user input
|
||||
if (tim.event.type == KEY_EVENT) {
|
||||
int key = tim.event.key;
|
||||
i32 key = tim.event.key;
|
||||
if ((key == TimKey_Right || key == 'd') && snek.look.x != -1) {
|
||||
snek.look = (point){{1, 0}};
|
||||
} else if ((key == TimKey_Left || key == 'a') && snek.look.x != 1) {
|
||||
@@ -90,31 +90,34 @@ static void game(void) {
|
||||
} else if ((key == TimKey_Up || key == 'w') && snek.look.y != 1) {
|
||||
snek.look = (point){{0, -1}};
|
||||
}
|
||||
else if (key == 'q' || key == TimKey_Escape){
|
||||
snek.state = PAUSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void menu(void) {
|
||||
scope(A, A, 20, 13) {
|
||||
tim_scope(A, A, 20, 13) {
|
||||
char* lbl = snek.state == OVER ? "GAME OVER" : "SNEK - THE GAME";
|
||||
char* btn = snek.state == PAUSE ? "Resume" : "Play";
|
||||
label(lbl, A, 0, A, A, BTN);
|
||||
if (button(btn, A, 2, 20, 5, BTN) || is_key_press(TimKey_Enter)) {
|
||||
if (button(btn, A, 2, 20, 5, BTN) || tim_is_key_press(TimKey_Enter)) {
|
||||
if (snek.state != PAUSE) {
|
||||
start();
|
||||
}
|
||||
snek.state = RUN;
|
||||
}
|
||||
if (button("Exit", A, 8, 20, 5, BTN) || is_key_press(TimKey_Escape)) {
|
||||
if (button("Exit", A, 8, 20, 5, BTN) || tim_is_key_press('q') || tim_is_key_press(TimKey_Escape)) {
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
i32 main(void) {
|
||||
// draw every 10 ms
|
||||
while (tim_run(10)) {
|
||||
TimCell_t bg = cell(" ", 0, BG);
|
||||
draw_lot(bg, 0, 0, tim.w, tim.h);
|
||||
while (tim_run(60)) {
|
||||
TimCell bg = tim_cell(" ", 0, BG);
|
||||
tim_draw_lot(bg, 0, 0, tim.w, tim.h);
|
||||
|
||||
if (snek.state == RUN) {
|
||||
game();
|
||||
@@ -122,8 +125,5 @@ int main(void) {
|
||||
menu();
|
||||
}
|
||||
|
||||
if (is_key_press(TimKey_Escape)) {
|
||||
snek.state = PAUSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user