Загрузить файлы в «a3»

This commit is contained in:
Bohdana Marchenko 2025-05-02 06:38:37 +00:00
parent 40d250a832
commit 9653efcd07
2 changed files with 31 additions and 16 deletions

View File

@ -25,8 +25,11 @@ void init_snake(struct event* world, struct state* st){
int cx = st->width / 2; int cx = st->width / 2;
int cy = st->height / 2; int cy = st->height / 2;
st->snake = NULL; st->snake = NULL;
// Place snake horizontally, centered, away from borders
int start_x = cx - 2;
int y = cy;
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
st->snake = add_snake(st->snake, cx, cy + i); // vertical, head at top st->snake = add_snake(st->snake, start_x + i, y);
} }
int placed = 0; int placed = 0;
@ -40,7 +43,7 @@ void init_snake(struct event* world, struct state* st){
} }
} }
// Initial game vector // Initial game vector: right
st->sx = 1; st->sx = 1;
st->sy = 0; st->sy = 0;
} }
@ -48,31 +51,45 @@ void init_snake(struct event* world, struct state* st){
// Step is called in a loop once in interval. // Step is called in a loop once in interval.
// It should modify the state and draw it. // 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 static int game_over = 0;
struct state* st = game; struct state* st = game;
if (w->type == EVENT_START){ if (w->type == EVENT_START){
// Called on beginning game_over = 0;
init_snake(w,st); init_snake(w,st);
} }
if (game_over) {
// Ignore all events except EVENT_START and EVENT_ESC
if (w->type != EVENT_ESC && w->type != EVENT_START) {
return 0;
}
}
// Called on key press // Called on key press
else if (w->type == EVENT_KEY){ else if (w->type == EVENT_KEY){
int key = w->key; int key = w->key;
// Modifies vector of snake movement // Modifies vector of snake movement
if (key == KEY_RIGHT){ if (key == KEY_RIGHT){
st->sx = 1; if (!(st->sx == -1 && st->sy == 0)) { // Prevent reverse
st->sy = 0; st->sx = 1;
st->sy = 0;
}
} }
else if (key == KEY_LEFT){ else if (key == KEY_LEFT){
st->sx = -1; if (!(st->sx == 1 && st->sy == 0)) { // Prevent reverse
st->sy = 0; st->sx = -1;
st->sy = 0;
}
} }
else if (key == KEY_DOWN){ else if (key == KEY_DOWN){
st->sx = 0; if (!(st->sx == 0 && st->sy == -1)) { // Prevent reverse
st->sy = 1; st->sx = 0;
st->sy = 1;
}
} }
else if (key == KEY_UP){ else if (key == KEY_UP){
st->sx = 0; if (!(st->sx == 0 && st->sy == 1)) { // Prevent reverse
st->sy = -1; st->sx = 0;
st->sy = -1;
}
} }
} }
// Called on esc key // Called on esc key
@ -88,9 +105,6 @@ int world_event(struct event* w,void* game){
st->height = w->height; st->height = w->height;
// Change game state // Change game state
int r = step_state(st); int r = step_state(st);
char ms[200];
sprintf(ms,"r %d\n",r);
set_message(ms,9,9);
// Draw snake // Draw snake
struct snake* sn = st->snake; struct snake* sn = st->snake;
while (sn != NULL){ while (sn != NULL){
@ -107,6 +121,7 @@ int world_event(struct event* w,void* game){
if (r){ if (r){
char message[] = "Koniec"; char message[] = "Koniec";
set_message(message,10,10); set_message(message,10,10);
game_over = 1;
} }
} }
return 0; return 0;

View File

@ -2,7 +2,7 @@
#define snake_h_INCLUDED #define snake_h_INCLUDED
// Number of food items on the plane // Number of food items on the plane
#define FOOD_COUNT 5 #define FOOD_COUNT 20
/** /**
* One part of the snake; * One part of the snake;