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()

Trees with Turtle in Python

Introduction

Make fractal trees using Python and Turtle.

Source code is available on https://github.com/dojojon/py_turtle/blob/master/tree.py

What you will make

Use loops and recursion to create fractal trees.

Steps

  1. Open the blank Python template Trinket: jumpto.cc/python-new.

  2. Add the following:

    1
    from turtle import *
  3. Add the following code to point our turtle up the canvas.

    1
    setheading(90)
  1. We are going to define a function that gets calls its self to draw the branches. It will set the branch color and draw the first branch. Add the following to define a function we can call.

    1
    2
    3
    def draw_branch():
    color("brown")
    forward(40)
  2. Below our new function add the following to call the function:

    1
    draw_branch()
  3. Our tree is made up of branches that get smaller and smaller, lets add an parameter to this function for the length. Update the function so it looks like this:

    1
    2
    3
    def draw_branch(len):
    color("brown")
    forward(len)
  4. And update where we call the function:

    1
    draw_branch(40)
  5. Next are going to draw two more branches. First we want to rotate the turtles heading, then we will call our draw branch function again. Update the draw branch code as follows:

    1
    2
    3
    4
    5
    def draw_branch(len):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len)
  6. When you run the code you end up with a circle and a turtle that never stops. Lets fix that. Each time we draw a branch, lets make it shorter that the one before. Update the code as follows:

    1
    2
    3
    4
    5
    def draw_branch(len):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len - 5)
  7. Looking a little better, but we need to tell the turtle to stop drawing branches when they get too small. We can do that with an if statement:

    1
    2
    3
    4
    5
    6
    def draw_branch(len):
    if (len > 5):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len - 5)
  8. Lots better now, lets update the code to draw the other branch. So after we have draw a branch we want the turtle to go back to the start of the branch before drawing the next:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    def draw_branch(len):
    if (len > 5):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len - 5)
    left(50)
    draw_branch(len - 5)
    right(25)
    backward(len)
  9. You should see a tree something like this:

  10. You can make thicker branches by using the following:

    1
    pensize(5)
  11. You can speed up the drawing by adding the following to the top of the script:

    1
    speed(0)
  12. Challenge: Try changing the shape of the by altering the length of the branches with a random number:

    Add the following to import the the random function

    1
    from random import randint

    Then update the draw_branch function to change how much we shorten each branch.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    def draw_branch(len):
    if (len > 5):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len - randint(1,10))
    left(50)
    draw_branch(len - randint(1,10))
    right(25)
    backward(len)
  13. Challenge: Try changing the shape of the by altering the angle between the branches. Remember to make sure you rotate back the same amount each time.

  14. Challenge: Try adding leaves to the end of the branch. You can draw a filled circle using the following:

    1
    2
    3
    4
    color("green", "green")
    begin_fill()
    circle(10)
    end_fill()