main.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # pylint:disable=E1101
  2. import pygame
  3. import sys
  4. from input_manager import InputManager
  5. from bongocat import Bongocat
  6. from clackmanager import Clackmanager
  7. class Main:
  8. font = None
  9. input_manager = InputManager()
  10. bongocat = Bongocat()
  11. clackmanager = Clackmanager()
  12. debug = len(sys.argv) > 1 and sys.argv[1] == '--debug'
  13. sounds = False
  14. def init(self, linux):
  15. if linux:
  16. from pyxhook import HookManager
  17. else:
  18. from pyHook import HookManager
  19. hooks_manager = HookManager()
  20. hooks_manager.KeyDown = self.OnKeyDown
  21. hooks_manager.KeyUp = self.OnKeyUp
  22. hooks_manager.HookKeyboard()
  23. if linux:
  24. hooks_manager.start()
  25. pygame.init()
  26. pygame.mixer.init(buffer=1024)
  27. pygame.mixer.set_num_channels(4)
  28. size = (499, 374)
  29. background = 255, 0, 255
  30. pygame.display.set_caption("Bongocat")
  31. bongo_ico = pygame.image.load("img/bongo.ico")
  32. pygame.display.set_icon(bongo_ico)
  33. screen = pygame.display.set_mode(size)
  34. self.font = pygame.font.SysFont('Iosevka', 15)
  35. self.bongocat.load()
  36. self.clackmanager.load()
  37. self.input_manager.init(linux)
  38. self.input_manager.on_update = self.update_bongocat
  39. while True:
  40. for event in pygame.event.get():
  41. if event.type == pygame.QUIT:
  42. if linux:
  43. hooks_manager.cancel()
  44. sys.exit()
  45. self.input_manager.timer_update()
  46. font_surface = self.font.render("Pressed keys: " + str(self.input_manager.currently_pressed_keys),
  47. True, [0, 0, 0], [255, 255, 255])
  48. textpos = font_surface.get_rect()
  49. screen.fill(background)
  50. self.bongocat.draw(screen)
  51. if self.debug:
  52. screen.blit(font_surface, textpos)
  53. pygame.display.flip()
  54. if linux:
  55. hooks_manager.cancel()
  56. def OnKeyDown(self, event):
  57. self.input_manager.press_key(event.Key)
  58. return True
  59. def OnKeyUp(self, event):
  60. self.input_manager.release_key(event.Key)
  61. return True
  62. def update_bongocat(self, down):
  63. self.bongocat.is_left_paw_pressing = self.input_manager.left_keys_pressed()
  64. self.bongocat.is_right_paw_pressing = self.input_manager.right_keys_pressed()
  65. if down and self.sounds:
  66. self.clackmanager.play_clack()
  67. if __name__ == "__main__":
  68. from sys import platform
  69. if platform == "linux" or platform == "linux2":
  70. linux = True
  71. else:
  72. linux = False
  73. main = Main()
  74. main.init(linux)