Tuesday, 2 July 2019

My first Pygame zero attempt

I attended the brilliant #ExaBytes19 on 28th June 2019, among many very good sessions, I went to Jon Witts (@jonwitts) session on PyGame Zero [1]. Pygame zero is meant for Educational use and teaching programming through game development. Impressed with Pygame Zero relative simplicity, I have had a go myself.

The Game!
A Bee buzzes around the screen and a player moves to avoid it; if the bee and player meet the player's image changes and a sneezing sound is produced (I just fancied have a short cute noise). Both the bee and the player characters wrap around the screen.

A very helpful source of advice on getting started on using Pygame can be found at [2]; my example takes the game produced there as a starting point. If you want to explain of what Pygame does previously mentioned page[1] and the page describing the built-in  functions [3], I found a great help.




All the images were PNGs, the player characters were produced using https://www.bitmoji.com/ .  



The game code is shown below; all images have to be put in an image folder and the sounds in a sounds folder.

Liked especially that the image for a character (or Actor) can be changed very easily. Collision detection can be done in a few ways but I selected for simplicity the idea that each image has a rectangle around it and used this say when they meet, as in the example below.

if bee.colliderect(player):

        player_hurt()


import random
bee = Actor ('bee')
bee.pos = 50,50
player = Actor ('panic')
player.pos = 750, 750
HEIGHT = 800
WIDTH = 800
def draw():
screen.clear()
bee.draw()
player.draw()
def update():
player_update()
bee_update()
if bee.colliderect(player):
player_hurt()
def player_update():
if keyboard.right:
player.pos = (player.x+5, player.y)
if keyboard.left:
player.pos = (player.x-5, player.y)
if keyboard.up:
player.pos = (player.x, player.y-5)
if keyboard.down:
player.pos = (player.x, player.y+5)
if player.y>HEIGHT+25:
player.y=25
if player.y< 25:
player.y = HEIGHT-50
if player.x>WIDTH+25:
player.x=25
if player.x< 25:
player.x = WIDTH-50
def bee_update():
bee.pos= (bee.x + random.randint(-25,25), bee.y + random.randint(-25,25))
if bee.y>HEIGHT+25:
bee.y=25
if bee.y< 25:
bee.y = HEIGHT-50
if bee.x>WIDTH+25:
bee.x=25
if bee.x< 25:
bee.x = WIDTH-50
def player_hurt():
player.image='ohno'
sounds.sneeze.play()
clock.schedule_unique(player_normal, 1)
def player_normal():
player.image='panic'
view raw myfirstsolo2.py hosted with ❤ by GitHub


Code, images, etc are available at: https://github.com/scottturneruon/pygame_tests


As background, the following were interesting and provide further useful information and tips

I enjoyed playing with it, thank Jon for sharing this.


Resources

  1. Welcome to Pygame Zero https://pygame-zero.readthedocs.io/en/stable/
  2. Introduction to Pygame Zero https://pygame-zero.readthedocs.io/en/stable/introduction.html
  3. Built-in Objects - Pygame Zero https://pygame-zero.readthedocs.io/en/stable/builtins.html
  4. Space Asteroids - Pygame Zero http://www.penguintutor.com/projects/docs/space-asteroids-pgzero.pdf
  5. Pygame Zero Invaders https://www.raspberrypi.org/magpi/pygame-zero-invaders/
  6. Pygame Zero: SpaceInvaders II https://www.raspberrypi.org/magpi/pygame-zero-space-invaders-ii/

All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

7 comments:

Using Elecfreaks microbit Smart home kit

Using Elecfreaks microbit Smart home kit  http://bit.ly/43ooJF o a cool set of comments to build simulated Smart Building activities using a...