Pygame zero 5: improving graphic and adding sounds

Where to get graphic

The graphic until now: two sprites.
The graphic until now: two sprites. Pygame zero.

Welcome to the 5th article about Pygame zero. We’ve done a basic scenario for our game. Now we want to improve the graphic.

A videogame is essentially made of sprites and sounds and actions to do to accomplish a task. Grafic is essential. We have two sprite at the moment and nothing else:

You can right click on these two and save them in images (’cause we need to have a images folder in pygame zero).

At the following address you can find a lot of nice images for free for our game.

platformer de luxe

This pack s around 6 MB.

Here you can see some images of the possible use of the pack:

And here are all the tiny pieces (sprites) you could use at your will.

All you can use for your pgzero game with platformer art.

You have various format of these pictures.

Adding syntesizer’s sound to the game

You can use wav files or ogg files, but you can also use the internal sinthesyzer. First you create the note and then you use the method play of the istance you created, like this:

# creating sounds with the internal synthesizer

# A5 is the Picth, 0.5 is the duration
sound1 = tone.create('A5', 0.5)
sound1.play()

The game with modified graphic and sounds

This is one of the version of the game, where the alien have to land on a platform.

#! /usr/bin/python
# eatgame
# Giovanni Gatto
# Based on a Pope code
# video:

import pgzrun
import random
import time

# SCREEN WIDTH AND HEIGHT
WIDTH = 800
HEIGHT = 600

# THE SPRITES
# THERE IS NO NEED TO PUT THE PATH OF THE IMAGES
# BUT YOU HAVE TO PUT THEM INTO THE FOLDER /images (same for /sounds)
#alien = Actor('alien', midbottom=(WIDTH // 2, HEIGHT))
landing = Actor('landing', midbottom=(WIDTH // 2, HEIGHT))
alien_flying = Actor('alien_flying', anchor=('center', 'center'))
alien_flying2 = Actor('alien_flying2')

# DEFINE A SOUND WITH THE INTERNAL SYNTHESIZER
sound1 = tone.create('A5', 0.5)
sound2 = tone.create('F5', 0.5)
sound3 = tone.create('E3', 0.5)
pos = 0


def draw():
    """The background and the sprites go into the screen"""
    screen.fill('darkblue')
    # alien.draw()
    alien_flying.draw()
    landing.draw()


xpos = random.randint(0, 800)


def update():
    """One sprite falls down carried by the wind, the other is moved by you with arrows"""
    global xpos
    rand_pos = random.randint(1, 30)
    alien_flying.y += 5

    if rand_pos == 5:
        animation()

    if keyboard.left:
        # clock.schedule_unique(animation, 0.1)
        alien_flying.x -= 5
    if keyboard.right:
        # clock.schedule_unique(animation, 0.1)
        alien_flying.x += 5
    if alien_flying.colliderect(landing):
        alien_flying.image = 'alien'
        # PLAYING THE SOUND DEFINED ABOVE
        sound1.play()
        clock.schedule_unique(sound2_delayed_play, 0.2)
        time.sleep(0.3)
        print('you win')
        alien_flying.y = 0
        xpos = random.randint(0, 800)
        alien_flying.x = xpos
    if alien_flying.y > 600:
        print("you lose")
        alien_flying. y = 0
        xpos = random.randint(0, 800)
        alien_flying.x = xpos
        sound3.play()


def sound2_delayed_play():
    sound2.play()


def set_alien_normal():
    alien_flying.image = 'alien_flying'


def animation():
    global pos
    if pos == 1:
        #alien_flying.angle += 10
        alien_flying.image = 'alien_flying'
        pos = 0
    else:
        alien_flying.image = 'alien_flying2'
        pos = 1


pgzrun.go()

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.