programming-bongocat/main.py

100 lines
2.6 KiB
Python

# pylint:disable=E1101
import pygame
import sys
from input_manager import InputManager
from bongocat import Bongocat
from clackmanager import Clackmanager
class Main:
font = None
input_manager = InputManager()
bongocat = Bongocat()
clackmanager = Clackmanager()
debug = len(sys.argv) > 1 and sys.argv[1] == '--debug'
sounds = False
def init(self, linux):
if linux:
from pyxhook import HookManager
else:
from pyHook import HookManager
hooks_manager = HookManager()
hooks_manager.KeyDown = self.OnKeyDown
hooks_manager.KeyUp = self.OnKeyUp
hooks_manager.HookKeyboard()
if linux:
hooks_manager.start()
pygame.init()
pygame.mixer.init(buffer=1024)
pygame.mixer.set_num_channels(4)
size = (499, 374)
background = 255, 0, 255
pygame.display.set_caption("Bongocat")
bongo_ico = pygame.image.load("img/bongo.ico")
pygame.display.set_icon(bongo_ico)
screen = pygame.display.set_mode(size)
self.font = pygame.font.SysFont('Iosevka', 15)
self.bongocat.load()
self.clackmanager.load()
self.input_manager.on_update = self.update_bongocat
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
if linux:
hooks_manager.cancel()
sys.exit()
font_surface = self.font.render("Pressed keys: " + str(self.input_manager.currently_pressed_keys),
True, [0, 0, 0], [255, 255, 255])
textpos = font_surface.get_rect()
screen.fill(background)
self.bongocat.draw(screen)
if self.debug:
screen.blit(font_surface, textpos)
pygame.display.flip()
if linux:
hooks_manager.cancel()
def OnKeyDown(self, event):
self.input_manager.press_key(event.Key)
return True
def OnKeyUp(self, event):
self.input_manager.release_key(event.Key)
return True
def update_bongocat(self, down):
self.bongocat.is_left_paw_pressing = self.input_manager.left_keys_pressed()
self.bongocat.is_right_paw_pressing = self.input_manager.right_keys_pressed()
if down and self.sounds:
self.clackmanager.play_clack()
if __name__ == "__main__":
from sys import platform
if platform == "linux" or platform == "linux2":
linux = True
else:
linux = False
main = Main()
main.init(linux)