이전에 파이썬으로 여러 가지 간단한 2 차원 게임을 만들었고 간단하기 때문에 그런 것을 만들 필요가 없었습니다. 하지만 지금은 네 번째로 돌아갈 필요가 있기 때문에 필요합니다. 앞으로 나아가려면 어떤 종류의 방향이 필요합니다 ...상태 기계를 파이썬으로 작성하기
저는 새로운 게임으로 약 200 줄에 달하며 실제 게임을 시작하지 않았습니다. 현재는 모든 창, 이벤트 및 주를 다루고 있습니다.
### state machine
def run:
#~setup window
# Current state
state = menu()
running = True
while running:
#~event handler
returns = state.update(events)
if returns == "playgame":
state = game()
state.draw(window)
#menu state
class menu:
def __init__(self):
#~define vars for the menu (buttons, etc...)
self.clickables = [] # filled with gui etc..
self.image = image() # image of the menu
def update(self, events):
for event in events: # go through events
for clickable in self.clickables: # go through clickables
if clickable.testClicked(event.pos) != False: # Returns if clicked
return clickable.testClicked(event.pos)
def draw(self, window):
self.image.draw(window)
#game state
class game(menu): # Exactly the same as menu (just used as example)
def __init__(self):
super(menu).__init__()
#gui button
class button: # Extremely shortened for example use
def __init__(self, do): # do is a string
self.whenClickedDo = do
def testClicked(self, mousePos):
if mousePos in self.myRect: # myRect is the area of the button (Example!)
return self.whenClickedDo
return False
이 위의 예는 완전히 돌진하지만 숙고 문제가되었다 ... 무엇이을 달성하기 위해 더 나은 방법, 또는이 일을 할 수있는 스마트/달성 위의 방법입니까?
TLDR; 함수 "run"은 자신을 다른 상태로 변경하는 데 사용될 값을 반환 할 수있는 "state"값을가집니다. 그것은 상태 기계를 만드는 합리적인 방법입니까?
그래서 (게임이 느리다는 것을) 명확히하기 위해 게임에 대한 조언을 듣고 있습니까? E.G. '메인 메뉴'상태, '게임 플레이'상태 등을 표시 할 수 있습니까? – RichSmith
@RichSmith 아니요 ... 현재 상태 업데이트 상태에서 새 상태를 반환하는 경우 커뮤니티의 대다수가 선호하는 기능을 알고 싶습니다. – AUFakie