Difference between revisions of "Enum in PyGame"
(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...") |
(→Using Your Enum) |
||
Line 32: | Line 32: | ||
if GAMESTATE == State.ONE: | if GAMESTATE == State.ONE: | ||
print('State ONE') | print('State ONE') | ||
+ | # Do Something | ||
elif GAMESTATE == State.TWO: | elif GAMESTATE == State.TWO: | ||
print('State TWO') | print('State TWO') | ||
+ | # Do Something else | ||
elif GAMESTATE == State.THREE: | elif GAMESTATE == State.THREE: | ||
print('State THREE') | print('State THREE') | ||
+ | # Do Something else else | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 09:00, 17 July 2018
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