Setup
All you need for this project is the Getting Started Kit.
Install Pygame Zero
In order to use Pygame Zero, you first have to install it. You do this in the same way you previously installed the Phidget22 library. Simply navigate to your package manager, search for pgzero and press install!
PyCharm
If you're using PyCharm, select File > Settings > Python Interpreter and use the + symbol to install pgzero.
PyScripter
If you're using PyScripter, select Tools > Tools > Install Packages with pip and enter pgzero.
Create Project Structure
Create a python script called flappybird.py in a location of your choice. Download the required images here and place them in a folder called images in the same location as your python file.
Write code (Python)
Copy the code below into your python script flappybird.py.
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *
import pgzrun
import random
TITLE = 'Flappy Bird with Getting Started Kit'
WIDTH = 400
HEIGHT = 708
# These constants control the difficulty of the game
GAP = 130
GRAVITY = 0.3
FLAP_STRENGTH = 6.5
SPEED = 3
bird = Actor('bird1', (75, 200))
bird.dead = False
bird.score = 0
bird.vy = 0
def reset_pipes():
pipe_gap_y = random.randint(200, HEIGHT - 200)
pipe_top.pos = (WIDTH, pipe_gap_y - GAP // 2)
pipe_bottom.pos = (WIDTH, pipe_gap_y + GAP // 2)
pipe_top = Actor('top', anchor=('left', 'bottom'))
pipe_bottom = Actor('bottom', anchor=('left', 'top'))
reset_pipes() # Set initial pipe positions.
def update_pipes():
pipe_top.left -= SPEED
pipe_bottom.left -= SPEED
if pipe_top.right < 0:
reset_pipes()
if not bird.dead:
bird.score += 1
def update_bird():
uy = bird.vy
bird.vy += GRAVITY
bird.y += (uy + bird.vy) / 2
bird.x = 75
if not bird.dead:
if bird.vy < -3:
bird.image = 'bird2'
else:
bird.image = 'bird1'
if bird.colliderect(pipe_top) or bird.colliderect(pipe_bottom):
bird.dead = True
bird.image = 'birddead'
if not 0 < bird.y < 720:
bird.y = 200
bird.dead = False
bird.score = 0
bird.vy = 0
reset_pipes()
def update():
update_pipes()
update_bird()
def draw():
screen.blit('background', (0, 0))
pipe_top.draw()
pipe_bottom.draw()
bird.draw()
screen.draw.text(
str(bird.score),
color='white',
midtop=(WIDTH // 2, 10),
fontsize=70,
shadow=(1, 1)
)
#Phidgets Code Start
#Event - this will take the place of the spacebar
def onRedButton_StateChange(self, state):
if not bird.dead and state:
bird.vy = -FLAP_STRENGTH
#Create
redButton = DigitalInput()
#Address
redButton.setIsHubPortDevice(True)
redButton.setHubPort(0)
#Subscribe to Events
redButton.setOnStateChangeHandler(onRedButton_StateChange)
#Open
redButton.openWaitForAttachment(5000)
#Phidgets Code End
pgzrun.go()
Code Review
Pygame Zero uses event hooks to allow programmers to easily create games. In this example, we are simply copying the functionality of the on_key_down hook and using it in our button's state change event.
Practice
- Add new functionality to your game using the green button. Some examples may be pausing the game, ending the game, speeding the game up, slowing the game down, etc.