Enum in PyGame

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Import Enum

Before you can use Enums in python or pygame we need to import the correct module, so you will need to add:

from enum import Enum

Declaring Enum

Enum is used has a base class for your Enum:

class State(Enum):
   ONE = 1
   TWO = 2
   THREE = 3

Using Your Enum

You can create a variable and assign it one of your Enum states, you will also be able to access the name and value:

GAMESTATE = State.ONE
print(GAMESTATE.name)
print(GAMESTATE.value)

Within your game loop you can actually check your GAMESTATE and run the appropriate code:

if GAMESTATE == State.ONE:
   print('State ONE')
   # Do Something
elif GAMESTATE == State.TWO:
   print('State TWO')
   # Do Something else
elif GAMESTATE == State.THREE:
   print('State THREE')
   # Do Something else else