newsss
This commit is contained in:
parent
ac44715755
commit
15e179b4c6
59
du4/snake.c
59
du4/snake.c
@ -1,11 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#define FOOD_COUNT 5
|
#define FOOD_COUNT 5
|
||||||
#define WIDTH 20
|
|
||||||
#define HEIGHT 10
|
|
||||||
|
|
||||||
struct snake {
|
struct snake {
|
||||||
int x;
|
int x;
|
||||||
@ -37,9 +33,6 @@ int is_snake(struct snake* snake, int x, int y);
|
|||||||
void free_snake(struct snake* sn);
|
void free_snake(struct snake* sn);
|
||||||
int step_state(struct state* state);
|
int step_state(struct state* state);
|
||||||
|
|
||||||
void display_game(struct state* state);
|
|
||||||
void start_game(struct state* game);
|
|
||||||
|
|
||||||
struct snake* add_snake(struct snake* snake, int x, int y) {
|
struct snake* add_snake(struct snake* snake, int x, int y) {
|
||||||
struct snake* new_part = (struct snake*)malloc(sizeof(struct snake));
|
struct snake* new_part = (struct snake*)malloc(sizeof(struct snake));
|
||||||
new_part->x = x;
|
new_part->x = x;
|
||||||
@ -134,16 +127,10 @@ int step_state(struct state* state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void display_game(struct state* game) {
|
void display_game(struct state* game) {
|
||||||
system("clear");
|
printf("SNAKE GAME\n");
|
||||||
printf("SNAKE GAME - Simple Version\n");
|
|
||||||
printf("================================\n\n");
|
|
||||||
|
|
||||||
for (int x = 0; x < WIDTH + 2; x++) printf("#");
|
for (int y = 0; y < 10; y++) {
|
||||||
printf("\n");
|
for (int x = 0; x < 20; x++) {
|
||||||
|
|
||||||
for (int y = 0; y < HEIGHT; y++) {
|
|
||||||
printf("#");
|
|
||||||
for (int x = 0; x < WIDTH; x++) {
|
|
||||||
int head = (game->snake->x == x && game->snake->y == y);
|
int head = (game->snake->x == x && game->snake->y == y);
|
||||||
int body = is_snake(game->snake->next, x, y);
|
int body = is_snake(game->snake->next, x, y);
|
||||||
int food = 0;
|
int food = 0;
|
||||||
@ -160,26 +147,22 @@ void display_game(struct state* game) {
|
|||||||
else if (food) printf("F");
|
else if (food) printf("F");
|
||||||
else printf(" ");
|
else printf(" ");
|
||||||
}
|
}
|
||||||
printf("#\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int x = 0; x < WIDTH + 2; x++) printf("#");
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void start_game(struct state* game) {
|
void start_game(struct state* game) {
|
||||||
game->snake = add_snake(NULL, WIDTH / 2, HEIGHT / 2);
|
game->snake = add_snake(NULL, 10, 5);
|
||||||
game->snake = add_snake(game->snake, WIDTH / 2 - 1, HEIGHT / 2);
|
game->snake = add_snake(game->snake, 9, 5);
|
||||||
|
|
||||||
game->sx = 1;
|
game->sx = 1;
|
||||||
game->sy = 0;
|
game->sy = 0;
|
||||||
game->width = WIDTH;
|
game->width = 20;
|
||||||
game->height = HEIGHT;
|
game->height = 10;
|
||||||
|
|
||||||
srand(time(NULL));
|
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||||
game->foodx[i] = rand() % WIDTH;
|
game->foodx[i] = 5 + i * 2;
|
||||||
game->foody[i] = rand() % HEIGHT;
|
game->foody[i] = 3 + i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,31 +170,11 @@ int main(void) {
|
|||||||
struct state game;
|
struct state game;
|
||||||
start_game(&game);
|
start_game(&game);
|
||||||
|
|
||||||
int playing = 1;
|
|
||||||
int tick = 0;
|
|
||||||
|
|
||||||
while (playing) {
|
|
||||||
display_game(&game);
|
display_game(&game);
|
||||||
|
|
||||||
tick++;
|
|
||||||
if (tick >= 3) {
|
|
||||||
tick = 0;
|
|
||||||
int status = step_state(&game);
|
int status = step_state(&game);
|
||||||
|
|
||||||
if (status == END_WALL) {
|
printf("Game status: %d\n", status);
|
||||||
printf("CRASH into wall!\n");
|
|
||||||
playing = 0;
|
|
||||||
} else if (status == END_SNAKE) {
|
|
||||||
printf("CRASH into yourself!\n");
|
|
||||||
playing = 0;
|
|
||||||
} else if (status == END_FOOD) {
|
|
||||||
printf("You ate all food. Good job!\n");
|
|
||||||
playing = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
usleep(80000);
|
|
||||||
}
|
|
||||||
|
|
||||||
free_snake(game.snake);
|
free_snake(game.snake);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user