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()




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

6 comments:

Who wants to produce AI produced cartoon strips

Question: How easy is it produce a comic/cartoon using genetative AI? Let's start with  using ChatGPT4o to produce cartoons. The idea wa...