Building a class-based Pygame Framework

Warning

This video is quite long (approx. 1 hour). It may be a good activity to undertake to get insight into more advanced concepts. It may be skipped.

Goals

  • Modify the Pygame template to make use of classes.

  • Create views that will separate game logic into methods (event_loop, update, draw) so we can easily swap between complex views like title screen, instructions, the game itself, and sub-menus within the game.

  • Mini introduction to inheritance of an interface.

  • Create different files to store our classes, rather than have the entire game in a single file.

  • Switch between scenes by modifying the code directly (actual scene switching to come at another time)

Starter code

Video Tutorial

What you need to do

  • Create a new View subclass using inheritance and override the event_loop, update, and draw methods.

  • Set that new view as the first current_view so when you run the game it loads your new view

  • Give the background a different color than all the other views.

  • Draw some text to give the View a title.

  • Add a unique mouse or key event

  • Get some animation like a moving shape

Final Code

Pygame Framework On GitHub