Build Simple Games with Python

For beginners, game creation delivers the "it works!" feeling faster than almost anything else. With Python, you can build a genuinely playable game in 30–100 lines of code. This article walks through three games that any beginner can finish today — a number guessing game, rock-paper-scissors, and a typing practice game — with diagrams showing exactly how they work.

Why Python for Games?

Python's short syntax means the code for game rules is also short. Browser-based tools like Google Colab and Replit let you run code instantly without installing anything. You can't easily build a 3D game, but for text-based games Python's basics — input, if statements, for loops, and random numbers — are more than enough.

If you want to animate sprites later, add the pygame library. But for your first game, text only is fine. Go get that "it works!" experience first.

How Games Work: 3 Common Patterns

Number Guessing Game: Code ↔ Terminal Output (4-step walkthrough) Every game is some combination of "input → judge → result → repeat" guess_game.py import random answer = random.randint(1, 100) count = 0 while True: ① Receive input g = int(input("数字: ")) count += 1 ② Judge if g < answer: print("もっと大きい") elif g > answer: print("もっと小さい") ③ Result + ④ Exit loop else: print("正解!"); break ▶ Terminal output $ python guess_game.py 数字: 50 Too high 数字: 75 Too low 数字: 62 Too high 数字: 68 Too low 数字: 65 Correct! Solved in 5 tries Theoretically solvable in ≤7 tries
Fig. 1: Number guessing game code and terminal output. Steps ①–④ in the code map to "input → judge → result → repeat."

Every game, when you break it down, is some combination of "input → judge → result → repeat." Once you understand this pattern, you can produce new games just by swapping out the rules.

① Number Guessing Game (30 lines)

Python picks a random number from 1 to 100, and the player enters guesses. After each guess, Python replies "higher" or "lower." The whole game is 30 lines.

import random
answer = random.randint(1, 100)
count = 0
while True:
    guess = int(input("1〜100の数字を入れてね: "))
    count += 1
    if guess < answer:
        print("もっと大きい!")
    elif guess > answer:
        print("もっと小さい!")
    else:
        print(f"正解!{count}回でクリア")
        break

All you need is input, int, random, while, and if — just five concepts. There's even a satisfying math fact: you can always win in 7 tries or fewer if you use the optimal strategy.

② Rock-Paper-Scissors (40 lines)

The player enters rock, paper, or scissors; the computer picks randomly; the code decides who wins. Perfect practice for lists and if statements.

Rock-Paper-Scissors Result Table (You → CPU) CPU: Rock CPU: Scissors CPU: Paper You: Rock Draw Win Lose You: Scissors Lose Draw Win You: Paper Win Lose Draw
Fig. 2: Rock-paper-scissors result table for encoding the win/lose logic.

Encode the table above as if statements. Once you're comfortable, extend the game: "first to 3 wins," "show the win rate," or even "make the CPU learn from patterns."

③ Typing Game (80 lines)

A word appears on screen; the player types it; the code measures how long it took and assigns a score. Use the time module to measure elapsed time and pick words randomly from a list. It doubles as actual typing practice, so family and friends will genuinely want to use it.

Text-only games are perfect starters, but keep improving after you finish. Add a counter to the guessing game, display the win rate in rock-paper-scissors, load typing questions from a CSV file. Adding one feature at a time is what makes variables, lists, functions, and file I/O click naturally.

Game design also means thinking about bad inputs. What happens if the user types a word where a number was expected? What if they just press Enter without typing anything? What command exits the game? Thinking through these edge cases is the step that turns "code that runs" into "a program someone else can actually use."

Common Pitfalls to Watch For

3 things beginners get stuck on
  • input() always returns a string, so you need to wrap it in int() before comparing it as a number.
  • Forgetting the break inside a while True loop — the game runs forever and you have to force-quit.
  • Trying to write perfect code on the first try. The correct order is: run → fix → run again, 10 times over.

How Will This Help in the Future?

Building games trains you to think from the user's perspective — "how will someone actually interact with this?" That mindset is essential in every type of software development: web apps, mobile apps, business tools. Practicing "build with the player in mind" as a teen gives you a head start that shows up throughout your career.

Python games also make strong first portfolio pieces. Write a README covering how to play, what syntax you used, what you found interesting, and what you'd add next, and it transforms from practice code into a "finished work." Even a short game that you can explain from start to finish counts.

What You Can Do Starting Today

Get started in 3 steps
  1. Open a new Python notebook in Google Colab or Replit.
  2. Copy the number guessing game code above and run it.
  3. Make one modification — change the range to 1–1000, or add a guess limit — and see what happens.

Summary

Building games with Python is one of the fastest ways to absorb the fundamentals. Once you understand the "input → judge → result → repeat" pattern, new games are just a matter of swapping the rules. Start with the number guessing game, then try rock-paper-scissors, then a typing game — and let your friends play each one.