当前位置: 首页 > news >正文

c++贪吃蛇V1.0

哈哈哈,回归啦,也是成功的开学了好吧

贪吃蛇V1.0代码(如有bug可在评论区指出):wasd操控

#include <iostream>
#include <vector>
#include <windows.h>
#include <conio.h>
#include <chrono>
#include <thread>using namespace std;const int WIDTH = 20;
const int HEIGHT = 20;
bool gameOver;
int x, y, fruitX, fruitY, score;
vector<pair<int, int>> snake;
enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN };
Direction dir;void Setup() {gameOver = false;dir = STOP;x = WIDTH / 2;y = HEIGHT / 2;fruitX = rand() % WIDTH;fruitY = rand() % HEIGHT;score = 0;snake.clear();snake.push_back({x, y});
}void Draw() {system("cls");for (int i = 0; i < WIDTH + 2; i++)cout << "#";cout << endl;for (int i = 0; i < HEIGHT; i++) {for (int j = 0; j < WIDTH; j++) {if (j == 0) cout << "#";if (i == y && j == x) cout << "O";else if (i == fruitY && j == fruitX) cout << "F";else {bool isBody = false;for (auto segment : snake) {if (segment.first == j && segment.second == i) {cout << "o";isBody = true;break;}}if (!isBody) cout << " ";}if (j == WIDTH - 1) cout << "#";}cout << endl;}for (int i = 0; i < WIDTH + 2; i++)cout << "#";cout << endl;cout << "Score:" << score << endl;
}void Input() {if (_kbhit()) {switch (_getch()) {case 'a': dir = LEFT; break;case 'd': dir = RIGHT; break;case 'w': dir = UP; break;case 's': dir = DOWN; break;case 'x': gameOver = true; break;}}
}void Logic() {pair<int, int> prev = snake[0];pair<int, int> prev2;snake[0] = {x, y};for (size_t i = 1; i < snake.size(); i++) {prev2 = snake[i];snake[i] = prev;prev = prev2;}switch (dir) {case LEFT: x--; break;case RIGHT: x++; break;case UP: y--; break;case DOWN: y++; break;}if (x >= WIDTH || x < 0 || y >= HEIGHT || y < 0)gameOver = true;for (size_t i = 1; i < snake.size(); i++) {if (snake[i].first == x && snake[i].second == y)gameOver = true;}if (x == fruitX && y == fruitY) {score += 10;fruitX = rand() % WIDTH;fruitY = rand() % HEIGHT;snake.push_back({-1, -1});}
}int main() {Setup();while (!gameOver) {Draw();Input();Logic();this_thread::sleep_for(chrono::milliseconds(100));}return 0;
}

http://www.dtcms.com/a/500170.html

相关文章:

  • 南京量身营销型网站设计个人网站 做啥好
  • 每日算法刷题Day75:10.18:leetcode 二叉树14道题,用时3h
  • C语言解析json字符串
  • 网站在哪里设置域名站长统计免费下载
  • 街道门户网站的建设思路.net网站模版
  • MyBatis—XML配置
  • Microsoft AI Genius | 用 MCP 解锁实时数据,重新定义交互边界
  • 杨园建设社区网站wordpress digg主题
  • HarmonyOS6 应用升级经验分享
  • 核工业西南建设集团有限公司网站网站管理助手 二级域名
  • 十月通勤小记
  • 贵州住房城乡建设厅官方网站公司的网站
  • 绪论——一文速通
  • 皮卡丘密码
  • 聊城建设局网站做公司官网大概多少钱
  • 教育系统网站cms网站开发完整视频
  • 肇庆免费模板建站logo成品效果图网站
  • 电商网站的支付接入该怎么做呢wordpress静态分页
  • Vue2(一):创建实例、插值表达式、Vue响应式特性、Vue指令、指令修饰符、计算属性
  • 网站建设东莞公司湖南邵阳调整多个风险区
  • 10种常用的进制转换
  • Chainlit+RAG 实战:从前端界面到多模态检索增强生成全流程开发
  • 太原0元网站建设创意网页设计作品
  • 制作网站首先做的是搜素引擎排名优化计费方式
  • Linux系统信息查看:CPU、内存、版本、硬盘使用情况
  • 【CVPR 2025】MaDCoW广角照片任意物体的畸变校正论文笔记
  • 教学单位 网站建设域名大全 二级域名
  • 34部署LNMP架构详细解析
  • 国内跨境电商建站系统北京网站优化企业
  • 【电路·3】化简电路、等效变换、求输入电阻;其实很简单~