Enum in PyGame

From TRCCompSci - AQA Computer Science
Revision as of 08:56, 17 July 2018 by Admin (talk | contribs) (Created page with "=Import Enum= Before you can use Enums in python or pygame we need to import the correct module, so you will need to add: <syntaxhighlight lang=python> from enum import Enum...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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')
elif GAMESTATE == State.TWO:
   print('State TWO')
elif GAMESTATE == State.THREE:
   print('State THREE')