Advertisement
Josif_tepe

Untitled

Jan 25th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. import pygame as pg
  2.  
  3. keybindings = {
  4.     'left': pg.K_LEFT,
  5.     'right': pg.K_RIGHT,
  6.     'down': pg.K_DOWN,
  7.     'action': pg.K_s,
  8.     'jump': pg.K_UP,
  9. }
  10.  
  11. class Control():
  12.     def __init__(self, caption):
  13.         self.screen = pg.display.get_surface()
  14.         self.done = False
  15.         self.clock = pg.time.Clock()
  16.         self.caption = caption
  17.         self.fps = 60
  18.         self.show_fps = False
  19.         self.current_time = 0.0
  20.         self.keys = pg.key.get_pressed()
  21.         self.state_dict = {}
  22.         self.state_name = None
  23.         self.state = None
  24.  
  25.     def setup_states(self, state_dict, start_state):
  26.         self.state_dict = state_dict
  27.         self.state_name = start_state
  28.         self.state = self.state_dict[self.state_name]
  29.  
  30.     def update(self):
  31.         self.current_time = pg.time.get_ticks()
  32.  
  33.         if self.state.quit:
  34.             self.done = True
  35.         elif self.state.done:
  36.             self.flip_state()
  37.         self.state.update(self.screen, self.keys, self.current_time)
  38.  
  39.     def flip_state(self):
  40.         previous_state = self.state_name
  41.         self.state_name = self.state.next
  42.         self.state = self.state_dict[self.state_name]
  43.         self.state.previous = previous_state
  44.  
  45.     def event_loop(self):
  46.         for event in pg.event.get():
  47.             if event.type == pg.QUIT:
  48.                 self.done = True
  49.             elif event.type == pg.KEYDOWN:
  50.                 self.keys = pg.key.get_pressed()
  51.                 self.toggle_show_fps(event.key)
  52.             elif event.type == pg.KEYUP:
  53.                 self.keys = pg.key.get_pressed()
  54.  
  55.             self.state.get_event(event)
  56.  
  57.  
  58.  
  59.     def toggle_show_fps(self, key):
  60.         if key == pg.K_z:
  61.             self.show_fps = not self.show_fps
  62.             if not self.show_fps:
  63.                 pg.display.set_caption(self.caption)
  64.  
  65.     def main(self):
  66.         # game loop for super mario game
  67.         while not self.done:
  68.             self.event_loop()
  69.             self.update()
  70.             pg.display.update()
  71.             self.clock.tick(self.fps)
  72.  
  73.             if self.show_fps:
  74.                 fps = self.clock.get_fps()
  75.                 fps_string = "{} - {:2f}".format(self.caption, fps)
  76.                 pg.display.set_caption(fps_string)
  77.  
  78. class _State():
  79.     def __init__(self):
  80.         self.start_time = 0.0
  81.         self.current_time = 0.0
  82.         self.done = False
  83.         self.quit = False
  84.         self.next = None
  85.         self.previous = None
  86.         self.persist = {}
  87.  
  88.     def startup(self, current_time, persistance):
  89.         self.persist = persistance
  90.         self.start_time = current_time
  91.  
  92.     def cleanup(self):
  93.         self.done = False
  94.         return self.persist
  95.  
  96.     def update(self, screen, keys, current_time):
  97.         pass
  98.  
  99.  
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement