Finding Pi - the Py Game That Teaches You the Digits of Pi and How to Make Make Pie by Playing Py Game Made Using Pygame and Py(thon) on (rasberry) Pi

by jetcrafts in Craft > Digital Graphics

61 Views, 2 Favorites, 0 Comments

Finding Pi - the Py Game That Teaches You the Digits of Pi and How to Make Make Pie by Playing Py Game Made Using Pygame and Py(thon) on (rasberry) Pi

Screenshot 2025-04-05 181938.png

this is a game i made at my grandparents place for pi day , I really hope u enjoy.


you play as pi running from approximation, which will kill you ,

u run around trying to pick back up all you're digits while getting chased

after picking up 6, an oven and then ingredients start to spawn

u use these to craft pies for power ups

to craft them you go to the pie in the middle

it gives you recipes for the fillings and crusts

use these to bake pies which give u power ups

apple pie gives you 5 more lives

lemon meringue pie gives u double speed

and raspberry pie gives u the ability to pin the approximation symbol down for 5 seconds

to win u must bake all the pie,s and collect the first 30 digits of pi


Supplies

  1. a python ide
  2. install pygame library

How to Run

Screenshot 2025-04-05 201230.png

https://github.com/jectrafts/finding-pi


this is the github with all the codes and sprites ( pls follow me)( I didn't upload code here cause its 600 lines and there are like 7 sprite images )

go to you're ide,s terminal and install pygame:

one way is using pip install:

pip install pygame


all u hv to do is download it as a zip file and unzip and run main.py

Gameplay

you use WASD or arrow keys to move

once you collect the first 6 numbers

a oven will spawn, pick it up

head to the pie in the middle which pauses the game and gives you recipes to make pies, to leave the pie click space

learn the recipes and collect ingredients around you to bake pies by going into the pie in the middle

you first have to make pie crust and then the fillings

you hv to bake apple pie first which gives you 5 more lives

then lemon pie which gives u 2x speed

and then rasberry pie which lets u pause time for 5 seconds


to win u must bake all pie,s and collect all 30 digits of pi


How Does This Game Even Work

Imports and Initialization

import pygame
import random
import math
import time

pygame.init()
screen = pygame.display.set_mode((1200, 700))
WIDTH, HEIGHT = screen.get_size()
pygame.display.set_caption("Square Pacman - Numbers & Chaser")


Imports:

  1. pygame: Core library for game development.
  2. random: For spawning objects randomly.
  3. math: For distance calculations (e.g., hypot).
  4. time: For delays (e.g., countdown on replay).

Initialization:

  1. Initializes Pygame.
  2. Creates a 1200x700 pixel window.
  3. Sets the window title to "Square Pacman - Numbers & Chaser".
  4. Stores screen dimensions in WIDTH and HEIGHT.

Colors

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
GRAY = (100, 100, 100)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
ORANGE = (255, 165, 0)
PURPLE = (128, 0, 128)
BROWN = (139, 69, 19)
YELLOW_BROWN = (153, 102, 51)
DARK_YELLOW = (204, 153, 0)
PINK_RED = (199, 21, 133)


setting the color


Image Loading

player_img = pygame.image.load("player.png").convert_alpha()
player_img = pygame.transform.scale(player_img, (30, 30))
chaser_img = pygame.image.load("chaser.png").convert_alpha()
chaser_img = pygame.transform.scale(chaser_img, (30, 30))

try:
pie_img = pygame.image.load("pie.png").convert_alpha()
pie_img = pygame.transform.scale(pie_img, (50, 50))
pie_rect = pie_img.get_rect(center=(WIDTH//2, HEIGHT//2))
except:
pie_img = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.circle(pie_img, BLUE, (25, 25), 25)
text = pygame.font.SysFont(None, 30).render("π", True, WHITE)
pie_img.blit(text, (25 - text.get_width()//2, 25 - text.get_height()//2))
pie_rect = pie_img.get_rect(center=(WIDTH//2, HEIGHT//2))

oven_img = pygame.image.load("oven.png").convert_alpha() if pygame.image.get_extended() else None
if oven_img:
oven_img = pygame.transform.scale(oven_img, (40, 40))
else:
oven_img = pygame.Surface((40, 40), pygame.SRCALPHA)
pygame.draw.rect(oven_img, (139, 69, 19), (0, 0, 40, 40))
pygame.draw.rect(oven_img, (100, 100, 100), (10, 10, 20, 20))

try:
background_img = pygame.image.load("background.png").convert()
background_img = pygame.transform.scale(background_img, (WIDTH, HEIGHT))
except:
background_img = pygame.Surface((WIDTH, HEIGHT))
background_img.fill(BLACK)

try:
start_background_img = pygame.image.load("start_background.png").convert()
start_background_img = pygame.transform.scale(start_background_img, (WIDTH, HEIGHT))
except:
start_background_img = pygame.Surface((WIDTH, HEIGHT))
start_background_img.fill(BLACK)

pygame.mixer.music.load("background_music.mp3")

Player and Chaser: Loads player.png and chaser.png, scales them to 30x30 pixels, and uses alpha transparency.

Pie: Loads pie.png (50x50), centers it on-screen. If it fails, draws a blue circle with a white π symbol as a fallback.

Oven: Loads oven.png (40x40) if supported, otherwise draws a brown rectangle with a gray center.

Backgrounds:

  1. background.png for gameplay, scaled to 1200x700, with a black fallback.
  2. start_background.png for the start screen, scaled similarly, with a black fallback.

Music: Loads background_music.mp3 for playback during gameplay.



Token Images


token_images = {}
for token in ["flour", "sugar", "butter", "water", "apple", "cinnamon", "lemon", "corn", "egg", "raspberry"]:
try:
img = pygame.image.load(f"{token}.png").convert_alpha()
token_images[token] = pygame.transform.scale(img, (20, 20))
except:
img = pygame.Surface((20, 20), pygame.SRCALPHA)
pygame.draw.circle(img, WHITE, (10, 10), 10)
token_images[token] = img

Loads images for each token type (e.g., flour.png), scales them to 20x20. If missing, uses a white circle as a fallback. Stored in a dictionary token_images.

Fonts and Clock

font = pygame.font.SysFont(None, 24)
large_font = pygame.font.SysFont(None, 48)
xlarge_font = pygame.font.SysFont(None, 64)
countdown_font = pygame.font.SysFont(None, 72)
bottom_digits_font = pygame.font.SysFont(None, 50)
recipe_font = pygame.font.SysFont(None, 24)

clock = pygame.time.Clock()

Defines various font sizes for different UI elements (e.g., font for general text, xlarge_font for "Next" digit).

clock controls the frame rate (later set to 60 FPS).


Game Data

pi_sequence = list("3.1415926535 8979323846 2643383279")
pi_digits = [d for d in pi_sequence if d.isdigit()]
current_digit_index = 0

easy_digit_size = 32
normal_digit_size = 24

last_collected_point = [0, 0, '']
oven_spawned = False
oven_pos = [0, 0]
oven_acquired = False
oven_message_time = 0

token_types = {
"flour": {"max": 11, "color": WHITE},
"sugar": {"max": 39, "color": WHITE},
"butter": {"max": 3, "color": YELLOW},
"water": {"max": 5, "color": BLUE},
"apple": {"max": 4, "color": GREEN},
"cinnamon": {"max": 1, "color": ORANGE},
"lemon": {"max": 4, "color": YELLOW},
"corn": {"max": 5, "color": YELLOW},
"egg": {"max": 2, "color": WHITE},
"raspberry": {"max": 4, "color": PINK_RED}
}

recipes = {
"pie_crust": {"flour": 3, "sugar": 1, "butter": 1, "water": 1},
"apple_filling": {"apple": 4, "sugar": 8, "cinnamon": 1, "flour": 2, "lemon": 1},
"lemon_filling": {"corn": 2, "sugar": 16, "water": 3, "lemon": 2, "egg": 2},
"raspberry_filling": {"corn": 3, "raspberry": 4, "sugar": 12, "lemon": 1}
}

pies = {
"apple_pie": {"pie_crust": 1, "apple_filling": 1},
"lemon_pie": {"pie_crust": 1, "lemon_filling": 1},
"raspberry_pie": {"pie_crust": 1, "raspberry_filling": 1}
}

collected_tokens = {token: 0 for token in token_types}
crafted_items = {"pie_crust": 0, "apple_filling": 0, "lemon_filling": 0, "raspberry_filling": 0,
"apple_pie": 0, "lemon_pie": 0, "raspberry_pie": 0}
tokens_on_map = []

Pi Digits: pi_sequence is a string of π digits; pi_digits filters out non-digits. current_digit_index tracks progress.

Difficulty: easy_digit_size (32) and normal_digit_size (24) adjust digit font size per level.

Game State: Tracks last collected point, oven status, and message timing.

Tokens: token_types defines max quantities and colors for each token.

Recipes: Defines ingredients for intermediate items (pie_crust, fillings) and final pies (apple_pie, etc.).

Inventory: collected_tokens tracks gathered tokens; crafted_items tracks crafted items; tokens_on_map lists active tokens.


Functions

  1. Spawning Functions:
  2. spawn_point_with_specific_digit(digit, existing_points): Spawns a digit at a random position, avoiding player, last point, pie, and other points.
  3. spawn_oven(): Spawns the oven away from the player and pie.
  4. spawn_token(existing_tokens): Spawns a token if under its max limit, avoiding collisions.
  5. Movement Functions:
  6. teleport_chaser_away(): Moves chaser far from player.
  7. move_player_away_from_point(point, distance=5): Nudges player away from a collected point.
  8. teleport_player_from_pie(): Moves player away from the center pie when hit.
  9. Crafting Functions:
  10. can_craft(item): Checks if an item can be crafted based on available tokens or components, with pie prerequisites (e.g., apple pie before lemon).
  11. craft_item(item): Deducts required resources and increments crafted count, applying effects (e.g., +5 lives for apple pie).
  12. Reset Game:
  13. reset_game(): Resets all game variables to initial states, spawns 5 initial digits.
  14. Text Wrapping:
  15. wrap_text(text, font, max_width): Splits text into lines that fit within max_width, used for "How to Play" and win messages.

Initial Game State


reset_game()
running = True
game_over = False
game_won = False
paused = False
in_start_menu = True
in_how_to_play = False
selected_level = 1
chaser_speed = 2
digit_font_size = normal_digit_size
chaser_reverse_time = 0
death_confirmation = False
wrong_digit_point = None
hit_pie = False
music_playing = False

Initializes game state variables, starting in the menu with music off.


Main Game Loop

while running:
current_time = pygame.time.get_ticks()
  1. Runs until running = False. current_time tracks milliseconds for timers.

Event Handling

  1. Quit: Closes game on window close or ESC, stopping music.
  2. Space: Toggles pause, teleports player from pie if hit.
  3. P Key: Pauses chaser for 5 seconds if raspberry pie is crafted.
  4. Mouse Clicks:
  5. Start menu: Selects levels (1-3, setting speed/font), "How to Play", or "Leave" (stops music).
  6. How to Play: "Back" to menu.
  7. Death confirmation: "Yes" (to menu, stops music) or "No" (resumes).
  8. Paused: Crafts items if oven acquired.
  9. Win screen: "Replay" (countdown, resets) or "Main Menu" (stops music).

Rendering and Logic

  1. Start Menu:
  2. Displays start_background_img.
  3. Shows title, level buttons, and controls text.
  4. How to Play:
  5. Black background.
  6. Wrapped instructions text and "Back" button.
  7. Gameplay (not game_over and not game_won):
  8. Active State:
  9. Displays background_img.
  10. Handles player movement (WASD/arrows), collision with pie (pauses), chaser movement (reverses if hits pie), digit collection (score up or death if wrong), token collection, oven spawning/acquisition, and pie crafting effects.
  11. Updates UI (score, lives, next digit, collected digits).
  12. Death Confirmation: Black background, asks to die or continue.
  13. Paused: Shows background_img, game state frozen, displays recipes and crafting options.
  14. Win Screen:
  15. Black background.
  16. Shows "You Won!", wrapped win message, π digits, and replay/menu buttons.



Display and Frame Rate

pygame.display.flip()
clock.tick(60)
pygame.quit()

Updates the screen and limits to 60 FPS. Quits Pygame on exit.















Hope U Enjoy

i hope u try this out and enjoy this


a little birdy told me the win screen has something cool