Night 29: Build your project
คุณเรียนจบคืนนี้แล้วด้วย Python หรือรหัสเทียม และนับรวมในทั้งสองภาษา คุณสามารถฝึกอีกครั้งได้ตามต้องการ
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,
IFandINPUTwork 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 |
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.
The shape of the program

- Top: the functions from night 28. The starter already has them, written a little shorter.
- Middle: the state:
EnergyandFood. - Bottom: the game loop: read, change, show, repeat.
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 output Status(Energy, Food).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Now make it a game. Read commands until quit. After each command, output the status. At the end, output Bye!.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
After each status line, ask PetMood(Energy, Food). If the pet is not happy, output I am hungry! หรือ I am tired!.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 4/5 · 280 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
For a command the game does not know, output I do not know that trick, with no status line and no warning.
คลิก Run เพื่อดูผลลัพธ์ที่นี่