Initialization
This commit is contained in:
parent
50f4be2f43
commit
cc5cf12df7
134
a2/snake.c
134
a2/snake.c
@ -1,58 +1,110 @@
|
|||||||
#include "snake.h"
|
#include "world.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#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
|
||||||
|
|
||||||
struct snake* add_snake(struct snake* snake,int x,int y){
|
// Start is called one in the beginning
|
||||||
struct snake* head = calloc(1,sizeof(struct snake));
|
void* init_game(){
|
||||||
head->x = x;
|
// Allocate memory for the state
|
||||||
head->y = y;
|
struct state* st = calloc(1,(sizeof(struct state)));
|
||||||
head->next = snake;
|
return st;
|
||||||
return head;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct snake* remove_snake(struct snake* snake) {
|
// The first event
|
||||||
if (snake == NULL) {
|
// Initialize game state
|
||||||
return NULL;
|
// - borders
|
||||||
|
// - snake position
|
||||||
|
// - food position
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (snake->next == NULL) {
|
int h = world->height;
|
||||||
free(snake);
|
int w = world->width;
|
||||||
return NULL;
|
for (int i = 0; i < 5; i++){
|
||||||
|
st->foodx[i] = rand() % w;
|
||||||
|
st->foody[i] = rand() % h;
|
||||||
}
|
}
|
||||||
|
// Initial game vector
|
||||||
struct snake* current = snake;
|
st->sx = 1;
|
||||||
while (current->next->next != NULL) {
|
st->sy = 0;
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(current->next);
|
|
||||||
current->next = NULL;
|
|
||||||
|
|
||||||
return snake;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_snake(struct snake* sn) {
|
// Step is called in a loop once in interval.
|
||||||
struct snake* current = sn;
|
// It should modify the state and draw it.
|
||||||
struct snake* next;
|
int world_event(struct event* w,void* game){
|
||||||
|
// Get state pointer
|
||||||
while (current!= NULL) {
|
struct state* st = game;
|
||||||
next = current->next;
|
if (w->type == EVENT_START){
|
||||||
free(current);
|
// Called on beginning
|
||||||
current = next;
|
init_snake(w,st);
|
||||||
}
|
}
|
||||||
}
|
// Called on key press
|
||||||
|
else if (w->type == EVENT_KEY){
|
||||||
int is_snake(struct snake* snake,int x,int y){
|
int key = w->key;
|
||||||
while (snake != NULL) {
|
// Modifies vector of snake movement
|
||||||
if (snake->x == x && snake->y == y) {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Called on esc key
|
||||||
|
else if (w->type == EVENT_ESC){
|
||||||
|
// Non zero means finish the loop and stop the game.
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
snake = snake->next;
|
// Called on interval timeout
|
||||||
|
else if (w->type == EVENT_TIMEOUT){
|
||||||
|
clear_screen();
|
||||||
|
// Copy screen size
|
||||||
|
st->width = w->width;
|
||||||
|
st->height = w->height;
|
||||||
|
// Change game state
|
||||||
|
int r = step_state(st);
|
||||||
|
char ms[200];
|
||||||
|
sprintf(ms,"r %d\n",r);
|
||||||
|
set_message(ms,9,9);
|
||||||
|
// Draw snake
|
||||||
|
struct snake* sn = st->snake;
|
||||||
|
while (sn != NULL){
|
||||||
|
set_cell('x',sn->x,sn->y);
|
||||||
|
sn = sn->next;
|
||||||
|
}
|
||||||
|
// Draw food
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Stop the game
|
||||||
|
if (r){
|
||||||
|
char message[] = "Koniec";
|
||||||
|
set_message(message,10,10);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int step_state(struct state* st){
|
|
||||||
int nx = (st->snake->x + st->sx);
|
int main(int argc, char** argv){
|
||||||
int ny = (st->snake->y + st->sy);
|
start_world(init_game,world_event,free);
|
||||||
return END_CONTINUE;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user