c语言写贪吃蛇小游戏代码
时间: 2023-12-26 19:25:15 浏览: 124
贪吃蛇是一种经典的小游戏,以下是C语言实现贪吃蛇小游戏的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 30
#define HEIGHT 20
#define SNAKE_MAX_LENGTH 100
int score = 0;
int speed = 200;
typedef struct _COORD {
int x;
int y;
} COORD;
typedef struct _SNAKE {
COORD body[SNAKE_MAX_LENGTH];
int length;
int direction;
} SNAKE;
typedef struct _FOOD {
COORD position;
} FOOD;
void gotoxy(int x, int y) {
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void drawBorder() {
int i;
for (i = 0; i < WIDTH + 2; i++) {
printf("#");
}
printf("\n");
for (i = 0; i < HEIGHT; i++) {
printf("#");
gotoxy(WIDTH + 1, i + 1);
printf("#");
}
printf("\n");
for (i = 0; i < WIDTH + 2; i++) {
printf("#");
}
printf("\n");
}
void initSnake(SNAKE *snake) {
snake->length = 3;
snake->direction = 'd';
snake->body[0].x = 5;
snake->body[0].y = 3;
snake->body[1].x = 4;
snake->body[1].y = 3;
snake->body[2].x = 3;
snake->body[2].y = 3;
}
void drawSnake(SNAKE *snake) {
int i;
for (i = 0; i < snake->length; i++) {
gotoxy(snake->body[i].x, snake->body[i].y);
printf("*");
}
}
void moveSnake(SNAKE *snake) {
int i;
for (i = snake->length - 1; i > 0; i--) {
snake->body[i].x = snake->body[i - 1].x;
snake->body[i].y = snake->body[i - 1].y;
}
switch (snake->direction) {
case 'w':
snake->body[0].y--;
break; case 's':
snake->body[0].y++;
break;
case 'a':
snake->body[0].x--;
break;
case 'd':
snake->body[0].x++;
break;
}
}
void generateFood(FOOD *food, SNAKE *snake) {
int x, y;
do {
x = rand() % WIDTH + 1;
y = rand() % HEIGHT + 1;
} while (isSnake(snake, x, y));
food->position.x = x;
food->position.y = y;
}
void drawFood(FOOD *food) {
gotoxy(food->position.x, food->position.y);
printf("@");
}
int isSnake(SNAKE *snake, int x, int y) {
int i;
for (i = 0; i < snake->length; i++) {
if (snake->body[i].x == x && snake->body[i].y == y) {
return 1;
}
}
return 0;
}
int isHitWall(SNAKE *snake) {
if (snake->body[0].x == 0 || snake->body[0].x == WIDTH + 1 || snake->body[0].y == 0 || snake->body[0].y == HEIGHT + 1) {
return 1;
}
return 0;
}
int isEatFood(SNAKE *snake, FOOD *food) {
if (snake->body[0].x == food->position.x && snake->body[0].y == food->position.y) {
return 1;
}
return 0;
}
void updateScore() {
gotoxy(WIDTH + 5, 5);
printf("Score: %d", score);
}
void updateSpeed() {
gotoxy(WIDTH + 5, 6);
printf("Speed: %d", speed);
}
void gameOver() {
system("cls");
printf("Game Over!\n");
printf("Your score is %d\n", score);
exit(0);
}
int main() {
SNAKE snake;
FOOD food;
srand(time(NULL));
initSnake(&snake);
generateFood(&food, &snake);
drawBorder();
drawSnake(&snake);
drawFood(&food);
while (1) {
if (_kbhit()) {
char ch = _getch();
switch (ch) {
case 'w':
case 's':
case 'a':
case 'd':
if (abs(ch - snake.direction) != 2) {
snake.direction = ch;
}
break;
case 'q':
gameOver();
break;
}
}
moveSnake(&snake);
if (isHitWall(&snake) || isSnake(&snake, snake.body[0].x, snake.body[0].y)) {
gameOver();
}
if (isEatFood(&snake, &food)) {
snake.length++;
score += 10;
speed -= 10;
if (speed < 50) {
speed = 50;
}
generateFood(&food, &snake);
updateScore();
updateSpeed();
}
system("cls");
drawBorder();
drawSnake(&snake);
drawFood(&food);
Sleep(speed);
}
return 0;
}
```
阅读全文