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