Gaming in Pygame
Game Development in python using Pygame is fun. Its a library that constitutes almost all the functions that can help us create a decent game. This is the code for a game called catalicious that I created using Pygame library. Its not a very elegant game in my opinion but a fine attempt on gaming. This have given me a really strong understanding of how to create simple concept games. I was looking into KIVY library before starting this game in pygame. I found Pygame easier to implement. Although the next attempt will be in kivy, I am not sure what to comment exactly as I dont know how well KIVY library can perform as far as gaming is concerned. In my opinion KIVY is more sophisticated library as compared to Pygame in terms of more built in functions. Please let me know in comments what do you think about it.
This is the code for the game in which cat dodges obstacles and scores points. Make sure you have a cat image and background image in your folder(With exact dimensions and name of image as specified) before you try to run this code and the soundtrack should be replaced. I liked to play this game with the soundtrack of my favorite movie Tron Legacy. It feels like having a super man cat. Enjoy the game and let me know if you have any issues.
# imports
import pygame
import time
import random
import sys
from pygame.locals import *
pygame.init()
# display info
display_width = 1150
display_height = 650
size = display_width, display_height
screen = pygame.display.set_mode(size)
# colors used
black = (0, 0, 0)
white = (255, 255, 255)
red = (200, 0, 0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
orange = (251,206,177)
black_color = (53,115,255)
# text imports
largeText = pygame.font.Font('freesansbold.ttf', 115)
# image and sound imports
CatImg = pygame.image.load("Cat1.png")
background = pygame.image.load("background.jpg").convert()
background = pygame.transform.scale(background,(1280,720))
pygame.display.set_caption('Catalicious')
cat_width = 100
clock = pygame.time.Clock()
#pygame.mixer.music.load('Daft Punk - The Son of Flynn (TRON LEGACY).mp3')
#pygame.mixer.music.play(0)
screen.blit(background, [0, 0])
background_position = [0, 0]
rect=background.get_rect()
pause = False
# definitions
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(screen, black_color, [thingx, thingy, thingw, thingh])
'''def milk(milkx, milky, milkw, milkh):
milk_img = pygame.image.load("Milk_Small.png").convert()
milk_img = pygame.transform.scale(milk_img, (100,100))
screen.blit(milk_img,[0,0])'''
def things_dodged(count):
font = pygame.font.SysFont(None, 40)
text = font.render("Dodged: "+str(count), True, black)
screen.blit(text,(0,0))
def cat(x,y):
screen.blit(CatImg, (x, y))
def text_objects(text, font):
textsurface = font.render(text, True, black)
return textsurface, textsurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
screen.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
largeText = pygame.font.Font('freesansbold.ttf', 80)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width/2),(display_height/2))
screen.blit(TextSurf, TextRect)
#message_display('YOU LOST')
while True:
for event in pygame.event.type():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Play Again",385,440,100,50,green, bright_green, game_loop)
button("Quit",670,450,100,50,red, bright_red, quit)
pygame.display.update()
clock.tick(15)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x,y,w,h))
if click[0] == 1 and action != None:
if action =="play":
game_loop()
elif action == "quit":
pygame.quit()
quit()
else:
pygame.draw.rect(screen, ic,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)), (y+(h/2)))
screen.blit(textSurf, textRect)
def unpause():
global pause
pause = False
def paused():
largeText = pygame.font.Font('freesansbold.ttf', 80)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width/2),(display_height/2))
screen.blit(TextSurf, TextRect)
while pause:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
#screen.fill(orange)
button("Continue",385,440,100,50,green, bright_green, "play")
button("Quit",670,450,100,50,red, bright_red, "quit")
pygame.display.update()
clock.tick(15)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(orange)
largeText = pygame.font.Font('freesansbold.ttf', 80)
TextSurf, TextRect = text_objects("Catalicious- The Hungry Cat", largeText)
TextRect.center = ((display_width/2),(display_height/2))
screen.blit(TextSurf, TextRect)
button("Start",385,440,100,50,green, bright_green, "play")
button("Quit",670,450,100,50,red, bright_red, "quit")
pygame.display.update()
clock.tick(15)
# main game loop:
def game_loop():
global pause
x = (display_width * 0.4)
y = (display_height * 0.7)
x_change = 0
y_change = 0
#Milk_Startx = random.randrange(0, display_width)
#Milk_Starty = -300
#Milk_Speed = 5
#Milk_Width = 50
#Milk_Height = 50
thing_startx = random.randrange(0, display_width)
thing_starty = -300
thing_speed = 3
thing_width = 50
thing_height = 50
dodged = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
elif event.key == pygame.K_RIGHT:
x_change = 10
elif event.key == pygame.K_UP:
y_change = -7
elif event.key == pygame.K_DOWN:
y_change = 7
if event.key == pygame.K_p:
pause = True
paused()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
x+=x_change
y+=y_change
screen.blit(background, background_position)
#things(thingx, thingy, thingh, color):
things(thing_startx, thing_starty, thing_width, thing_height,black)
thing_starty += thing_speed
cat(x,y)
things_dodged(dodged)
if x > display_width - cat_width or x <0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0,display_width)
dodged+=1
thing_speed +=.5
thing_width += (dodged * 1.2)
if y < thing_starty+thing_height:
#''' if Milk_Starty > display_height:
# Milk_Starty = 0 - Milk_Height
# Milk_Startx = random.randrange(0,display_width)
print('y crossover')
if x > thing_startx and x < thing_startx + thing_width or x + cat_width > thing_startx and x + cat_width < thing_startx + thing_width:
#if x > Milk_Startx and x < Milk_Startx + Milk_Width or x + cat_width > Milk_Startx and x + cat_width < Milk_Startx + Milk_Width:
print('x crossover')
crash()
pygame.display.update()
clock.tick(120)
game_intro()
game_loop()
pygame.quit()
quit()
This is the code for the game in which cat dodges obstacles and scores points. Make sure you have a cat image and background image in your folder(With exact dimensions and name of image as specified) before you try to run this code and the soundtrack should be replaced. I liked to play this game with the soundtrack of my favorite movie Tron Legacy. It feels like having a super man cat. Enjoy the game and let me know if you have any issues.
# imports
import pygame
import time
import random
import sys
from pygame.locals import *
pygame.init()
# display info
display_width = 1150
display_height = 650
size = display_width, display_height
screen = pygame.display.set_mode(size)
# colors used
black = (0, 0, 0)
white = (255, 255, 255)
red = (200, 0, 0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
orange = (251,206,177)
black_color = (53,115,255)
# text imports
largeText = pygame.font.Font('freesansbold.ttf', 115)
# image and sound imports
CatImg = pygame.image.load("Cat1.png")
background = pygame.image.load("background.jpg").convert()
background = pygame.transform.scale(background,(1280,720))
pygame.display.set_caption('Catalicious')
cat_width = 100
clock = pygame.time.Clock()
#pygame.mixer.music.load('Daft Punk - The Son of Flynn (TRON LEGACY).mp3')
#pygame.mixer.music.play(0)
screen.blit(background, [0, 0])
background_position = [0, 0]
rect=background.get_rect()
pause = False
# definitions
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(screen, black_color, [thingx, thingy, thingw, thingh])
'''def milk(milkx, milky, milkw, milkh):
milk_img = pygame.image.load("Milk_Small.png").convert()
milk_img = pygame.transform.scale(milk_img, (100,100))
screen.blit(milk_img,[0,0])'''
def things_dodged(count):
font = pygame.font.SysFont(None, 40)
text = font.render("Dodged: "+str(count), True, black)
screen.blit(text,(0,0))
def cat(x,y):
screen.blit(CatImg, (x, y))
def text_objects(text, font):
textsurface = font.render(text, True, black)
return textsurface, textsurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
screen.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
largeText = pygame.font.Font('freesansbold.ttf', 80)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width/2),(display_height/2))
screen.blit(TextSurf, TextRect)
#message_display('YOU LOST')
while True:
for event in pygame.event.type():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Play Again",385,440,100,50,green, bright_green, game_loop)
button("Quit",670,450,100,50,red, bright_red, quit)
pygame.display.update()
clock.tick(15)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x,y,w,h))
if click[0] == 1 and action != None:
if action =="play":
game_loop()
elif action == "quit":
pygame.quit()
quit()
else:
pygame.draw.rect(screen, ic,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)), (y+(h/2)))
screen.blit(textSurf, textRect)
def unpause():
global pause
pause = False
def paused():
largeText = pygame.font.Font('freesansbold.ttf', 80)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width/2),(display_height/2))
screen.blit(TextSurf, TextRect)
while pause:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
#screen.fill(orange)
button("Continue",385,440,100,50,green, bright_green, "play")
button("Quit",670,450,100,50,red, bright_red, "quit")
pygame.display.update()
clock.tick(15)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(orange)
largeText = pygame.font.Font('freesansbold.ttf', 80)
TextSurf, TextRect = text_objects("Catalicious- The Hungry Cat", largeText)
TextRect.center = ((display_width/2),(display_height/2))
screen.blit(TextSurf, TextRect)
button("Start",385,440,100,50,green, bright_green, "play")
button("Quit",670,450,100,50,red, bright_red, "quit")
pygame.display.update()
clock.tick(15)
# main game loop:
def game_loop():
global pause
x = (display_width * 0.4)
y = (display_height * 0.7)
x_change = 0
y_change = 0
#Milk_Startx = random.randrange(0, display_width)
#Milk_Starty = -300
#Milk_Speed = 5
#Milk_Width = 50
#Milk_Height = 50
thing_startx = random.randrange(0, display_width)
thing_starty = -300
thing_speed = 3
thing_width = 50
thing_height = 50
dodged = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
elif event.key == pygame.K_RIGHT:
x_change = 10
elif event.key == pygame.K_UP:
y_change = -7
elif event.key == pygame.K_DOWN:
y_change = 7
if event.key == pygame.K_p:
pause = True
paused()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
x+=x_change
y+=y_change
screen.blit(background, background_position)
#things(thingx, thingy, thingh, color):
things(thing_startx, thing_starty, thing_width, thing_height,black)
thing_starty += thing_speed
cat(x,y)
things_dodged(dodged)
if x > display_width - cat_width or x <0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0,display_width)
dodged+=1
thing_speed +=.5
thing_width += (dodged * 1.2)
if y < thing_starty+thing_height:
#''' if Milk_Starty > display_height:
# Milk_Starty = 0 - Milk_Height
# Milk_Startx = random.randrange(0,display_width)
print('y crossover')
if x > thing_startx and x < thing_startx + thing_width or x + cat_width > thing_startx and x + cat_width < thing_startx + thing_width:
#if x > Milk_Startx and x < Milk_Startx + Milk_Width or x + cat_width > Milk_Startx and x + cat_width < Milk_Startx + Milk_Width:
print('x crossover')
crash()
pygame.display.update()
clock.tick(120)
game_intro()
game_loop()
pygame.quit()
quit()
Comments
Post a Comment