Pygame zero: another example

Pygame zero

pygame zero
pygame zero

In a recent article about pygamezero we’ve seen an example.

Python: let’s make a game (Pygame zero)

Now we will have a look at another example and, finally, to a Daniel Pope talk at the EuroPython conference of 2016.

A circle

Let’s draw a circle on the screen with pygame zero.

"""
Create a circle

This creates a circle

"""


import pgzrun

WIDTH = 800
HEIGHT = 600

def draw():
    screen.clear()
    screen.draw.circle((400, 300), 30, (255,255,255))

pgzrun.go()

 

Interacting with the screen

Now the code will show how to interact with the screen, making it change from red to green.

If you want something to happen when you  click the mouse you just have to write  a function like this:

def on_mouse_down(button):
    print("clicked", button)

In this case, when you press the mouse button, it will print if it is the left mouse button or the right one (mouse.left).

 

"""Create a circle
This creates a circle in the middle of the screen
if you click, background colors changes
"""
import pgzrun


# Two colors and the default background color
RED = 150, 0, 0
GREEN = 0, 128, 0
bg = RED
 
# screen size
WIDTH = 800
HEIGHT = 600
 
def draw(): # This draws a circle on the screen
    screen.fill(bg)
    screen.draw.circle((400, 300), 30, (255,255,255))
 
def on_mouse_down(): # when you click the mouse
	global bg
	bg = GREEN
 
def on_mouse_up(): # when you release the mouse
	global bg
	bg = RED
 
pgzrun.go()

 

Daniel Pope: pygame zero presentation at EuroPython conference

For Daniel Pope Pygame zero was made to make easier to see sprite moving on the screen moving and interacting to fill the gap with Scratch that allows that immediately. To have a visual impact of your code is very important to teach computer science to the younger. Another positive aspect is that pgzero is written i python 3. Teacher wanted to teach just one language, Python 3. This module make it possible because it solves the problems with the installation of pygame for python 3.

pygame zero code example
pygame zero code example
Pygame zero game
Pygame zero game

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.