snake/main.c

111 lines
2.7 KiB
C
Raw Normal View History

2020-04-23 15:39:46 +00:00
#include "world.h"
#include <stdlib.h>
2021-04-20 06:54:59 +00:00
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include "snake.h"
// This file contains functions for drawing the the game and changing the state
// Start is called one in the beginning
void* init_game(){
// Allocate memory for the state
struct state* st = calloc(1,(sizeof(struct state)));
return st;
}
2021-04-20 07:00:42 +00:00
// The first event
// Initialize game state
// - borders
// - snake position
// - food position
2021-04-20 06:54:59 +00:00
void init_snake(struct event* world, struct state* st){
int cy = world->height/2;
int cx = world->width/2 - 5;
for (int i = 0; i < 5; i++){
st->snake = add_snake(st->snake,cx + i ,cy);
}
int h = world->height;
int w = world->width;
for (int i = 0; i < 5; i++){
st->foodx[i] = rand() % w;
st->foody[i] = rand() % h;
}
2021-04-21 13:03:19 +00:00
// Initial game vector
st->sx = 1;
st->sy = 0;
2021-04-20 06:54:59 +00:00
}
// Step is called in a loop once in interval.
// It should modify the state and draw it.
int world_event(struct event* w,void* game){
// Get state pointer
struct state* st = game;
if (w->type == EVENT_START){
2021-04-20 07:00:42 +00:00
// Called on beginning
2021-04-20 06:54:59 +00:00
init_snake(w,st);
}
2021-04-20 07:00:42 +00:00
// Called on key press
2021-04-20 06:54:59 +00:00
else if (w->type == EVENT_KEY){
int key = w->key;
2021-04-20 07:00:42 +00:00
// Modifies vector of snake movement
2021-04-20 06:54:59 +00:00
if (key == KEY_RIGHT){
st->sx = 1;
st->sy = 0;
}
else if (key == KEY_LEFT){
st->sx = -1;
st->sy = 0;
}
else if (key == KEY_DOWN){
st->sx = 0;
st->sy = 1;
}
else if (key == KEY_UP){
st->sx = 0;
st->sy = -1;
}
}
2021-04-20 07:00:42 +00:00
// Called on esc key
2021-04-20 06:54:59 +00:00
else if (w->type == EVENT_ESC){
// Non zero means finish the loop and stop the game.
return 1;
}
2021-04-20 07:00:42 +00:00
// Called on interval timeout
2021-04-20 06:54:59 +00:00
else if (w->type == EVENT_TIMEOUT){
2021-04-21 13:03:19 +00:00
clear_screen();
2021-04-20 07:00:42 +00:00
// Copy screen size
2021-04-20 06:54:59 +00:00
st->width = w->width;
st->height = w->height;
2021-04-20 07:00:42 +00:00
// Change game state
2021-04-20 06:54:59 +00:00
int r = step_state(st);
2021-04-21 13:03:19 +00:00
char ms[200];
sprintf(ms,"r %d\n",r);
set_message(ms,9,9);
2021-04-20 06:54:59 +00:00
// Draw snake
struct snake* sn = st->snake;
while (sn != NULL){
set_cell('x',sn->x,sn->y);
sn = sn->next;
}
2021-04-20 07:00:42 +00:00
// Draw food
2021-04-20 06:54:59 +00:00
for (int i = 0 ; i < FOOD_COUNT; i++){
if (st->foodx[i] >= 0 && st->foody[i] >= 0){
set_cell('*',st->foodx[i],st->foody[i]);
}
}
2021-04-20 07:00:42 +00:00
// Stop the game
2021-04-20 06:54:59 +00:00
if (r){
char message[] = "Koniec";
2021-04-21 13:03:19 +00:00
set_message(message,10,10);
2021-04-20 06:54:59 +00:00
}
}
return 0;
}
2020-04-23 15:39:46 +00:00
int main(int argc, char** argv){
2021-04-20 06:54:59 +00:00
start_world(init_game,world_event,free);
2020-04-23 15:39:46 +00:00
return 0;
}