做一个c++的贪吃蛇游戏
时间: 2023-11-21 11:06:32 浏览: 28
好的,我可以为您提供一些贪吃蛇游戏的代码示例。请注意,这是一个简单的示例,您可以根据自己的需要进行修改和扩展。
```
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#define ROW 20
#define COL 40
typedef struct _snake
{
int x;
int y;
struct _snake *next;
} Snake;
Snake *head = NULL;
int direction = 0;
int food_x, food_y;
int score = 0;
int speed = 100;
void gotoxy(int x, int y)
{
COORD pos = {x, y};
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsole, pos);
}
void init()
{
srand((unsigned)time(NULL));
head = (Snake *)malloc(sizeof(Snake));
head->x = COL / 2;
head->y = ROW / 2;
head->next = NULL;
direction = 2;
food_x = rand() % COL;
food_y = rand() % ROW;
}
void draw_map()
{
int i, j;
for (i = 0; i <= ROW; i++)
{
gotoxy(0, i);
printf("|");
gotoxy(COL, i);
printf("|");
}
for (j = 0; j <= COL; j++)
{
gotoxy(j, 0);
printf("-");
gotoxy(j, ROW);
printf("-");
}
}
void draw_snake()
{
Snake *p = head;
while (p != NULL)
{
gotoxy(p->x, p->y);
if (p == head)
{
printf("@");
}
else
{
printf("*");
}
p = p->next;
}
}
void draw_food()
{
gotoxy(food_x, food_y);
printf("#");
}
void update_snake()
{
Snake *p = head;
int x = p->x;
int y = p->y;
while (p->next != NULL)
{
p = p->next;
}
gotoxy(p->x, p->y);
printf(" ");
free(p);
p = NULL;
Snake *new_head = (Snake *)malloc(sizeof(Snake));
switch (direction)
{
case 1:
new_head->x = x;
new_head->y = y - 1;
break;
case 2:
new_head->x = x + 1;
new_head->y = y;
break;
case 3:
new_head->x = x;
new_head->y = y + 1;
break;
case 4:
new_head->x = x - 1;
new_head->y = y;
break;
default:
break;
}
new_head->next = head;
head = new_head;
if (head->x == food_x && head->y == food_y)
{
score++;
speed -= 5;
Snake *new_food = NULL;
do
{
new_food = (Snake *)malloc(sizeof(Snake));
new_food->x = rand() % COL;
new_food->y = rand() % ROW;
new_food->next = NULL;
} while (is_snake(new_food));
food_x = new_food->x;
food_y = new_food->y;
free(new_food);
new_food = NULL;
}
}
int is_snake(Snake *p)
{
Snake *q = head;
while (q != NULL)
{
if (q->x == p->x && q->y == p->y)
{
return 1;
}
q = q->next;
}
return 0;
}
void game_over()
{
Snake *p = head;
while (p != NULL)
{
Snake *q = p->next;
free(p);
p = q;
}
head = NULL;
system("cls");
printf("Game Over! Score: %d\n", score);
system("pause");
exit(0);
}
int main()
{
init();
while (1)
{
draw_map();
draw_food();
draw_snake();
Sleep(speed);
if (_kbhit())
{
int ch = _getch();
switch (ch)
{
case 'w':
if (direction != 3)
{
direction = 1;
}
break;
case 'd':
if (direction != 4)
{
direction = 2;
}
break;
case 's':
if (direction != 1)
{
direction = 3;
}
break;
case 'a':
if (direction != 2)
{
direction = 4;
}
break;
default:
break;
}
}
update_snake();
if (head->x < 1 || head->x > COL - 1 || head->y < 1 || head->y > ROW - 1 || is_snake(head->next))
{
game_over();
}
system("cls");
}
return 0;
}
```
这段代码使用了 Windows API 中的 `SetConsoleCursorPosition` 函数来定位光标位置,使用了 `Sleep` 函数来控制游戏速度,使用了 `_kbhit` 和 `_getch` 函数来获取键盘输入。您可以根据自己的需要进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)