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

C++编写的经典贪吃蛇游戏

C++编写的经典贪吃蛇游戏

在油管上面看到一个Up主TheCherno的The Most Professional C++ Project I’ve Ever Reviewed // Code Review视频,他对一个使用C++编写的贪吃蛇游戏做了代码审视,对应的
Github源代码地址为:https://github.com/Nazar2347/Snake

Snake Game (C++ / Raylib)

My representaion on the classic Snake game, built in C++ with Raylib library for rendering and CMake for build automation.
Includes core game logic, interactive UI, and robust unit tests using Catch2
我对经典贪吃蛇游戏的诠释,使用 C++ 构建,使用 Raylib 库进行渲染,并使用 CMake 进行构建自动化。
包含核心游戏逻辑、交互式 UI 以及使用 Catch2 进行强大的单元测试。

Features

  • 🎮 W,A,S,D - controlls

  • 🤖 Movable food for Snake - so it’s a bit challeging 😃

  • 🔰 3 levels with different layout + Level cutomization (incoming feature)

  • 🟢 Rendering, UI, visuals with Raylib

  • 🧪 Unit tests with Catch2

  • 🧱 Used OOP Game patterns: Command, GameLoop

  • 🧩 Separeted {core logic} from {render} and merging into {game logic} then into game loop

  • 📁 Clean folder structure and CMake build automation

  • 📄 DoxyDocumentaion

  • 📑 Clang for Code formating

  • 📊 Kanban Project Managment methodology using Jira

    Gameplay

    GameCapture

GameCapture2

How to run the game (Windows) (2.17 MB) 🕹

  1. You can run game by simply launching: Install->SnakeMain.exe
OR
  1. Download form my GoogleDisk

How to run the tests 🧪

  1. Clone repository, open command console in downloaded repo
  2. Run these commands:
  • cmake -S . -B ./build
  • cmake --build ./build
  • ctests --test-dir ./build/tests --verbose

OR

You can see tests are always running after succesfull build and compilation of the program!

Project structure📂:

Snake/

├── Assets # Textures for game

├── external #External libraries source files (RayLib)

├── Install/ # Game Instalation package

├── source/

│ ├── core/ # Game logic (Snake, Board, Food)

│ ├── render/ # Raylib rendering code

│ └── app/ # Game entry point and logic

├── tests/ # Catch2 unit tests

├── CMakeLists.txt # Build configuration

└── README.md # This file

Kanban Project Managment methology 📊🧐

  • Since the beginning till the first release version all the work tasks have been described and managed
    by applying Kanban Project Managment methology in Jira Atlassian to have structured worflow and clear steps inorder to succeed
    KanbanWorkflow

🤓☝️ It’s my third small game project, where this time I used newly obtained skills such as:

  • Game Patterns (GameLoop, Command)
  • Combination of popular code practices (S.O.L.I.D), and less coupling class architecture
  • Smart pointers
  • Jira Kanban project managment
  • GitHub branch-based workflow

使用VS2022和cmake编译运行snake源代码

首先得保证在Windows11中安装了VS2022cmake,然后按照下面步骤执行:

  1. Clone repository, open command console in downloaded repo
  2. Run these commands:
  • cmake -S . -B ./build
  • cmake --build ./build
  • ctests --test-dir ./build/tests --verbose

注意:cmake -S . -B ./build实际执行起来比较慢,会下载raylibCatch2源代码,如下图所示:
raylib
Catch2
所有命令执行完成后得到的build目录如下图所示:
build目录
鼠标双击上图中的Snake.sln工程文件,使用VS2022打开项目,如下图所示:
snake项目工程结构
并且在对应的Snake\build\source\app\Debug目录下生成了SnakeMain.exe可执行程序,鼠标双击该可执行程序,运行截图如下所示:
运行snake程序
可以看出游戏界面还蛮好看的。

Game Over
当我们直接使用VS2022打开snake项目,并将SnakeMain设为启动项目可以正常编译,但是会因为找不到资源文件导致运行失败,如下图所示:
运行snake游戏由于找不到资源文件运行失败
可以看到由于打开资源文件失败,导致游戏主界面是空白的。
原因是源代码中配置的工作目录不对,原始的工作目录为:${ProjectDir},如下图所示:

原始工作目录为${ProjectDir}
ProjectDir对应的全路径

从上图可以看出工作目录为${ProjectDir}宏,对应的全路径为:E:\projects\VS2022Porjects\CPlusExamples\Snake\build\source\app\

需要改成Assets资源文件夹所在目录$(SolutionDir)..\,即对应我的电脑的目录全路径为:E:\projects\VS2022Porjects\CPlusExamples\Snake\build\..\,如下图所示:
更改工作目录
$(SolutionDir)宏对应的是Snake.sln解决方案根目录,在build上一层目录有Assets资源目录
解决方案根目录
Assert目录和build目录同级

然后再次运行SnakeMain项目,可以看到项目能够正常运行,如下图所示:
更改工作目录后snakeMain正常运行

参考资料

  • The Most Professional C++ Project I’ve Ever Reviewed // Code Review
  • https://github.com/Nazar2347/Snake
  • https://github.com/raysan5/raylib
    A simple and easy-to-use library to enjoy videogames programming
  • http://www.raylib.com/
  • https://github.com/catchorg/Catch2
    A modern, C+±native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
http://www.dtcms.com/a/393588.html

相关文章:

  • 风险预测模型原理
  • PS练习5:利用翻转制作图像倒影
  • 平替Jenkins,推荐一款国产开源免费的CICD工具 - Arbess
  • aws 实战小bug
  • NumPy 系列(一):numpy 数组基础
  • VSCode 的 launch.json 配置
  • OpenLayers地图交互 -- 章节六:范围交互详解
  • 分布式专题——15 ZooKeeper特性与节点数据类型详解
  • 分布式专题——16 ZooKeeper经典应用场景实战(上)
  • Torch-Rechub学习笔记-task2
  • Hadoop分布式计算平台
  • hive调优系列-1.调优须知
  • 爆炸特效:Unity+Blender-01
  • 解决切换 Node 版本后 “pnpm 不是内部或外部命令”问题
  • flag使用错误出现bug
  • 【Kafka面试精讲 Day 20】集群监控与性能评估
  • SQL 注入攻防:绕过注释符过滤的N种方法
  • 微软常用运行库
  • 在Kubernetes(k8s)环境中无法删除持久卷(PV)和持久卷声明(PVC)的解决方案
  • 【连载7】 C# MVC 跨框架异常处理对比:.NET Framework 与 .NET Core 实现差异
  • 芯脉:面向高速接口的SoC架构与完整性设计<3>
  • ArrayList与LinkedList深度对比
  • AI IDE 综合评估:代码能力与上下文连续性深度分析
  • OceanBase备租户创建(一):通过CREATE STANDBY TENANT
  • C++ 多态:从概念到实践,吃透面向对象核心特性
  • ​​如何用 Webpack 或 Vite 给文件名(如 JS、CSS、图片等静态资源)加 Hash?这样做有什么好处?​​
  • QT-数据库编程
  • FastAPI + APScheduler + Uvicorn 多进程下避免重复加载任务的解决方案
  • 数据库造神计划第十八天---事务(1)
  • Docker在Linux中离线部署