Advertisement
Guest User

Pygame Combo Example.

a guest
Mar 27th, 2025
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | Gaming | 0 0
  1. import pygame
  2.  
  3. key_code = pygame.key.key_code
  4.  
  5. class Scene:
  6.     def __init__(self, control):
  7.         self.control = control
  8.  
  9.     def on_draw(self, surface): pass
  10.     def on_event(self, event): pass
  11.     def on_update(self, delta): pass
  12.  
  13. class SceneHandler:
  14.     def __init__(self):
  15.         self._current = None
  16.         self._next = None
  17.  
  18.     def set(self, scene):
  19.         if self._current:
  20.             self._next = scene
  21.         else:
  22.             self._current = scene
  23.  
  24.     def update(self):
  25.         if self._next:
  26.             self._current = self._next
  27.             self._next = None
  28.  
  29.         return self._current
  30.  
  31. class SceneControl:
  32.     def __init__(self, caption, size, flags=0, fps=60):
  33.         pygame.display.set_caption(caption)
  34.         self.display = pygame.display.set_mode(size, flags)
  35.         self.rect = self.display.get_rect()
  36.         self.clock = pygame.time.Clock()
  37.         self.handler = SceneHandler()
  38.         self.running = True
  39.         self.delta = 0
  40.         self.fps = fps
  41.  
  42.     def run(self, scene):
  43.         self.handler.set(scene)
  44.         while self.running:
  45.             scene = self.handler.update()
  46.             for event in pygame.event.get():
  47.                 if event.type != pygame.QUIT:
  48.                     scene.on_event(event)
  49.                 else:
  50.                     self.running = False
  51.  
  52.             scene.on_update(self.delta)
  53.             scene.on_draw(self.display)
  54.             pygame.display.flip()
  55.             self.delta = self.clock.tick(self.fps)
  56.  
  57. class Timer:
  58.     def __init__(self, interval):
  59.         self.interval = interval
  60.         self.timer = 0
  61.  
  62.     def action(self, ticks):
  63.         return 0 < self.timer > ticks
  64.  
  65.     def clear(self):
  66.         self.timer = 0
  67.  
  68.     def set(self):
  69.         self.timer = pygame.time.get_ticks() + self.interval
  70.  
  71. class AttackState:
  72.     def __init__(self, combos, interval):
  73.         self.combos = combos
  74.         self.timer = Timer(interval)
  75.         self.state = None
  76.  
  77.     def action(self, key):
  78.         if self.state:
  79.             if key in self.combos[self.state]:
  80.                 ticks = pygame.time.get_ticks()
  81.                 if self.timer.action(ticks):
  82.                     print(self.combos[self.state][key])
  83.                 else:
  84.                     print("Time Elapse")
  85.                     self.update(key)
  86.             else:
  87.                 self.update(key)
  88.         else:
  89.             self.update(key)
  90.  
  91.     def update(self, key):
  92.         if key in self.combos:
  93.             self.state = key
  94.             self.timer.set()
  95.         else:
  96.             self.state = None
  97.             self.timer.clear()
  98.  
  99. class Player:
  100.     def __init__(self, combos):
  101.         self.state = AttackState(combos, 500)
  102.  
  103.     def attack(self, event):
  104.         self.state.action(event.key)
  105.         print(self.state.state)
  106.  
  107. class ComboExample(Scene):
  108.     def __init__(self, control):
  109.         super().__init__(control)
  110.         self.background = 'black'
  111.         self.player = Player(self.combo_set_a())
  112.  
  113.     def combo_set_a(self):
  114.         return {
  115.             key_code('j'): {key_code('u'): 'Combo 1'},
  116.             key_code('k'): {key_code('i'): 'Combo 2'}
  117.         }
  118.  
  119.     def on_draw(self, surface):
  120.         surface.fill(self.background)
  121.  
  122.     def on_event(self, event):
  123.         if event.type == pygame.KEYDOWN:
  124.             self.player.attack(event)
  125.  
  126. pygame.init()
  127. control = SceneControl("Combo Example", (600, 600))
  128. control.run(ComboExample(control))
  129. pygame.quit()
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement