Night 29: Build your project
你已使用 Python 或伪代码完成过这个夜间练习,两种语言均计入完成记录。你可以再次练习。
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Tonight: build your project
- Tonight you join the parts from night 28 into a real pet game!
- Nothing new to learn: functions, a game loop,
ifandinput()work together.
Words for tonight
| Word | Meaning |
|---|---|
| project | a bigger piece of work with many parts |
| menu | the list of things the user can choose |
| state | what the program remembers right now, like energy and food |
English
The game rules
| Command | What happens |
|---|---|
play |
energy goes down by 2 (play), food goes down by 1 |
eat |
food goes up by 3 (eat) |
sleep |
energy becomes 10 |
quit |
the game ends with Bye! |
- The pet starts with
energy = 5andfood = 2.
English
The shape of the program

- Top: the functions from night 28. The starter already has them.
- Middle: the state:
energyandfood. - Bottom: the game loop: read, change, show, repeat.
English
Play it yourself
- After the tasks, press Run and play: type
play,eat,sleepandquit. - Change the rules! Maybe
sleepmakes the pet a little hungry. Maybeplaygives a star. - Tomorrow is the last night. Your pet will be a star!
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
The parts from night 28 are ready. Read one command. play: use play(energy), and 1 food less (never below 0). eat: use eat(food). sleep: energy becomes 10. Then print status(energy, food).
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Now make it a game. Read commands until quit. After each command, print the status. At the end, print Bye!.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
After each status line, ask pet_mood(energy, food). If the pet is not happy, print I am hungry! 或 I am tired!.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
For a command the game does not know, print I do not know that trick, with no status line and no warning.
点击“运行”查看此处输出。