Small games from a first program
Anyone new to code. Any language that can print text and, after week 13, draw shapes.
从第一行程序开始,一年里做出几个自己设计的小游戏;每周都有办法测出哪里坏了。
A sample plan written with AI help, not a person’s story. Found a mistake? Say so in the room. · 示范计划,AI 协助编写,不是真人经历。发现错误,请到讨论室说。
Weeks 1–13 · 第 1–13 周
1Open any free code editor. Write a program that prints the name of an invented game and its one rule.
Check: Change one letter in the code. Only that letter should change in the output.
2Store health as 5 in a variable. Subtract 2 twice, printing health after the start and after each change.
Check: The output must be 5, 3, 1. Start from 9 without editing the subtractions: 9, 7, 5.
3Ask the player for left or right and print a different result for each. Handle every other answer too.
Check: Type LEFT, an empty line and 7. None may crash or be silently treated as right.
4Write a guessing game: a secret number from 1 to 20, with higher and lower hints in a loop.
Check: Fix the secret at 1, then at 20. Every hint must be true, and the loop must stop.
5Write a computer guesser that always picks the middle of the numbers still possible, from 1 to 20.
Check: Run all 20 secrets. The most guesses needed should be exactly 5. Six means a range bug.
6Move the hint, the input check and the win test into three separate functions.
Check: Call each function alone with three inputs. If one needs the whole game running, split it again.
7Describe five connected places as data, not as if statements. Let the player walk between them.
Check: Draw the map from the data on paper. Walk each exit both ways and mark any mismatch.
8Add one item that opens one locked exit in the walking game.
Check: Try the exit without the item, take the item twice, and leave and return. Nothing should duplicate.
9Simulate 6000 rolls of two dice and print how often each total from 2 to 12 appears.
Check: Counts must sum to 6000. Total 7 should be near 1000; below 850 or above 1150 suggests a bug.
10Design a duel with two actions, attack and guard. Write all four combinations in a table, then code it.
Check: Force each pair of actions once. Compare the printed result with the table cell by cell.
11Let two random players fight 1000 duels and count first wins, second wins and draws.
Check: The three counts must sum to 1000. Swap who moves first: does the advantage move too?
12Make a list of ten strange inputs and run every game so far against all ten.
Check: Record input, expected reply, actual reply. Rerun every failing row after its fix.
13Review: rebuild the week 4 guessing game in an empty file without looking. Put both versions side by side; list what changed.
Check: Run both with secrets 1 and 20. Name one thing the new version does better and one worse.
Weeks 14–26 · 第 14–26 周
14Draw a 10 by 8 grid of squares on screen using two nested loops.
Check: Color the cell in column 9, row 0. If it appears elsewhere, rows and columns are swapped.
15Draw one shape that moves with four keys, with the program reading which keys are held down.
Check: Hold two opposite keys, then release one. Write what should happen first, then see if it does.
16Change movement to speed multiplied by the time since the last frame.
Check: Limit the program to 30 frames a second, then 60. Crossing time should match within a tenth.
17Add rectangular walls. Write a function that says whether two rectangles overlap.
Check: Test five cases: apart, touching edges, corner overlap, one inside another, identical. Decide whether touching counts.
18Let the mover slide along walls by checking sideways and vertical movement separately.
Check: Push diagonally into each side of a wall and each corner. Sticking or passing through is a fail.
19Place eight items to collect and show how many remain.
Check: Collect two that overlap in a single frame. The count must drop by exactly two.
20Add an obstacle that travels between two positions and sends the mover back to the start on contact.
Check: Watch twenty turns. Print its position at each turn; drift past the ends shows a missing limit.
21Organize the game into three states: title, playing, ended. Only one may run at a time.
Check: Press each key in each state and fill a table. An unexpected effect marks a leak between states.
22Write one reset function that returns every changing value to its starting value.
Check: Finish ten games without closing the program. Print all values after each reset; they must be identical.
23Store three layouts as rows of text characters and build each level from them.
Check: Remove one character from a row. The loader should name the bad row instead of drawing nonsense.
24Add a short sound and a visible flash for each event, plus a mute key.
Check: Play one round muted. Every event must still be noticeable without sound.
25Sketch a new game on paper with one rule of your own. Play it alone ten times with small paper pieces.
Check: Write the choice made each turn. If one choice was best every time, change the rule.
26Review: turn the week 8 locked exit into a drawn level. Compare text with drawing and note what changed in the code.
Check: List what moved unchanged from text to drawing. Anything rewritten was tied to how it was shown.
Weeks 27–39 · 第 27–39 周
27Add a jump: starting upward speed 400 pixels per second, gravity 800 pixels per second squared.
Check: Theory gives a height of 100 pixels. At 60 frames per second, print the peak; allow 5 off.
28Add platforms the mover can land on from above but pass through from below.
Check: Drop from far higher than usual. Falling through shows that a frame skipped past the platform.
29Make a level wider than the screen and let the view follow the mover.
Check: Visit both ends. Nothing outside the level should show, and collisions must not depend on the view.
30Move speeds, gravity and level names into one settings file or block.
Check: Double gravity by editing one line only. Peak height should roughly halve.
31Write an obstacle that notices the mover within 150 pixels and returns to its route afterwards.
Check: Stand at 149 and 151 pixels. Then stay near the limit: rapid switching means it needs two distances.
32Implement breadth-first search on the grid so a chaser takes a shortest route around walls.
Check: On three small maps, count the shortest route by hand. Compare, including a map with no route.
33Choose one number that controls difficulty. Play five attempts at each of three values and log time survived.
Check: If the three sets of times overlap fully, that number is not controlling difficulty. Choose another.
34Add a pause that stops movement, timers and sounds together.
Check: Pause for a full minute with a timer at 3 seconds left. It must still show 3 on return.
35Make the game usable without color differences, and let every control key be changed.
Check: View a screenshot in grayscale and play with one hand. List everything that became unclear.
36Save the current level and settings on the device, with a version number inside the saved data.
Check: Close and reopen. Then change the version number: the game should explain, not crash.
37Damage the saved data five ways: empty, cut short, wrong type, negative level, extra field.
Check: Every case must start the game safely. Note which damage was accepted and should have been refused.
38Generate item positions at random from a seed number shown on screen.
Check: Enter one seed twice and compare both lists of positions. Different lists mean another random source is involved.
39Review: rewrite the week 17 overlap function from memory, tests first. Describe each difference from the old one in a comment.
Check: Run the new tests on the old function. Any failure is a bug that hid for months.
Weeks 40–52 · 第 40–52 周
40Generate random walls, and reject any map where the exit cannot be reached.
Check: Make 500 maps and test each with separate code. Zero unreachable maps may get through.
41Invent a turn-based grid puzzle with one movement rule you have not seen elsewhere. Code one level.
Check: State the rule in one sentence. Find a board position where the sentence does not decide what happens.
42Add undo to the puzzle by recording the board before each move.
Check: Make 30 moves, undo 30 times and compare with the saved starting board, value by value.
43Write a solver that searches the puzzle for the fewest moves.
Check: Replay its answer in the real game. Then make a level impossible; it must finish and say so.
44Design eight puzzle levels, each needing one idea the level before did not need.
Check: Write the idea beside each. If the solver wins without that idea, the level does not teach it.
45Build a first level that teaches the controls without any written instructions.
Check: List every fact a newcomer must know and where the level shows it. Blank rows need work.
46Measure the time taken by updating and by drawing. Speed up only the slower one.
Check: Compare average times over 300 frames before and after. No real difference means undoing the change.
47Remove every copied block of three or more lines by making a function or a loop.
Check: Rerun all earlier checks. A cleanup that changes any behavior is not only a cleanup.
48Ask a consenting person at home to play for ten minutes while you stay silent and take notes.
Check: Separate what they did from what you think it means. Count the moments they stopped moving.
49Fix the problem from those notes that stopped play most often, and nothing else.
Check: Play the same level yourself with the old and new builds. Show the exact step that differs.
50Write a help page: controls, goal, known bugs, and the source of every sound and picture.
Check: Follow it from a fresh copy of the folder. Every missing file or unclear step counts as a fault.
51Make a second tiny game in one sitting, reusing your own input, collision and reset code.
Check: Label each reused function edited or unedited. Many edits show the old code was too specific.
52Review: build the week 15 moving shape again, adding walls and reset. Write what you would no longer do from weeks 15 to 22.
Check: Run all five overlap cases and ten resets. Write one fault you now predict before running.