基于C++的AI贪吃蛇

访问【WRITE-BUG数字空间】_[内附完整源码和文档]

用C++做了个有AI功能的贪吃蛇小游戏,希望大家enjoy it.

总体概况
开发环境:VIsual Studio 2017

开发语言:C++ 和 少许Windows API

运行环境:Windows 10

​01 初始化工作-游戏设置
游戏设置和相关初始化放在了一个类里面,并进行了静态声明。主要设置了游戏窗口的长和款。并在GameInit()函数里面设置了窗口大小,隐藏光标,初始化随机数种子等。代码如下:

//游戏设置相关模块,把函数都放到一个类里面了。函数定义为static静态成员,不生成实体也可以直接调用class GameSetting{public: //游戏窗口的长宽 static const int window_height = 40; static const int window_width = 80;public: static void GameInit() { //设置游戏窗口大小 char buffer[32]; sprintf_s(buffer, “mode con cols=%d lines=%d”,window_width, window_height); system(buffer); //隐藏光标 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息 CursorInfo.bVisible = false; //隐藏控制台光标 SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态 //初始化随机数种子 srand((unsigned int)time(0)); }};

用到了几个相关的Windows API,本文不做过多介绍,大家百度即可。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于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; } ``` 注意:此代码可能存在一些漏洞和问题,仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值