Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- key_code = pygame.key.key_code
- class Scene:
- def __init__(self, control):
- self.control = control
- def on_draw(self, surface): pass
- def on_event(self, event): pass
- def on_update(self, delta): pass
- class SceneHandler:
- def __init__(self):
- self._current = None
- self._next = None
- def set(self, scene):
- if self._current:
- self._next = scene
- else:
- self._current = scene
- def update(self):
- if self._next:
- self._current = self._next
- self._next = None
- return self._current
- class SceneControl:
- def __init__(self, caption, size, flags=0, fps=60):
- pygame.display.set_caption(caption)
- self.display = pygame.display.set_mode(size, flags)
- self.rect = self.display.get_rect()
- self.clock = pygame.time.Clock()
- self.handler = SceneHandler()
- self.running = True
- self.delta = 0
- self.fps = fps
- def run(self, scene):
- self.handler.set(scene)
- while self.running:
- scene = self.handler.update()
- for event in pygame.event.get():
- if event.type != pygame.QUIT:
- scene.on_event(event)
- else:
- self.running = False
- scene.on_update(self.delta)
- scene.on_draw(self.display)
- pygame.display.flip()
- self.delta = self.clock.tick(self.fps)
- class Timer:
- def __init__(self, interval):
- self.interval = interval
- self.timer = 0
- def action(self, ticks):
- return 0 < self.timer > ticks
- def clear(self):
- self.timer = 0
- def set(self):
- self.timer = pygame.time.get_ticks() + self.interval
- class AttackState:
- def __init__(self, combos, interval):
- self.combos = combos
- self.timer = Timer(interval)
- self.state = None
- def action(self, key):
- if self.state:
- if key in self.combos[self.state]:
- ticks = pygame.time.get_ticks()
- if self.timer.action(ticks):
- print(self.combos[self.state][key])
- else:
- print("Time Elapse")
- self.update(key)
- else:
- self.update(key)
- else:
- self.update(key)
- def update(self, key):
- if key in self.combos:
- self.state = key
- self.timer.set()
- else:
- self.state = None
- self.timer.clear()
- class Player:
- def __init__(self, combos):
- self.state = AttackState(combos, 500)
- def attack(self, event):
- self.state.action(event.key)
- print(self.state.state)
- class ComboExample(Scene):
- def __init__(self, control):
- super().__init__(control)
- self.background = 'black'
- self.player = Player(self.combo_set_a())
- def combo_set_a(self):
- return {
- key_code('j'): {key_code('u'): 'Combo 1'},
- key_code('k'): {key_code('i'): 'Combo 2'}
- }
- def on_draw(self, surface):
- surface.fill(self.background)
- def on_event(self, event):
- if event.type == pygame.KEYDOWN:
- self.player.attack(event)
- pygame.init()
- control = SceneControl("Combo Example", (600, 600))
- control.run(ComboExample(control))
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement