Initial commit

This commit is contained in:
Sofia 2018-12-10 23:05:51 +02:00
commit 502e8f7725
24 changed files with 267 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.pyc
__pycache__

7
LICENSE Normal file
View File

@ -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.

35
README.md Normal file
View File

@ -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

BIN
audio/clack_01.ogg Normal file

Binary file not shown.

BIN
audio/clack_02.ogg Normal file

Binary file not shown.

BIN
audio/clack_03.ogg Normal file

Binary file not shown.

BIN
audio/clack_04.ogg Normal file

Binary file not shown.

BIN
audio/clack_05.ogg Normal file

Binary file not shown.

BIN
audio/clack_06.ogg Normal file

Binary file not shown.

BIN
audio/clack_07.ogg Normal file

Binary file not shown.

BIN
audio/clack_08.ogg Normal file

Binary file not shown.

BIN
audio/clack_09.ogg Normal file

Binary file not shown.

BIN
audio/clack_10.ogg Normal file

Binary file not shown.

44
bongocat.py Normal file
View File

@ -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

39
clackmanager.py Normal file
View File

@ -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"

BIN
img/bongo.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
img/bongocat.xcf Normal file

Binary file not shown.

BIN
img/bongocat_plain.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

BIN
img/left_paw_down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
img/left_paw_lifted.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

BIN
img/right_paw_down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

BIN
img/right_paw_lifted.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

57
input_manager.py Normal file
View File

@ -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

83
main.py Normal file
View File

@ -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()