Welcome to the world of PhotoBomb, a delightful game designed to ignite the creativity and coding skills of young minds. Developed by TEAM X from IGNITE Pathways, this game blends fun with learning, making it an excellent tool for introducing children to the basics of programming. In this blog, we’ll walk you through the game and explain the code behind it, giving you a peek into how this engaging project comes to life.
What is PhotoBomb?
PhotoBomb is an interactive game where the player's goal is to "bomb" the photo with various stickers as quickly and accurately as possible. The player is scored based on how well they can place the stickers, testing their reflexes and precision. This engaging game encourages players to sharpen their reaction times while having a blast.
Getting Started
To begin, you'll need to clone the PhotoBomb repository from GitHub:
Once you have the repository, navigate to the project directory:
cd photobomb
Make sure you have Python installed on your machine. PhotoBomb uses several Python libraries, so you'll need to install them using.
Understanding the Code
The core of PhotoBomb is built using Python and Pygame, a set of Python modules designed for writing video games. Here’s a breakdown of the main components:
Initialization and Constants
First, the necessary libraries are imported, and constants for the screen dimensions and timer are defined:
from colors import *
import os
import sys
import pygame
import math, time, random
from enum import Enum
from pygame.locals import *
pygame.display.set_caption("Photobomb")
WIDTH = 1500
HEIGHT = 900
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
FLASH=pygame.Surface((WIDTH,HEIGHT),pygame.SRCALPHA)
Enumerations
Enumerations for places and victims are defined to make it easier to manage these options:
class Places(Enum):
Hawaii, France, India = 1,2,3
class Victims(Enum):
Shubham, Robert, Pronamee = 1,2,3
Photobomb Class
The Photobomb class encapsulates the game's logic and UI:
Initialization
The init method initializes variables, loads images and fonts, and sets up initial UI elements:
class Photobomb:
def init(self):
self.buffer = 250
self.load_assets()
self.initialize_ui_elements()
self.start_up_init()
Loading Assets
The load_assets method loads images and fonts used in the game:
def load_assets(self):
self.frame = pygame.image.load('pictureFrame.png').convert_alpha()
self.firstbg = pygame.image.load('firstbg.jpg').convert_alpha()
self.secondbg = pygame.image.load('secondbg.jpeg').convert_alpha()
self.lastbg = pygame.image.load('finalbg.png').convert_alpha()
self.cameraoverlay = pygame.image.load('cameraoverlay.png').convert_alpha()
self.load_fonts()
self.load_images()
Loading Fonts and Images
Separate methods for loading fonts and images to keep the code organized:
def load_fonts(self):
self.chalkFont = pygame.font.Font('font/Chalkduster.ttf', 50)
self.chalkFont3 = pygame.font.Font('font/Chalkduster.ttf', 35)
self.font2 = pygame.font.Font('font/IndianPoker.ttf', 75)
self.font2.set_bold(True)
self.scorefont = pygame.font.Font('font/SparkleFilled.ttf', 150)
self.montserratFont = pygame.font.Font('font/Montserrat-Light.otf', 40)
def load_images(self):
self.f1 = pygame.image.load('People/France_people1.png').convert_alpha()
self.f2 = pygame.image.load('People/France_people2.png').convert_alpha()
self.f3 = pygame.image.load('People/France_people3.png').convert_alpha()
self.h1 = pygame.image.load('People/Hawaii_people1.png').convert_alpha()
self.h2 = pygame.image.load('People/Hawaii_people2.png').convert_alpha()
self.h3 = pygame.image.load('People/Hawaii_people3.png').convert_alpha()
self.i1 = pygame.image.load('People/India_people1.png').convert_alpha()
self.i2 = pygame.image.load('People/India_people2.png').convert_alpha()
self.i3 = pygame.image.load('People/India_people3.png').convert_alpha()
self.people = [self.h1, self.h2, self.h3]
self.leftcow = pygame.image.load('cowleft.png').convert_alpha()
self.rightcow = pygame.image.load('cowright.png').convert_alpha()
Initializing UI Elements
The initialize_ui_elements method sets up initial UI text and interactive elements:
def initialize_ui_elements(self):
self.startText = self.font2.render("Ready to Photobomb?", 1, (randcol()))
self.startSize = self.font2.size("Ready to Photobomb?")
self.timer = TIMER self.cowrunning = False FLASH.fill((255, 255, 255, 32))
self.initialize_place_texts() self.initialize_victim_texts()
self.cameraUsernameLoc = (140 1500 / WIDTH, HEIGHT - (130 / 900) HEIGHT)
self.cameraUsernameRect = pygame.Rect(self.cameraUsernameLoc, (200, 50))
self.cowpos = (0, HEIGHT - 460)
Initializing Place and Victim Texts
Separate methods for setting up the text elements for places and victims:
def initialize_place_texts(self):
self.placeText = self.chalkFont3.render("Choose your location: ", 1, (green))
self.placeSize = self.chalkFont3.size("Choose your location: ")
self.place1 = self.chalkFont3.render(Places.Hawaii.name, 1, pygame.Color(cyan))
self.place1Size = self.chalkFont3.size(Places.Hawaii.name)
self.place1Loc = (700, self.buffer + 100)
self.place1Rect = pygame.Rect((self.place1Loc[0] - 30, self.place1Loc[1]), (self.place1Size[0] + 30, self.place1Size[1]))
self.place2 = self.chalkFont3.render(Places.France.name, 1, pygame.Color(cyan))
self.place2Size = self.chalkFont3.size(Places.Hawaii.name)
self.place2Loc = (900, self.buffer + 100)
self.place2Rect = pygame.Rect((self.place2Loc[0] - 30, self.place2Loc[1]), (self.place2Size[0] + 30, self.place2Size[1]))
self.place3 = self.chalkFont3.render(Places.India.name, 1, pygame.Color(cyan))
self.place3Size = self.chalkFont3.size(Places.Hawaii.name)
self.place3Loc = (1100, self.buffer + 100)
self.place3Rect = pygame.Rect((self.place3Loc[0] - 30, self.place3Loc[1]), (self.place3Size[0] + 30, self.place3Size[1]))
def initialize_victim_texts(self):
self.vicText = self.chalkFont3.render("Choose your vacation buddy: ", 1, (green))
self.vicSize = self.chalkFont3.size("Choose your vacation buddy: ")
self.vic1 = self.chalkFont3.render(Victims.Shubham.name, 1, pygame.Color(cyan))
self.vic1Size = self.chalkFont3.size(Victims.Shubham.name)
self.vic1Loc = (750, self.buffer + 200)
self.vic1Rect = pygame.Rect((self.vic1Loc[0] - 30, self.vic1Loc[1]), (self.vic1Size[0] + 30, self.vic1Size[1]))
self.vic2 = self.chalkFont3.render(Victims.Robert.name, 1, pygame.Color(cyan))
self.vic2Size = self.chalkFont3.size(Victims.Robert.name)
self.vic2Loc = (1000, self.buffer + 200)
self.vic2Rect = pygame.Rect((self.vic2Loc[0] - 30, self.vic2Loc[1]), (self.vic2Size[0] + 30, self.vic2Size[1]))
self.vic3 = self.chalkFont3.render(Victims.Pronamee.name, 1, pygame.Color(cyan))
self.vic3Size = self.chalkFont3.size(Victims.Pronamee.name)
self.vic3Loc = (1200, self.buffer + 200)
self.vic3Rect = pygame.Rect((self.vic3Loc[0] - 30, self.vic3Loc[1]), (self.vic3Size[0] + 30, self.vic3Size[1]))
Startup Initialization
Setting up initial UI states and elements:
def start_up_init(self):
self.startButton = self.font2.render(" Start ", 1, black)
self.startButtonSize = self.font2.size(" Start ")
self.startButtonLoc = (WIDTH / 2 - self.startButtonSize[0] / 2, HEIGHT / 3 - self.startButtonSize[1] / 2)
self.startButtonRect = pygame.Rect(self.startButtonLoc, self.startButtonSize)
self.startButtonRectOutline = pygame.Rect(self.startButtonLoc, self.startButtonSize)
self.playButton = self.font2.render(" Play ", 1, black)
self.playButtonSize = self.font2.size(" Play ")
self.playButtonLoc = (WIDTH / 2 - self.playButtonSize[0] / 2, HEIGHT - self.buffer + 20 - self.playButtonSize[1] / 2)
self.playButtonRect = pygame.Rect(self.playButtonLoc, self.playButtonSize)
self.playButtonRectOutline = pygame.Rect(self.playButtonLoc, self.playButtonSize)
self.selectedPlace = Places.Hawaii
self.thirdbg = pygame.image.load('hawaii.jpeg').convert_alpha()
self.selectedVictim = Victims.Shubham
self.userCamera = self.montserratFont.render(self.selectedVictim.name + "'s Camera", 1, (white))
self.state = 0
self.score = 0
Main Game Loop
The main method determines which part of the game to run based on the current state:
def main(self):
if self.state == 0:
self.show_splash_screen()
elif self.state == 1:
self.select_loc_and_victim()
elif self.state == 2:
self.play()
elif self.state == 3:
self.show_results_screen()
Running the Game
The entry point of the program that initializes and runs the game loop:
if name == "__main__":
os.environ['SDL_VIDEO_CENTERED'] = '1' # center SCREEN
pygame.init()
pygame.display.set_caption("Photobomb")
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
Runit = Photobomb()
Myclock = pygame.time.Clock()
while 1:
Runit.main()
Myclock.tick(64)
PhotoBomb is a fantastic way to engage players in coding while allowing them to express their creativity and test their reflexes. By understanding the code and its structure, players can gain valuable skills in programming and game development. We hope you enjoy playing and learning with PhotoBomb. Happy coding!
Feel free to check out our PhotoBomb GitHub repository for the complete code and additional resources.
Comments