请用c语言做一个含闯关的贪吃蛇游戏
时间: 2024-03-11 12:50:45 浏览: 16
好的,以下是一个简单的含闯关的贪吃蛇游戏的 C 语言代码,可以在控制台中运行:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define WIDTH 60
#define HEIGHT 20
#define MAX_LENGTH 100
#define SPEED 100
int score = 0;
int level = 1;
typedef struct Snake{
int x;
int y;
struct Snake* next;
}Snake;
Snake* head;
Snake* tail;
Snake* food;
char map[HEIGHT][WIDTH];
void init();
void draw();
void createFood();
void update();
void gameOver();
void clearSnake();
void clearFood();
void clearScreen();
void gotoxy(int x, int y);
int main()
{
init();
while(1){
draw();
update();
Sleep(SPEED);
}
return 0;
}
void init(){
srand(time(NULL));
head = (Snake*)malloc(sizeof(Snake));
head->x = WIDTH / 2;
head->y = HEIGHT / 2;
head->next = NULL;
tail = head;
createFood();
for(int i = 0; i < HEIGHT; i++){
for(int j = 0; j < WIDTH; j++){
if(i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1){
map[i][j] = '#';
}
else{
map[i][j] = ' ';
}
}
}
}
void draw(){
clearScreen();
for(int i = 0; i < HEIGHT; i++){
for(int j = 0; j < WIDTH; j++){
printf("%c", map[i][j]);
}
printf("\n");
}
printf("Score: %d\n", score);
printf("Level: %d\n", level);
}
void createFood(){
food = (Snake*)malloc(sizeof(Snake));
while(1){
food->x = rand() % (WIDTH - 2) + 1;
food->y = rand() % (HEIGHT - 2) + 1;
Snake* p = head;
while(p != NULL){
if(p->x == food->x && p->y == food->y){
gotoxy(food->x, food->y);
printf(" ");
gotoxy(food->x, food->y);
continue;
}
p = p->next;
}
break;
}
food->next = NULL;
gotoxy(food->x, food->y);
printf("*");
}
void update(){
// get the direction from keyboard
int dx = 0, dy = 0;
if(kbhit()){
char c = getch();
switch(c){
case 'w':
dx = -1;
dy = 0;
break;
case 's':
dx = 1;
dy = 0;
break;
case 'a':
dx = 0;
dy = -1;
break;
case 'd':
dx = 0;
dy = 1;
break;
}
}
// update the snake's position
int new_x = head->x + dx;
int new_y = head->y + dy;
if(map[new_x][new_y] == '#' || map[new_x][new_y] == 'o'){
gameOver();
return;
}
if(new_x == food->x && new_y == food->y){
score++;
if(score % 5 == 0){
level++;
SPEED -= 10;
}
tail->next = (Snake*)malloc(sizeof(Snake));
tail->next->x = tail->x;
tail->next->y = tail->y;
tail->next->next = NULL;
tail = tail->next;
createFood();
}
else{
gotoxy(tail->x, tail->y);
printf(" ");
tail->x = tail->next->x;
tail->y = tail->next->y;
Snake* p = head;
while(p->next->next != NULL){
p = p->next;
}
free(p->next);
p->next = NULL;
}
head->x = new_x;
head->y = new_y;
map[head->x][head->y] = 'o';
gotoxy(head->x, head->y);
printf("o");
}
void gameOver(){
clearScreen();
printf("Game Over!\n");
printf("Score: %d\n", score);
printf("Level: %d\n", level);
printf("Press any key to continue...\n");
clearSnake();
clearFood();
score = 0;
level = 1;
head = (Snake*)malloc(sizeof(Snake));
head->x = WIDTH / 2;
head->y = HEIGHT / 2;
head->next = NULL;
tail = head;
createFood();
for(int i = 0; i < HEIGHT; i++){
for(int j = 0; j < WIDTH; j++){
if(i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1){
map[i][j] = '#';
}
else{
map[i][j] = ' ';
}
}
}
getch();
}
void clearSnake(){
Snake* p = head;
while(p != NULL){
Snake* q = p;
p = p->next;
free(q);
}
}
void clearFood(){
gotoxy(food->x, food->y);
printf(" ");
free(food);
}
void clearScreen(){
system("cls");
}
void gotoxy(int x, int y){
COORD pos;
pos.X = y;
pos.Y = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
```
在这个游戏中,通过键盘控制贪吃蛇的方向,吃到食物可以得分,如果撞到墙壁或自己的身体,则游戏结束。在每得到5分时,游戏难度会提高一级,速度会加快。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)