commit 502e8f77253d44b3f300ac5207ae393dcb382ada Author: teascade Date: Mon Dec 10 23:05:51 2018 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..feae5c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +__pycache__ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5cb6afc --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2018 Sofia 'Teascade' Talarmo + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..dca6627 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Programming Bongocat! + +A simple python program that catches all your global keyboard inputs and then pretends to press them (bongocat will press the keyboard with their left paw if the button is on the left side of the keyboard and with their right paw if it's on the right). + +## Usage + +Dependencies: +- Python 2.7.x +- [PyHook][pyhook] (if using windows, [PyxHook][pyxhook] on linux) +- [Pygame][pygame] + +When you've made sure your `python` is version 2.7.x, + +**on Windows** you can run on +`python -m pip install pyhook` +or **on Linux** run +`python -m pip install pyxhook` + +After that install pygame with +`python -m pip install pygame` + +**If you are working on linux** you need to replace `pyHook` with `pyxhook` on line 5 of `main.py` + +And then you should be able to open bongocat with +`python main.py` + +## License + +Programming bongocat is licensed under the terms of the [MIT license][license] + + +[pyhook]: https://github.com/naihe2010/pyHook +[pyxhook]: https://github.com/JeffHoogland/pyxhook +[pygame]: https://www.pygame.org +[license]: LICENSE \ No newline at end of file diff --git a/audio/clack_01.ogg b/audio/clack_01.ogg new file mode 100644 index 0000000..365c16d Binary files /dev/null and b/audio/clack_01.ogg differ diff --git a/audio/clack_02.ogg b/audio/clack_02.ogg new file mode 100644 index 0000000..87b7c38 Binary files /dev/null and b/audio/clack_02.ogg differ diff --git a/audio/clack_03.ogg b/audio/clack_03.ogg new file mode 100644 index 0000000..1f75370 Binary files /dev/null and b/audio/clack_03.ogg differ diff --git a/audio/clack_04.ogg b/audio/clack_04.ogg new file mode 100644 index 0000000..1c9e445 Binary files /dev/null and b/audio/clack_04.ogg differ diff --git a/audio/clack_05.ogg b/audio/clack_05.ogg new file mode 100644 index 0000000..b474d8b Binary files /dev/null and b/audio/clack_05.ogg differ diff --git a/audio/clack_06.ogg b/audio/clack_06.ogg new file mode 100644 index 0000000..4ce515a Binary files /dev/null and b/audio/clack_06.ogg differ diff --git a/audio/clack_07.ogg b/audio/clack_07.ogg new file mode 100644 index 0000000..49bf039 Binary files /dev/null and b/audio/clack_07.ogg differ diff --git a/audio/clack_08.ogg b/audio/clack_08.ogg new file mode 100644 index 0000000..b8ed2ae Binary files /dev/null and b/audio/clack_08.ogg differ diff --git a/audio/clack_09.ogg b/audio/clack_09.ogg new file mode 100644 index 0000000..466bb44 Binary files /dev/null and b/audio/clack_09.ogg differ diff --git a/audio/clack_10.ogg b/audio/clack_10.ogg new file mode 100644 index 0000000..1690a7f Binary files /dev/null and b/audio/clack_10.ogg differ diff --git a/bongocat.py b/bongocat.py new file mode 100644 index 0000000..a6fb624 --- /dev/null +++ b/bongocat.py @@ -0,0 +1,44 @@ + +import pygame + + +class Bongocat: + + bongocat_plain = None + left_paw_lifted = None + left_paw_down = None + right_paw_lifted = None + right_paw_down = None + + rect = None + + is_left_paw_pressing = False + is_right_paw_pressing = False + + def load(self): + self.bongocat_plain = pygame.image.load("img/bongocat_plain.png") + self.left_paw_lifted = pygame.image.load("img/left_paw_lifted.png") + self.left_paw_down = pygame.image.load("img/left_paw_down.png") + self.right_paw_lifted = pygame.image.load("img/right_paw_lifted.png") + self.right_paw_down = pygame.image.load("img/right_paw_down.png") + + self.rect = self.bongocat_plain.get_rect() + + def draw(self, screen): + screen.blit(self.bongocat_plain, self.rect) + + if self.is_left_paw_pressing: + screen.blit(self.left_paw_down, self.rect) + else: + screen.blit(self.left_paw_lifted, self.rect) + + if self.is_right_paw_pressing: + screen.blit(self.right_paw_down, self.rect) + else: + screen.blit(self.right_paw_lifted, self.rect) + + def left_paw_pressing(self, pressing): + self.is_left_paw_pressing = pressing + + def right_paw_pressing(self, pressing): + self.is_right_paw_pressing = pressing diff --git a/clackmanager.py b/clackmanager.py new file mode 100644 index 0000000..c03cf1d --- /dev/null +++ b/clackmanager.py @@ -0,0 +1,39 @@ + +from pygame.mixer import Sound, Channel +from pygame import mixer +import random + + +class Clackmanager: + + clacks = [] + + channel1 = None + channel2 = None + + def load(self): + self.clacks.append(Sound("audio/clack_01.ogg")) + self.clacks.append(Sound("audio/clack_02.ogg")) + self.clacks.append(Sound("audio/clack_03.ogg")) + self.clacks.append(Sound("audio/clack_04.ogg")) + self.clacks.append(Sound("audio/clack_05.ogg")) + self.clacks.append(Sound("audio/clack_06.ogg")) + self.clacks.append(Sound("audio/clack_07.ogg")) + self.clacks.append(Sound("audio/clack_08.ogg")) + self.clacks.append(Sound("audio/clack_09.ogg")) + self.clacks.append(Sound("audio/clack_10.ogg")) + + self.channel1 = Channel(0) + self.channel2 = Channel(2) + + def play_clack(self): + while True: + clack = random.choice(self.clacks) + if clack.get_num_channels() == 0: + break + if not self.channel1.get_busy(): + self.channel1.play(clack) + print "played on 1" + elif not self.channel2.get_busy(): + self.channel2.play(clack) + print "played on 2" diff --git a/img/bongo.ico b/img/bongo.ico new file mode 100644 index 0000000..08715f8 Binary files /dev/null and b/img/bongo.ico differ diff --git a/img/bongocat.xcf b/img/bongocat.xcf new file mode 100644 index 0000000..6f72abc Binary files /dev/null and b/img/bongocat.xcf differ diff --git a/img/bongocat_plain.png b/img/bongocat_plain.png new file mode 100644 index 0000000..bed66ef Binary files /dev/null and b/img/bongocat_plain.png differ diff --git a/img/left_paw_down.png b/img/left_paw_down.png new file mode 100644 index 0000000..4d5d53a Binary files /dev/null and b/img/left_paw_down.png differ diff --git a/img/left_paw_lifted.png b/img/left_paw_lifted.png new file mode 100644 index 0000000..9b4914a Binary files /dev/null and b/img/left_paw_lifted.png differ diff --git a/img/right_paw_down.png b/img/right_paw_down.png new file mode 100644 index 0000000..c24feec Binary files /dev/null and b/img/right_paw_down.png differ diff --git a/img/right_paw_lifted.png b/img/right_paw_lifted.png new file mode 100644 index 0000000..56a3537 Binary files /dev/null and b/img/right_paw_lifted.png differ diff --git a/input_manager.py b/input_manager.py new file mode 100644 index 0000000..be9fb9c --- /dev/null +++ b/input_manager.py @@ -0,0 +1,57 @@ + +class InputManager: + + currently_pressed_keys = [] + + left_paw_keys = [ + "1", "2", "3", "4", "5", + "Q", "W", "E", "R", "T", "Y", + "A", "S", "D", "F", "G", "H", + "Z", "X", "C", "V", "B", + + "Espace", "Oem_5", "Tab", "Capital", "Lshift", + "Lcontrol", "Lwin", "Lmenu", "Space", + "F1", "F2", "F3", "F4", "F5" + ] + + right_paw_keys = [ + "6", "7", "8", "9", "0", "Oem_Plus", "Oem_4", "Back", + "U", "I", "O", "P", "Oem_6", "Oem_1", "Return", + "J", "K", "L", "Oem_3", "Oem_7", "Oem_2", + "N", "M", "Oem_Comma", "Oem_Period", "Oem_Minus", "Rshift", + + "Rmenu", "Rwin", "Rcontrol", + "Up", "Left", "Down", "Right", + "Insert", "Home", "Prior", "Delete", "End", "Next", + "Snapshot", "Scroll", "Pause", + "Numpad0", "Numpad1", "Numpad2", "Numpad3", "Numpad4", "Numpad5", "Numpad6", "Numpad7", "Numpad8", "Numpad9", + "Numlock", "Divide", "Multiply", "Subtract", "Add", "Decimal", + "F6", "F7", "F8", "F9", "F10", "F11", "F12" + ] + + def on_update(self, down): + pass + + def press_key(self, key): + if not key in self.currently_pressed_keys: + self.currently_pressed_keys.append(key) + self.on_update(True) + + def release_key(self, key): + if key in self.currently_pressed_keys: + self.currently_pressed_keys.remove(key) + self.on_update(False) + + def left_keys_pressed(self): + pressed = False + for key in self.currently_pressed_keys: + if key in self.left_paw_keys: + pressed = True + return pressed + + def right_keys_pressed(self): + pressed = False + for key in self.currently_pressed_keys: + if key in self.right_paw_keys: + pressed = True + return pressed diff --git a/main.py b/main.py new file mode 100644 index 0000000..418f08f --- /dev/null +++ b/main.py @@ -0,0 +1,83 @@ +# pylint:disable=E1101 + +import pygame +import sys +from pyHook import HookManager + +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 = False + sounds = False + + def init(self): + + hooks_manager = HookManager() + hooks_manager.KeyDown = self.OnKeyDown + hooks_manager.KeyUp = self.OnKeyUp + hooks_manager.HookKeyboard() + + 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', 24) + 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: + 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() + + 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__": + main = Main() + main.init()