Rhythm Game with Pygame Zero and Phidgets

Pygame Zero allows you to create 2D games in Python. In this project, you will create a Rhythm game.

Prerequisites

You should review the following before moving on:

Setup

All you need for this project is the Getting Started Kit.

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!

Thonny

If you're using Thonny, select Tools > Manage Packages and search for pgzero.

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 rhythm.py in a location of your choice.

Download the required images 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 rhythm.py.


#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *

import pgzrun

WIDTH = 500
HEIGHT = 500
SPEED = 5

#Define Actors
red_target = Actor('red_button')
red_target.pos = ((WIDTH/2) - 100), HEIGHT - 150

green_target = Actor('green_button')
green_target.pos = ((WIDTH/2) + 100), HEIGHT - 150

red_circle = Actor('red_circle')
red_circle.pos = (red_target.x,0)

#Used for PERFECT, GOOD, MISS text
status_string = ""

def draw():
    screen.clear()
    screen.fill((255,255,255))
    red_target.draw()
    green_target.draw()
    red_circle.draw()
    screen.draw.text(status_string, center=(red_target.x, red_target.y + 100), color="red",fontsize=50)    
    
def update():
    #move red circle down the screen
    red_circle.y += SPEED
    
    #if circle has reached bottom, move it to top
    if(red_circle.y == HEIGHT + red_circle.height):
        red_circle.y = 0    
    
#Phidgets Code Start
#Button Event
def onRedButton_StateChange(self, state):
    global status_string
    
    #When button is pressed
    if(state):
        #Compare y-position to judge accuracy 
        accuracy = abs(red_target.y - red_circle.y)
        if(accuracy < 10):
            status_string = "PERFECT"
        elif(accuracy < 30):
            status_string = "GOOD"
        else:
            status_string = "MISS"
        
#Create, Address, Subscribe to Events and Open
redButton = DigitalInput()
redButton.setIsHubPortDevice(True)
redButton.setHubPort(0)
redButton.setOnStateChangeHandler(onRedButton_StateChange)
redButton.openWaitForAttachment(1000)
#Phidgets Code End    
    
pgzrun.go()
  

Run Your Program

You will see a red circle fall towards the red button on screen. Press your red button when the circle is in the correct position.

Code Review

The circle is continually repositioned in the draw method. When the player presses the red button, it captures the y-position of the circle and compares it to the y-position of the target. Depending on how accurate the player is, they will see PERFECT, GOOD or MISS.

Practice

  1. Try adding a scoring system. Display the score in the middle of the screen.
    • Perfect: +2 points
    • Good: +1 point
    • Miss: -1 point
  2. Add code so the green circle falls towards the green button.
  3. Modify your code so that multiple circles are on the screen at one time. Note: you will have to figure out which circle is closest to the target in order to score your game.
  4. Bonus: Create levels that are based on different songs. Try to have the circles fall to the beat of the song.

What are Phidgets?

Phidgets are programmable USB sensors. Simply plug in your sensor, write code in your favorite language and go!

Phidgets have been used by STEM professionals for over 20 years and are now available to students.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done