Crafting intuitive digital experiences that connect and inspire.
Our Pygame
‘Dodge a star’ is a game of avoidance and catching. The aim of the game is for the player to move the girl across the game screen using the left and right arrow keys, to avoid the falling stars and catch the bunch of gifts. For each star the player dodges they earn one point and for each gift caught they earn one point. The game is over once the player gets hit by a star with a prompt screen then appearing to ask the player if they want to play again (press S) or quit (press Q).
Keys
> = move right
< = move left
Space = game pause
S = game start
Q = game quit


Simple graphic assets drawn by me in Microsoft OneNote:




Python (3.6) code
import pygame
import time
import random
pygame.init()
pygame.mixer.music.load("Jazz.wav")
bell = pygame.mixer.Sound("Bell.wav")
# Define the colors
black = ( 0,0,0)
pinkbeige = ( 247, 216, 220)
pink = ( 208,32,144)
lightblue = ( 198,226,255)
red = ( 205,79,57)
sky = ( 99,184,255)
indianred = ( 178,34,34)
beige = ( 255,250,178)
lightgreen = (222,255,178)
purple = (167,102,255)
border = 10
#Detail for sprite
girl_width, girl_height = 77, 166
#Defining fonts
smallfont = pygame.font.SysFont("comicsansms", 30)
medfont = pygame.font.SysFont("comicsansms", 45 )
largefont = pygame.font.SysFont("impact", 65, bold=True)
XLfont = pygame.font.SysFont("comicsansms", 80, bold=True)
Pause = False
#Display screen detail
display_width, display_height = 900, 600
screen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Dodge a Star")
#Images
clock = pygame.time.Clock()
girlImg = pygame.image.load('girl1.png')
cloudImg = pygame.image.load('cloud1.png')
grassImg = pygame.image.load('grass.png')
starImg = pygame.image.load('star.png')
giftImg = pygame.image.load('gift.png')
x = (display_width*0.001)
y = (display_height*0.70)
#-------------------Definitions------------------#
def things_dodged(count):
font = pygame.font.SysFont(None, 35)
text = font.render("Dodged: "+str(count), True, red)
screen.blit(text, (0, 0))
def gifts_caught(count):
font = pygame.font.SysFont(None, 35)
text = font.render("Christmas Bonus: "+str(count), True, sky)
screen.blit(text, (150, 0))
def things(thingx, thingy, thingw, thingh):
screen.blit(starImg, (thingx, thingy, thingw, thingh))
def gifts(giftsx, giftsy, giftsw, giftsh):
screen.blit(giftImg, (giftsx, giftsy, giftsw, giftsh))
def girl(x,y):
screen.blit(girlImg, (x,y))
def cloud(x,y):
screen.blit(cloudImg, (display_width*0.001,display_height*0.50)) #1st cloud
screen.blit(cloudImg, (display_width*0.25,display_height*0.15)) #2nd cloud
screen.blit(cloudImg, (display_width*0.5,display_height*0.25)) #3rd cloud
screen.blit(cloudImg, (display_width*0.75,display_height*0.25)) #4th cloud
def grass(x,y):
screen.blit(grassImg, (display_width*0.001, display_height*0.3))
def text_objects(text, color, size):
if size == "small":
textSurface = smallfont.render(text, True, color)
elif size == "medium":
textSurface = medfont.render(text, True, color)
elif size == "large":
textSurface = largefont.render(text, True, color)
elif size == "XLfont":
textSurface = XLfont.render(text, True, color)
return textSurface, textSurface.get_rect()
def message_display(msg, color, y_displace=0, size = "small"):
textSurf, textRect = text_objects(msg, color, size)
textRect.center = (display_width/2), (display_height/2) + y_displace
screen.blit(textSurf, textRect)
def paused():
while paused:
pygame.mixer.music.stop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key ==pygame.K_c:
pygame.mixer.music.play()
return
if event.key == pygame.K_q:
pygame.quit()
quit()
screen.fill(pinkbeige)
grass(x,y)
cloud(x,y)
message_display("Paused", pink, -100, "large")
message_display("Press C to continue or Q to quit.", indianred, 250, "medium")
pygame.display.update()
clock.tick(15)
#----------------------Game menu-----------------------#
def game_intro(): #intro screen
pygame.mixer.music.play(-1) #music plays on infinite loop
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key ==pygame.K_s: #if the letter S is pressed the game starts
intro = False
if event.key == pygame.K_q: #if the letter Q is pressed the game quits
pygame.quit()
quit()
#messages displayed
screen.fill(beige) #Background colour
grass(x,y)
cloud(x,y)
screen.blit(starImg, (display_width*0.001,display_height*0.50))
screen.blit(starImg, (display_width*0.85,display_height*0.25))
message_display("Welcome to Dodge a Star!", pink, -190, "large")
message_display("The aim of the game is to dodge the stars", pink, -90, "small")
message_display("and catch the presents !", pink, -50, "small")
message_display("Use the right and left keys to move the player", pink, -10, "small")
#message_display("~The more you catch and dodge, the more you win~", purple, 120)
message_display("Press S to start or Q to quit.", indianred, 250, "medium")
pygame.display.update()
clock.tick(15)
#------------------------Game begins------------------------#
def game_loop():
#position where girl starts
x = (display_width*0.001) #Go lower to go lower
y = (display_height*0.70) #Go higher to go lower
x_change = 0
#starting and details of the stars
thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 5
thing_width = 80
thing_height = 100
dodged = 0
#starting and detail of thr gifts
gifts_startx = random.randrange(0, display_width)
gifts_starty = -500
gifts_speed = 5
gifts_width = 40
gifts_height = 50
caught = 0
gameExit = False
gameOver = False
while not gameExit: #event handling loop
while gameOver == True:
screen.fill(lightgreen)
grass(x,y)
cloud(x,y)
#Message for game over screen
message_display("Game over :(",
indianred,
y_displace=-200,
size="XLfont")
message_display("You dodged " +str(dodged)+" boxes!",
purple, -100,
size="medium")
message_display("You've got " +str(caught)+" Christmas bonuses!",
purple, -50,
size="medium")
message_display("Press S to start again or Q to quit.",
indianred, 250,
size="medium")
pygame.display.update()
#instructions for the keys, S = start, Q = quit
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
quit()
if event.key == pygame.K_s:
game_loop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#instuction for sprite to move left and right
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
#instuctions for pause
if event.key == pygame.K_SPACE:
pause = True
paused()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x+= x_change #changing variable
# prevent running off screen
x = min(x,display_width-girl_width)
x = max(x,0)
#--------------------drawing area---------------------#
#order of display
screen.fill(lightblue)
grass (x,y)
cloud (x,y)
things_dodged(dodged)
gifts_caught(caught)
girl(x,y)
#Displays the stars
things(thing_startx, thing_starty, thing_width, thing_height)
thing_starty += thing_speed #increases speed
#Displays the gifts
gifts(gifts_startx, gifts_starty, gifts_width, gifts_height)
gifts_starty += gifts_speed #increases speed
#---------------collision logic statement-----------------#
#Sprite (star) to return to the screen after falling
if thing_starty > display_height:
thing_starty = 0 - thing_height #come back
thing_startx = random.randrange(0, display_width-thing_width)
dodged +=1 #count increases as dodged
thing_speed += 0.4 #increases speed incremently
#Collision of star and the girl
if y < thing_starty+thing_height:
if x > thing_startx and x < thing_startx + thing_width or x + girl_width > thing_startx and x + girl_width < thing_startx + thing_width:
gameOver = True
#Sprite (gift) to return to the screen after falling
if gifts_starty > display_height:
gifts_starty = 0 - gifts_height
gifts_startx = random.randrange(0, display_width-gifts_width)
gifts_speed += 0.2
#Collision of girl and gifts
if y+girl_height > gifts_starty and y < gifts_starty+gifts_height: # checks y crossover
if x+girl_width > gifts_startx and x < gifts_startx+gifts_width:
gifts_starty = 0 - gifts_height
gifts_startx = random.randrange(0, display_width-gifts_width)
gifts_speed += 0.2
caught += 1
bell.play()
pygame.display.update()
clock.tick(60)
#running of game
game_intro()
game_loop()
pygame.quit()
quit()