Pizza Hunt - Pygame

Introduction

Simple Pygame Zero game where the player has 10 seconds to collect as many pizza slices as possible.

Set Up

Details on Pygame Zero can be found here. https://pygame-zero.readthedocs.io/en/stable/

Source Code

Source code can be found https://github.com/dojojon/pygame_2020/tree/master/chase_pizza.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pgzrun
from random import randint

WIDTH = 800
HEIGHT = 600

PLAYER_SPEED = 5

countdown = 10
score = 0

player = Actor('alien')
player.pos = (WIDTH/2, HEIGHT/2)

pizza = Actor('pizza')
pizza.pos = (randint(0, WIDTH), randint(0, HEIGHT))

def update(dt):

global countdown
countdown -= dt

if countdown < 0:
countdown = 0
else:
update_player()

def update_player():

if keyboard[keys.LEFT]:
player.left -= PLAYER_SPEED
elif keyboard[keys.RIGHT]:
player.left += PLAYER_SPEED

if keyboard[keys.UP]:
player.top -= PLAYER_SPEED
elif keyboard[keys.DOWN]:
player.top += PLAYER_SPEED

if player.colliderect(pizza):
eat_pizza()

def draw():
screen.clear()
player.draw()
pizza.draw()

screen.draw.text("Pizza Hunt", midtop=(WIDTH/2,10), align="Center", fontsize=60, color="Red" )

screen.draw.text(str(score), (5,5), fontsize=50)
screen.draw.text('{0:.2f}'.format(countdown), topright=(WIDTH-5, 5), fontsize=50)

if countdown == 0:
screen.draw.text("Game Over", midtop=(WIDTH/2,100), align="Center", fontsize=160, color="Orange" )
screen.draw.text("Score " + str(score), midtop=(WIDTH/2,220), align="Center", fontsize=50, color="Yellow" )

def eat_pizza():
global score
print("Nom Nom Nom")
pizza.pos = (randint(0, WIDTH), randint(0, HEIGHT))
score += 1


pgzrun.go()