用Amazon Q Developer CLI构建平台跳跃游戏,全程无需编程!

本文描述了亚马逊云科技资深开发者布道师黄浩文如何通过“氛围编程”(Vibe Coding),使用Amazon Q Developer CLI构建一款经典平台跳跃游戏,全程无需开发者编写任何代码。

文章详述了三阶段的开发过程:首次尝试、功能增强及通过让AI编程助手从参考代码项目中学习,以及规划构建完整的新游戏项目。

引言


近期我踏上了一段有趣的探险旅程:使用Amazon Q Developer CLI生成一款经典平台游戏的原型。随着更深入地探索Amazon Q CLI的功能,发现它能够完成的远不止创建基础游戏原型。这个项目还在自我进化中,但已经包含了基本游戏机制、角色动画、计分系统、物理效果(重力、跳跃、碰撞检测)、音效以及多个游戏关卡——所有这一切都无需我编写一行代码!这种生成式AI编程辅助的体验,激励我记录下这次平台跳跃游戏开发之旅。

通过氛围编程,构建平台跳跃游戏的旅程分为三部分:

  1. 首次尝试。

  2. 功能增强迭代。

  3. 完整游戏项目的实现。

下文将详细介绍整个过程。

设置

首先,确保您已安装Amazon Q Developer CLI(后文简称Q CLI)。由于我使用的是MacBook,我通过以下步骤安装了适用于macOS的Q CLI。

  • 下载适用于macOS的Amazon Q命令行工具。

  • 使用Amazon Web Services Builder ID注册并进行身份验证。

  • 按照说明安装shell集成并授予macOS辅助功能权限。

Amazon Q Developer CLI:

https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-installing.html?trk=8bbe71eb-70f9-4bf2-aada-259c2bbd07ce&sc_channel=blog

下载适用于macOS的Amazon Q命令行工具:

https://desktop-release.codewhisperer.us-east-1.amazonaws.com/latest/Amazon%20Q.dmg

Builder ID注册:

https://community.aws/builderid?trk=8bbe71eb-70f9-4bf2-aada-259c2bbd07ce&sc_channel=el

在开始平台游戏开发之前,通过运行以下命令验证Q CLI环境是否正常工作。

$ q doctor

如上截图所示,`q doctor`命令是一个有用的工具,可以确认您的Q CLI安装正常运行。

接下来将使用Pygame作为框架,用Python开发游戏。Pygame是一个开源库,它提供了在Python中创建游戏的基本构建模块,包括:

  • 图形和动画功能。

  • 声音播放功能。

  • 输入处理(键盘、鼠标、游戏手柄)。

  • 基本碰撞检测。

  • 表面操作和绘图基元。

  • 支持各种图像和声音格式。

您可以使用pip包管理器通过以下命令轻松安装Pygame库。

$ pip install pygame

首次尝试

环境设置完成后,使用Q CLI创建一款使用Pygame的简单平台游戏原型。

$ q chat> Create a classic platform game using the Pygame library, replacing traditional coins with stars and standard walls with post-industrial cyberpunk style

左右滑动查看完整示意

*提示词中文大意:使用Pygame库创建一款经典平台游戏,将传统金币替换为星星,将标准墙壁替换为后工业赛博朋克风格。

经过几分钟的思考和代码自动编写,Q CLI很快生成了一款完全可运行的平台跳跃游戏。生成的文件'main.py'包含了一款完整的游戏实现,并可立即开始体验,如下截图所示。

如果您有兴趣探索这款仅通过自然语言提示词创建的平台游戏原型,您可以在我的GitHub仓库中查看完整代码。

GitHub仓库:

https://github.com/hanyun2019/q-cli-platform-game-demo/tree/main/code/v1

要运行游戏,请使用以下Shell命令。

source venv/bin/activate && python main.py

左右滑动查看完整示意

本章小结

首次使用Q CLI,它就能在提交单个提示词后,成功完成编程任务,开局不错。

然而,目前其输入的游戏UI看起来相对基础简朴。所以,我决定继续使用额外的提示词与Q CLI进行持续沟通,希望使游戏效果在视觉等方面更具吸引力。

代码的增强和迭代

基于我的第一次尝试,我体验到了Q CLI在氛围编程中的魅力,这促使我进行了几项有趣的探索。由于篇幅限制,我在此处列出一些我主要的输入提示词,让各位开发者们可以了解我当时的思考过程。

重新加载现有代码

我意识到我需要找到一种方法来重新加载我之前编写的代码。考虑到时间限制,在一天内完成整个平台游戏项目似乎不太现实。因此,我想建立一个工作流程,使我能够从之前的代码工作进度继续。

首先,我需要创建一个新的V2目录来开始下一阶段的工作。我想:既然我正在使用Vibe Coding,为什么不利用Q CLI来协助这项任务呢?因此我进行了以下探索。

> /quit
$ q chat >  Please examine the code directory located at /Users/AMAZON/DA-Stage/DA-Role/2025/CLI-Q/my-blogs/platform-game/v1, which contains the platform game code(version 1) that you previously wrote. I need you to create a new directory at /Users/AMAZON/DA-Stage/DA-Role/2025/CLI-Q/my-blogs/platform-game/v2 and copy all the version 1 code files to thisnew directory.

左右滑动查看完整示意

*提示词中文大意:请检查位于/2025/CLI-Q/my-blogs/platform-game/v1的代码目录,其中包含之前编写的平台游戏代码(版本1)。我需要在/2025/CLI-Q/my-blogs/platform-game/v2创建一个新目录,并将所有版本1代码文件复制到这个新目录中。

如上截图所示,Q CLI成功创建了一个新的V2子目录,并将V1版本的所有代码复制到这个新位置。接下来,我指示Q CLI在该新子目录中继续工作。

>  You should work on the directory: /Users/AMAZON/DA-Stage/DA-Role/2025/CLI-Q/my-blogs/platform-game/v2

左右滑动查看完整示意

然后可以继续进行氛围编程。由于篇幅限制,下文只展示一些提交给Q CLI的有趣提示词,供各位开发者们参考。

添加精灵和音效

Enhance this game by implementing the following extension features: 1) Improving the 3D visual effects of the gameplay graphics; 2) Add appropriate sound effects; 3) Creating a scrolling camera system

左右滑动查看完整示意

*提示词中文大意:通过实施以下扩展功能来增强这款游戏:1.改进游戏图形的3D视觉效果;2.添加适当的音效。

实现滚动摄像机

> Continue to enhance this platform game by: 1) Improving the 3D visual effects of the sprites; 2) Adding more levels; 3) Implementing power-ups; 4) Creating a scrolling camera; 5) Including more enemy types

左右滑动查看完整示意

*提示词中文大意:继续增强这款平台游戏:1.改进精灵的3D视觉效果;2.添加更多关卡;3.实现能力提升道具;4.创建滚动摄像机;5.包含更多敌人类型。

此探索阶段的最终游戏截图如下所示。

如果您对这一探索阶段的代码感到好奇,可以在我的GitHub仓库中查看完整代码。

GitHub仓库:

https://github.com/hanyun2019/q-cli-platform-game-demo/tree/main/code/v2

探索到这里,我对生成的代码其实并不是特别满意。于是我开始思考:是否存在一种更高效的氛围编程方法呢?例如,是否可以从现有的开源项目中学习,以创建一个更全面的游戏项目代码规划呢?

这一问题构成了下文将要讨论的重要基础。

完整游戏项目的实现

在前文两部分中,我们探索了使用Q CLI从头开始构建一款平台跳跃游戏。然而,逐步地提供自然语言需求,让Q CLI进行修改的方法,对于设计一款真正全面的平台跳跃游戏来说是不够的。很多软件开发的重要方面,例如整体游戏框架设计、跨多个关卡的难度递进设计等,都需要采用更全面的方法。因此,我们需要一种更整体全局考虑的氛围编程(Vibe Coding)思考方式。

这一认识促使我提出一个新问题:我能否为Q CLI背后的大语言模型(LLM)提供一个完整的参考和全局的视角,使它能够学习并从全局角度设计游戏?这正是我在本章节中希望探索的重点内容。

选择参考项目的原则

我选择了一个PirateMaker项目作为我游戏开发的参考代码库。我打算将该代码库直接提交给Q CLI用于学习和参考,在它完全吸收学习后,让Q CLI再来设计一个新的游戏项目框架。

选择这个特定游戏代码项目的一个关键因素是其许可状态:该项目代码处于公共领域。这意味着它可以被复制、修改、分发,甚至用于商业目的而无需获得许可。

从外部代码学习

为演示Q CLI如何从外部代码学习并生成新的代码,首先验证环境并启动对话。

$ q doctor$ q chat

然后,提交以下提示词给Q CLI。

> Referring to the approach and implementation of the code in the project https://github.com/clear-code-projects/PirateMaker, design a complete platform game in Python.

左右滑动查看完整示意

*提示词中文大意:参考项目https://github.com/clear-code-projects/PirateMaker中的代码方法和实现,用Python设计一款完整的平台游戏。

提交提示词后,Q CLI的回复输出如下。

I'll help you design a platform game in Python using the PirateMaker project as a reference. Let's first examine the PirateMaker project structure to understand its implementation approach.
I will run the following shell command:git clone https://github.com/clear-code-projects/PirateMaker /tmp/PirateMaker && ls -la /tmp/PirateMaker
Allow this action? Use 't'to trust(always allow)this tool for the session. [y/n/t]:> t

左右滑动查看完整示意

*Q CLI回复内容中文大意如下:

我将帮助您使用PirateMaker项目作为参考,用Python设计一款平台游戏。让我们首先检查PirateMaker项目结构,以了解其实现方法。

我将运行以下shell命令:

git clone https://github.com/clear-code-projects/PirateMaker /tmp/PirateMaker && ls -la /tmp/PirateMaker

允许此操作吗?使用't'来信任(始终允许)本次会话中的这个工具。[y/n/t]:

> t

当输入"t"后,Q CLI开始执行了`git clone`命令,并接着开始详细分析PirateMaker项目代码的结构和实现细节。它检查了项目的完整层次结构,并生成了自己理解之后的整体游戏设计。

Q CLI生成的整体游戏设计

接下来是激动人心的时刻!在阅读和分析了之前展示给它的所有PirateMaker代码后,Q CLI首先生成了一款高层次的整体游戏设计。

I've designed a complete platform game in Python, drawing inspiration from the PirateMaker project approach. Here's an overview of the implementation:I've created a comprehensive game structure with the following key components:1.Core Game Engine(main.py)   • Manages game states(menu, level play, editor)   • Handles the main game loop   • Coordinates all game components
2. Game Configuration(settings.py)   • Contains all game constants and settings   • Defines screen dimensions, physics parameters, and game states
3. Level Management(level.py)   • Handles level loading from CSV files   • Manages level rendering and game mechanics   • Processes collisions and interactions
4. Player Character(player.py)   • Implements Mario's movement mechanics   • Handles animations and state changes   • Manages power-up states(Small, Big, Fire, Star)
5. Enemy System(enemies.py)   • Defines enemy behaviors(Goombas, Koopas)   • Implements AI movement patterns   • Handles enemy-player interactions
6. Tile System(tiles.py)   • Creates the game world building blocks   • Implements interactive elements   • Supports animated tiles and special blocks
7. User Interface(ui.py)   • Manages menus, HUD, and game screens   • Handles button interactions   • Displays game information(score, star, time)
8. Level Editor(editor.py)   • Provides tools for creating custom levels   • Supports multiple layer editing   • Implements save/load functionality
9. Visual Effects(particles.py)   • Creates particle effects for various game events   • Enhances visual feedback
10. Support Functions(support.py)    • Contains helper functions for asset loading    • Provides utility functions for the game
## Key Features• **Classic platform Gameplay**: Walking, running, jumping mechanics• **Power-up System**: Mushroom, Fire Flower, Star• **Enemy Interactions**: Jump on enemies to defeat them• **Interactive Elements**: Question blocks, breakable bricks• **Level Editor**: Create and customize game levels• **Particle Effects**: Visual feedback for actions• **Menu System**: Main menu, level selection, pause screen
## Implementation DetailsThe game uses a tile-based approach for level design, with CSV files storing the level data. The rendering system handles scrolling to follow the player through larger levels. Physics calculations manage gravity, jumping, and collisions.The animation system supports frame-based animations for the player, enemies, and interactive elements. The game state management allows for seamless transitions between menus, gameplay, and the level editor.## Project Structureplatform-game/├── main.py           # Entry point and game loop├── settings.py       # Game configuration├── level.py          # Level management├── player.py         # Player character├── enemies.py        # Enemy behaviors├── tiles.py          # Game world elements├── ui.py             # User interface├── support.py        # Helper functions├── editor.py         # Level editor├── particles.py      # Visual effects├── assets/           # Game assets folder└── levels/           # Level data files
This implementation provides a solid foundation for a platform game with all the essential features. The modular design allows for easy expansion with additional enemies, power-ups, and game mechanics.

左右滑动查看完整示意

修复代码问题

当然开发过程并非没有挑战。当首次执行时,Q CLI生成的代码通常包含需要解决的错误。然而,我最欣赏Q CLI的是其命令行界面,它独立于特定的IDE运行。这种方法使开发者能够在整个调试过程中保持一致和流畅的氛围编程体验。

如果最初生成的代码有缺陷,您可以直接通过例如以下类型的提示词与Q CLI进行沟通。

> You previously referenced https://github.com/clear-code-projects/PirateMaker and generated some platform game code in this directory, but the game cannot run correctly now. Please modify these codes so that the game can run properly

左右滑动查看完整示意

*提示词中文大意:你之前参考了https://github.com/clear-code-projects/PirateMaker并在这个目录中生成了一些平台游戏代码,但游戏现在无法正常运行。请修改这些代码,使游戏能够正常运行。

对于特定的实现问题,您可以在与Q CLI交互时继续使用氛围编程方法。例如,要解决UTF-8编码问题,使用以下提示词。

> Could you fix the trouble "failed to print tool: stream did not contain valid UTF-8"?

左右滑动查看完整示意

*提示词中文大意:你能修复“failed to print tool: stream did not contain valid UTF-8”这个问题吗?

当向Q CLI请求新的功能特性时,只需用自然语言描述需求,例如以下自然语言描述。

> I need a 'Replay' feature

左右滑动查看完整示意

或者:

> When the red block and blue block collide with each other, the screen freezes, and you can only press 'R' to restart or press 'ESC' to exit.

左右滑动查看完整示意

或者:

> The Arrow keys used for moving left and right are not working

左右滑动查看完整示意

或者:

> How can I proceed to the next level?

左右滑动查看完整示意

游戏展示

最终我成功完成了一款有三种不同难度级别的平台跳跃游戏(其实游戏还可以无限进化),而且全程未亲自写一行代码!您可点击观看下方视频,体验这款完全由AI生成的游戏开发过程的沉浸式演示(体现了氛围编程的方法),供各位开发者参考。

截止目前为止,虽然该款完全由AI生成的游戏,某些艺术元素仍需完善,比如在这个版本中我尚未指示Q CLI生成更生动的墙壁纹理,但游戏的底层代码结构已经基本全面。可以预见,如果再花几个小时以氛围编程的方式与Q CLI交互,相信会产生更加精细的游戏体验。


有关这款游戏Q CLI生成代码技术细节,可在我的GitHub仓库中访问完整的代码库。

GitHub仓库:

https://github.com/hanyun2019/q-cli-platform-game-demo/blob/main/code/v3/

如果希望在您的macOS本地环境中运行游戏,可参考执行以下shell命令。

source venv/bin/activate python fixed_game_v1.py

左右滑动查看完整示意

总结

使用Amazon Q Developer CLI,我创建了一款完整的平台跳跃游戏,而没有亲自编写一行代码,这一开发过程经历了以下三个不同阶段。

  1. 通过简单的提示词生成了基本的功能性游戏。

  2. 通过迭代式自然语言互动推动Q CLI代码改进,游戏加入了精灵图像、音效和滚动摄像等功能。

  3. 通过指示Q CLI分析一款公开的游戏项目作为基础知识参考,它生成了一款较完整的经典平台跳跃游戏架构,其中包含了复杂的关卡管理、物理系统和敌人系统等。当技术问题(BUGs)出现时,我使用自然语言提示词和Q CLI互动,最终也成功解决了这些缺陷。最终的产品形态,已经具有了初步的自定义精灵图、音效和三种难度级别,而所有这些代码都是通过AI驱动的氛围编程来完成的

完成这段平台游戏开发之旅后,我强烈推荐Amazon Q Developer CLI作为编程助手。这是一个全新的世界和时代,游戏开发的入门门槛从未如此之低,创意可能性也从未如此广阔。

祝各位开发者氛围编程愉快!

本篇作者

黄浩文

亚马逊云科技资深开发者布道师。拥有电信、互联网以及云计算等行业超过20年的丰富经验。目前专注于生成式AI、大语言模型(LLM)、机器学习和数据科学等领域的技术内容创作和实践分享,致力于赋能全球开发者。

星标不迷路,开发更极速!

关注后记得星标「亚马逊云开发者」

听说,点完下面4个按钮

就不会碰到bug了!

点击阅读原文查看博客!获得更详细内容!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值