生成一个基于c++的贪吃蛇游戏代码
时间: 2024-05-04 22:16:02 浏览: 75
贪吃蛇游戏代码(C++)
好的,以下是一个基于C语言的贪吃蛇游戏代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
#define ROWS 20
#define COLS 30
#define MAX_LENGTH (ROWS * COLS)
typedef struct node {
int row;
int col;
struct node* next;
}node;
node* head;
node* food;
int snakeLength;
int score;
int speed;
char map[ROWS][COLS];
void setCursorPosition(int x, int y) //设置光标位置函数
{
HANDLE hOutput;
COORD loc;
loc.X = x;
loc.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, loc);
}
void drawMap() //绘制游戏地图
{
int i, j;
setCursorPosition(0, 0);
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (i == 0 || i == ROWS - 1 || j == 0 || j == COLS - 1)
map[i][j] = '#';
else
map[i][j] = ' ';
}
}
for (node* p = head; p; p = p->next) {
map[p->row][p->col] = '*';
}
map[food->row][food->col] = '@';
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
printf("Score: %d\n", score);
printf("Length: %d\n", snakeLength);
printf("Speed: %d\n", 550 - speed * 50);
}
int isHitWall(node* pHead) //判断是否撞墙
{
return pHead->row == 0 || pHead->row == ROWS - 1 || pHead->col == 0 || pHead->col == COLS - 1;
}
int isHitSelf(node* pHead, node* pFood) //判断是否撞到自己
{
node* p = pHead->next;
while (p != NULL) {
if (p->row == pHead->row && p->col == pHead->col) {
return 1;
}
p = p->next;
}
if (pHead->row == pFood->row && pHead->col == pFood->col) {
return 1;
}
return 0;
}
void generateFood() //生成食物
{
node* p = head;
while (p) {
int row = rand() % (ROWS - 2) + 1;
int col = rand() % (COLS - 2) + 1;
if (map[row][col] == ' ') {
food->row = row;
food->col = col;
break;
}
p = p->next;
}
}
void changeSpeed() //改变游戏速度
{
if (snakeLength >= 5 && snakeLength < 10) {
speed = 1;
}
else if (snakeLength >= 10 && snakeLength < 15) {
speed = 2;
}
else if (snakeLength >= 15 && snakeLength < 20) {
speed = 3;
}
else if (snakeLength >= 20 && snakeLength < 25) {
speed = 4;
}
else if (snakeLength >= 25) {
speed = 5;
}
}
int main()
{
int direction = 0; //0 - up, 1 - down, 2 - left, 3 - right
head = (node*)malloc(sizeof(node));
head->row = ROWS / 2;
head->col = COLS / 2;
head->next = NULL;
food = (node*)malloc(sizeof(node));
score = 0;
speed = 0;
snakeLength = 1;
generateFood();
drawMap();
while (1) {
if (_kbhit()) {
int ch = _getch();
if (ch == 27) { //按ESC键退出游戏
break;
}
else if (ch == 'w' || ch == 'W') {
direction = 0;
}
else if (ch == 's' || ch == 'S') {
direction = 1;
}
else if (ch == 'a' || ch == 'A') {
direction = 2;
}
else if (ch == 'd' || ch == 'D') {
direction = 3;
}
}
Sleep(550 - speed * 50); //根据游戏速度控制Sleep时间,使得游戏速度不会过快或过慢
node* tail = head;
while (tail->next) {
tail = tail->next;
}
if (tail->row == food->row && tail->col == food->col) { //吃到食物,生成新的食物
node* newTail = (node*)malloc(sizeof(node));
newTail->row = tail->row;
newTail->col = tail->col;
tail->next = newTail;
newTail->next = NULL;
snakeLength++;
score = score + 10;
generateFood();
changeSpeed();
}
else { //没有吃到食物,尾部移动即可
node* p = head;
while (p->next != tail) {
p = p->next;
}
map[tail->row][tail->col] = ' ';
p->next = NULL;
tail->row = head->row;
tail->col = head->col;
tail->next = head->next;
head->next = tail;
head->row = head->row + (direction == 0 ? -1 : (direction == 1 ? 1 : 0));
head->col = head->col + (direction == 2 ? -1 : (direction == 3 ? 1 : 0));
if (isHitWall(head) || isHitSelf(head, food)) { //游戏结束
printf("GAME OVER!\n");
break;
}
}
drawMap();
}
return 0;
}
```
注意:此代码可能存在一些漏洞和问题,仅供参考。
阅读全文