Browse Source

Initial commit

Teascade 8 months ago
commit
502e8f7725

+ 2
- 0
.gitignore View File

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

+ 7
- 0
LICENSE View File

@@ -0,0 +1,7 @@
1
+Copyright 2018 Sofia 'Teascade' Talarmo
2
+
3
+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:
4
+
5
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+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
- 0
README.md View File

@@ -0,0 +1,35 @@
1
+# Programming Bongocat!
2
+
3
+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).
4
+
5
+## Usage
6
+
7
+Dependencies:
8
+- Python 2.7.x
9
+- [PyHook][pyhook] (if using windows, [PyxHook][pyxhook] on linux)
10
+- [Pygame][pygame]
11
+
12
+When you've made sure your `python` is version 2.7.x,
13
+
14
+**on Windows** you can run on
15
+`python -m pip install pyhook`  
16
+or **on Linux** run
17
+`python -m pip install pyxhook`
18
+
19
+After that install pygame with
20
+`python -m pip install pygame`
21
+
22
+**If you are working on linux** you need to replace `pyHook` with `pyxhook` on line 5 of `main.py`
23
+
24
+And then you should be able to open bongocat with
25
+`python main.py`
26
+
27
+## License
28
+
29
+Programming bongocat is licensed under the terms of the [MIT license][license]
30
+
31
+
32
+[pyhook]: https://github.com/naihe2010/pyHook
33
+[pyxhook]: https://github.com/JeffHoogland/pyxhook
34
+[pygame]: https://www.pygame.org
35
+[license]: LICENSE

BIN
audio/clack_01.ogg View File


BIN
audio/clack_02.ogg View File


BIN
audio/clack_03.ogg View File


BIN
audio/clack_04.ogg View File


BIN
audio/clack_05.ogg View File


BIN
audio/clack_06.ogg View File


BIN
audio/clack_07.ogg View File


BIN
audio/clack_08.ogg View File


BIN
audio/clack_09.ogg View File


BIN
audio/clack_10.ogg View File


+ 44
- 0
bongocat.py View File

@@ -0,0 +1,44 @@
1
+
2
+import pygame
3
+
4
+
5
+class Bongocat:
6
+
7
+    bongocat_plain = None
8
+    left_paw_lifted = None
9
+    left_paw_down = None
10
+    right_paw_lifted = None
11
+    right_paw_down = None
12
+
13
+    rect = None
14
+
15
+    is_left_paw_pressing = False
16
+    is_right_paw_pressing = False
17
+
18
+    def load(self):
19
+        self.bongocat_plain = pygame.image.load("img/bongocat_plain.png")
20
+        self.left_paw_lifted = pygame.image.load("img/left_paw_lifted.png")
21
+        self.left_paw_down = pygame.image.load("img/left_paw_down.png")
22
+        self.right_paw_lifted = pygame.image.load("img/right_paw_lifted.png")
23
+        self.right_paw_down = pygame.image.load("img/right_paw_down.png")
24
+
25
+        self.rect = self.bongocat_plain.get_rect()
26
+
27
+    def draw(self, screen):
28
+        screen.blit(self.bongocat_plain, self.rect)
29
+
30
+        if self.is_left_paw_pressing:
31
+            screen.blit(self.left_paw_down, self.rect)
32
+        else:
33
+            screen.blit(self.left_paw_lifted, self.rect)
34
+
35
+        if self.is_right_paw_pressing:
36
+            screen.blit(self.right_paw_down, self.rect)
37
+        else:
38
+            screen.blit(self.right_paw_lifted, self.rect)
39
+
40
+    def left_paw_pressing(self, pressing):
41
+        self.is_left_paw_pressing = pressing
42
+
43
+    def right_paw_pressing(self, pressing):
44
+        self.is_right_paw_pressing = pressing

+ 39
- 0
clackmanager.py View File

@@ -0,0 +1,39 @@
1
+
2
+from pygame.mixer import Sound, Channel
3
+from pygame import mixer
4
+import random
5
+
6
+
7
+class Clackmanager:
8
+
9
+    clacks = []
10
+
11
+    channel1 = None
12
+    channel2 = None
13
+
14
+    def load(self):
15
+        self.clacks.append(Sound("audio/clack_01.ogg"))
16
+        self.clacks.append(Sound("audio/clack_02.ogg"))
17
+        self.clacks.append(Sound("audio/clack_03.ogg"))
18
+        self.clacks.append(Sound("audio/clack_04.ogg"))
19
+        self.clacks.append(Sound("audio/clack_05.ogg"))
20
+        self.clacks.append(Sound("audio/clack_06.ogg"))
21
+        self.clacks.append(Sound("audio/clack_07.ogg"))
22
+        self.clacks.append(Sound("audio/clack_08.ogg"))
23
+        self.clacks.append(Sound("audio/clack_09.ogg"))
24
+        self.clacks.append(Sound("audio/clack_10.ogg"))
25
+
26
+        self.channel1 = Channel(0)
27
+        self.channel2 = Channel(2)
28
+
29
+    def play_clack(self):
30
+        while True:
31
+            clack = random.choice(self.clacks)
32
+            if clack.get_num_channels() == 0:
33
+                break
34
+        if not self.channel1.get_busy():
35
+            self.channel1.play(clack)
36
+            print "played on 1"
37
+        elif not self.channel2.get_busy():
38
+            self.channel2.play(clack)
39
+            print "played on 2"

BIN
img/bongo.ico View File


BIN
img/bongocat.xcf View File


BIN
img/bongocat_plain.png View File


BIN
img/left_paw_down.png View File


BIN
img/left_paw_lifted.png View File


BIN
img/right_paw_down.png View File


BIN
img/right_paw_lifted.png View File


+ 57
- 0
input_manager.py View File

@@ -0,0 +1,57 @@
1
+
2
+class InputManager:
3
+
4
+    currently_pressed_keys = []
5
+
6
+    left_paw_keys = [
7
+        "1", "2", "3", "4", "5",
8
+        "Q", "W", "E", "R", "T", "Y",
9
+        "A", "S", "D", "F", "G", "H",
10
+        "Z", "X", "C", "V", "B",
11
+
12
+        "Espace", "Oem_5", "Tab", "Capital", "Lshift",
13
+        "Lcontrol", "Lwin", "Lmenu", "Space",
14
+        "F1", "F2", "F3", "F4", "F5"
15
+    ]
16
+
17
+    right_paw_keys = [
18
+        "6", "7", "8", "9", "0", "Oem_Plus", "Oem_4", "Back",
19
+        "U", "I", "O", "P", "Oem_6", "Oem_1", "Return",
20
+        "J", "K", "L", "Oem_3", "Oem_7", "Oem_2",
21
+        "N", "M", "Oem_Comma", "Oem_Period", "Oem_Minus", "Rshift",
22
+
23
+        "Rmenu", "Rwin", "Rcontrol",
24
+        "Up", "Left", "Down", "Right",
25
+        "Insert", "Home", "Prior", "Delete", "End", "Next",
26
+        "Snapshot", "Scroll", "Pause",
27
+        "Numpad0", "Numpad1", "Numpad2", "Numpad3", "Numpad4", "Numpad5", "Numpad6", "Numpad7", "Numpad8", "Numpad9",
28
+        "Numlock", "Divide", "Multiply", "Subtract", "Add", "Decimal",
29
+        "F6", "F7", "F8", "F9", "F10", "F11", "F12"
30
+    ]
31
+
32
+    def on_update(self, down):
33
+        pass
34
+
35
+    def press_key(self, key):
36
+        if not key in self.currently_pressed_keys:
37
+            self.currently_pressed_keys.append(key)
38
+            self.on_update(True)
39
+
40
+    def release_key(self, key):
41
+        if key in self.currently_pressed_keys:
42
+            self.currently_pressed_keys.remove(key)
43
+            self.on_update(False)
44
+
45
+    def left_keys_pressed(self):
46
+        pressed = False
47
+        for key in self.currently_pressed_keys:
48
+            if key in self.left_paw_keys:
49
+                pressed = True
50
+        return pressed
51
+
52
+    def right_keys_pressed(self):
53
+        pressed = False
54
+        for key in self.currently_pressed_keys:
55
+            if key in self.right_paw_keys:
56
+                pressed = True
57
+        return pressed

+ 83
- 0
main.py View File

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