← 返回资讯屏轩教育 · 行业资讯

Case Study: Virtual Pet Simulation | 案例分析:虚拟宠物模拟

2026-07-17 03:54 · 本文部分内容由AI辅助创作

📚 Case Study: Virtual Pet Simulation | 案例分析:虚拟宠物模拟

In this case study, we will design and build a simple virtual pet simulation using Scratch. You will learn how to apply computational thinking skills — decomposition, pattern recognition, abstraction, and algorithm design — to turn an idea into a working program. By following each step, you’ll practise real-world problem-solving that mirrors how software developers tackle projects.

在本案例中,我们将使用 Scratch 设计并搭建一个简单的虚拟宠物模拟程序。你将学习如何运用计算思维技能——分解、模式识别、抽象和算法设计——把想法变成可运行的程序。跟随每一步操作,你将练习真实的解决问题方式,就像软件开发人员处理项目一样。


1. Introduction to the Case Study | 案例简介

Our goal is to create a virtual pet that a user can feed, play with, and put to sleep. The pet will have three main attributes: hunger, happiness, and energy. If the pet becomes too hungry or too tired, it will show a sad expression; if all needs are met, it will appear happy. The user interacts by clicking on-screen buttons.

我们的目标是创建一个用户可以喂食、玩耍和哄睡的虚拟宠物。宠物将有三个主要属性:饥饿度、快乐值和精力值。如果宠物太饿或太累,它会显示悲伤的表情;如果所有需求都得到满足,它就会表现出开心的样子。用户通过点击屏幕上的按钮进行交互。


2. Understanding the Problem | 理解问题

Before writing any code, we must clarify exactly what the program should do. We ask questions: What are the inputs? (User clicks on feed, play, or sleep buttons.) What are the outputs? (The pet’s appearance changes based on its internal values.) What are the constraints? (Hunger increases over time, energy decreases when awake, and happiness fades if neglected.)

在编写任何代码之前,我们必须厘清程序到底要做什么。我们需要问自己:输入是什么?(用户点击喂食、玩耍或睡觉按钮。)输出是什么?(宠物的外观根据其内部数值变化。)限制条件有哪些?(饥饿度随时间增加,清醒时精力下降,被冷落时快乐值会降低。)


3. Decomposition: Breaking Down the Tasks | 分解:拆分任务

Decomposition means dividing a complex problem into smaller, manageable parts. For this project, we can break it down into sub-tasks:

分解就是把复杂问题拆分成更小、更容易处理的部分。对于这个项目,我们可以将其分解为以下子任务:


4. Pattern Recognition and Abstraction | 模式识别与抽象

Pattern recognition helps us spot similarities. In this simulation, hunger, happiness, and energy all behave in a similar way: they increase or decrease within a fixed range (0–100) and trigger a costume change when they cross a threshold. We can abstract this idea by creating a general “update status” block that checks all variables at once.

模式识别帮助我们找到相似之处。在这个模拟中,饥饿度、快乐值和精力值的行为方式十分相似:它们在固定范围(0–100)内增减,并在越过某个阈值时触发造型变化。我们可以通过创建一个通用的“更新状态”模块来抽象这个想法,一次性检查所有变量。


5. Algorithm Design: Flowchart | 算法设计:流程图

We can design a simple algorithm using a flowchart before coding. The main loop might look like this:

在编码之前,我们可以使用流程图设计一个简单的算法。主循环可能如下所示:

StepAction
1Set hunger = 50, happiness = 50, energy = 50
2Wait 1 second
3Increase hunger by 1, decrease happiness and energy by 1 if awake
4If any variable reaches 0 or 100, change costume
5Check for button clicks and adjust variables accordingly
6Repeat from step 2

Flowchart symbols: an oval for start/end, rectangles for actions, diamonds for decisions. This logical structure will guide our Scratch coding.

流程图符号:椭圆表示开始/结束,矩形表示操作,菱形表示判断。这种逻辑结构将指导我们的 Scratch 编码。


6. Setting Up the Scratch Project | 搭建 Scratch 项目

Open Scratch and create a new project. Delete the default cat sprite. Import or draw a cute animal sprite, such as a dog or a creature of your choice. Add three additional sprites for buttons: a “Feed” button (maybe a bone icon), a “Play” button (a ball), and a “Sleep” button (a moon). You can design simple text buttons using the Paint editor.

打开 Scratch 并新建一个项目。删除默认的猫咪角色。导入或绘制一个可爱的动物角色,比如一只小狗或你选择的生物。再添加三个按钮角色:“喂食”按钮(可以用骨头图标)、“玩耍”按钮(一个球)和“睡觉”按钮(一个月亮)。你可以使用绘图编辑器设计简单的文字按钮。


广告 / Sponsored

7. Creating Variables and Sprites | 创建变量和角色

In the Variables palette, make three variables: hunger, happiness, and energy. Make them “for all sprites” so every sprite can read and change them. Add a fourth variable “awake” to track if the pet is asleep. When the green flag is clicked, initialize all values to 50 and set awake to 1 (awake). Display these variables on the stage so we can monitor them during testing.

在“变量”面板中,创建三个变量:hunger、happiness 和 energy。将它们设为“适用于所有角色”,这样每个角色都能读取和修改它们。再添加第四个变量“awake”来记录宠物是否在睡觉。当点击绿旗时,将所有初始值设为 50,并将 awake 设为 1(清醒)。在舞台上显示这些变量,以便在测试时监控它们的值。


8. Programming the Pet's Needs | 编写宠物的需求程序

Inside the pet sprite, build a script that runs continuously. Use a forever loop with a wait block (e.g., wait 1 seconds). Inside the loop:

在宠物角色内部,构建一个连续运行的脚本。使用“重复执行”循环并添加“等待”积木(例如等待 1 秒)。在循环内:


Define the “update costume” block: if hunger > 80 or energy < 20, switch to sad costume; if happiness > 80 and hunger < 30 and energy > 50, switch to happy costume; else switch to normal costume. This makes the pet visually respond to its internal state.

定义“update costume”积木:如果 hunger > 80 或 energy < 20,换成伤心造型;如果 happiness > 80 且 hunger < 30 且 energy > 50,换成开心造型;否则换成正常造型。这样宠物就能根据内部状态做出视觉反应。


8. Handling User Interactions | 处理用户交互

Each button sprite needs its own script. When the “Feed” button is clicked, broadcast a message “feed”. In the pet sprite, when it receives “feed”, decrease hunger by 20 (but not below 0), and increase happiness by 5. Similarly, “Play” decreases energy by 10 and increases happiness by 15, but only if the pet is awake. The “Sleep” = 0 set it to 1. While sleeping, energy increases by 2 every second instead of decreasing.

每个按钮角色都需要自己的脚本。当点击“喂食”按钮时,广播消息“feed”。在宠物角色中,当接收到“feed”时,hunger 减少 20(但不低于 0),happiness 增加 5。类似地,“玩耍”会减少 energy 10 并增加 happiness 15,但前提是宠物处于清醒状态。“睡觉”按钮会切换 awake 变量:如果 awake = 1,将其设为 0 并换成睡觉造型;如果 awake = 0,则设为 1。睡觉时,energy 每秒增加 2 而不是减少。


9. Adding a Game Over Condition | 添加游戏结束条件

To make the simulation more engaging, we can introduce a lose condition. If hunger reaches 100 or energy reaches 0 while awake, the pet can say “I need care!” and the program stops. Alternatively, you can hide the pet and show a “Game Over” backdrop.

为了让模拟更吸引人,我们可以引入失败条件。如果饥饿度到达 100 或者精力值降到 0 且清醒时,宠物会说“我需要照顾!”,然后程序停止运行。或者,你也可以隐藏宠物并显示“游戏结束”背景。


10. Testing and Debugging | 测试与调试

Run the program with the green flag and watch the variable monitors. Click each button and observe if the pet’s costume changes correctly. Check boundary values: manually set hunger to 99 and then feed it, or set energy to 1 and play. Ensure variables never go outside 0–100. Common bugs include forgetting to reset awake when the green flag is clicked or allowing negative values. Fix these by adding initialisation blocks and clamp checks.

点击绿旗运行程序,观察变量监视器。点击每个按钮,观察宠物的造型是否正确变化。检查边界值:手动将饥饿度设为 99 然后喂食,或将精力值设为 1 然后玩耍。确保变量永远不会超出 0–100 的范围。常见的 bug 包括点击绿旗时忘记重置 awake,或者允许负值出现。通过添加初始化积木和范围限制检查来修复这些问题。


11. Evaluation and Improvements | 评估与改进

Reflect on what worked well. Does the virtual pet feel responsive? Could you add more features? Ideas for expansion include: a health variable, sound effects, a shop to buy items, or a timer that shows how many days the pet has survived. You could also replace the simple buttons with drag-and-drop food items. Evaluating a project helps you think like a developer and plan the next version.

回顾一下哪些地方做得好。虚拟宠物的反馈及时吗?你能添加更多功能吗?扩展的想法包括:增加健康值变量、音效、可购买物品的商店,或者一个显示宠物存活天数的计时器。你也可以用拖拽食物道具来代替简单的按钮。评估项目能帮助你像开发者一样思考,并规划下一版本。


12. Conclusion | 总结

This case study has walked you through the full cycle of software development — from understanding a problem and breaking it down, to implementing and testing a solution. You’ve used Scratch to bring a virtual pet to life while practising computational thinking skills that are essential in computer science. Try adapting the project to create your own unique pet simulation!

本案例带你走过了软件开发的完整周期——从理解问题、拆分问题,到实现和测试解决方案。你在用 Scratch 让虚拟宠物动起来的同时,也练习了计算机科学中必不可少的计算思维技能。试着改造项目,创建属于你自己的独特宠物模拟吧!

Published by TutorHao | Computer Science Revision Series | aleveler.com

广告 / Sponsored
现货 Cambridge International AS & ALevel IT Coursebook剑桥国际IGC…
计算机 备考教辅 · 正版现货 · 与本文内容相关
查看详情 / View on Taobao参考价 ¥467.0

更多咨询请联系16621398022(同微信)

备考资讯每日更新,关注不错过
考纲变化 · 科目分析 · 学习方法
扫码关注公众号 tutorhao
在微信里长按二维码识别关注
或截图后用「扫一扫 → 相册」选择本图
公众号:tutorhao