annotate grease/impl/controls.py @ 8:708a6f651c31

Modifications to use the grease manager and modes
author KarstenBock@gmx.net
date Tue, 12 Jul 2011 12:31:53 +0200
parents bc88f7d5ca8b
children
rev   line source
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3 # Copyright (c) 2010 by Casey Duncan
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4 # All Rights Reserved.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 # This software is subject to the provisions of the MIT License
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7 # A copy of the license should accompany this distribution.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13 """Control systems for binding controls to game logic"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15 import grease
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16 from pyglet.window import key
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18 class KeyControls(grease.System):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19 """System that maps subclass-defined action methods to keys.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 Keys may be mapped in the subclass definition using decorators
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 defined here as class methods or at runtime using the ``bind_key_*``
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23 instance methods.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25 See :ref:`an example implementation in the tutorial <tut-controls-example>`.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 MODIFIER_MASK = ~(key.MOD_NUMLOCK | key.MOD_SCROLLLOCK | key.MOD_CAPSLOCK)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28 """The MODIFIER_MASK allows you to filter out modifier keys that should be
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29 ignored by the application. By default, capslock, numlock, and scrolllock
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
30 are ignored.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 world = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34 """:class:`grease.World` object this system is bound to"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 def __init__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37 self._key_press_map = {}
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38 self._key_release_map = {}
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39 self._key_hold_map = {}
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 for name in self.__class__.__dict__:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 member = getattr(self, name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42 if hasattr(member, '_grease_hold_key_binding'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43 for binding in member._grease_hold_key_binding:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44 self.bind_key_hold(member, *binding)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45 if hasattr(member, '_grease_press_key_binding'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46 for binding in member._grease_press_key_binding:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47 self.bind_key_press(member, *binding)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48 if hasattr(member, '_grease_release_key_binding'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49 for binding in member._grease_release_key_binding:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50 self.bind_key_release(member, *binding)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 self.held_keys = set()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53 ## decorator methods for binding methods to key input events ##
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 @classmethod
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
56 def key_hold(cls, symbol, modifiers=0):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
57 """Decorator to bind a method to be executed where a key is held down"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 def bind(f):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59 if not hasattr(f, '_grease_hold_key_binding'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60 f._grease_hold_key_binding = []
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61 f._grease_hold_key_binding.append((symbol, modifiers & cls.MODIFIER_MASK))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62 return f
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63 return bind
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
65 @classmethod
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
66 def key_press(cls, symbol, modifiers=0):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
67 """Decorator to bind a method to be executed where a key is initially depressed"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
68 def bind(f):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
69 if not hasattr(f, '_grease_press_key_binding'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
70 f._grease_press_key_binding = []
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
71 f._grease_press_key_binding.append((symbol, modifiers & cls.MODIFIER_MASK))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
72 return f
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
73 return bind
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
74
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
75 @classmethod
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
76 def key_release(cls, symbol, modifiers=0):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
77 """Decorator to bind a method to be executed where a key is released"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
78 def bind(f):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
79 if not hasattr(f, '_grease_release_key_binding'):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
80 f._grease_release_key_binding = []
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
81 f._grease_release_key_binding.append((symbol, modifiers & cls.MODIFIER_MASK))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
82 return f
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
83 return bind
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
84
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
85 ## runtime binding methods ##
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
86
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
87 def bind_key_hold(self, method, key, modifiers=0):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
88 """Bind a method to a key at runtime to be invoked when the key is
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
89 held down, this replaces any existing key hold binding for this key.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
90 To unbind the key entirely, pass ``None`` for method.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
91 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
92 if method is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
93 self._key_hold_map[key, modifiers & self.MODIFIER_MASK] = method
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
94 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
95 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
96 del self._key_hold_map[key, modifiers & self.MODIFIER_MASK]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
97 except KeyError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
98 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
99
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
100 def bind_key_press(self, method, key, modifiers=0):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
101 """Bind a method to a key at runtime to be invoked when the key is initially
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
102 pressed, this replaces any existing key hold binding for this key. To unbind
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
103 the key entirely, pass ``None`` for method.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
104 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
105 if method is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
106 self._key_press_map[key, modifiers & self.MODIFIER_MASK] = method
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
107 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
108 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
109 del self._key_press_map[key, modifiers & self.MODIFIER_MASK]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
110 except KeyError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
111 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
112
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
113 def bind_key_release(self, method, key, modifiers=0):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
114 """Bind a method to a key at runtime to be invoked when the key is releaseed,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
115 this replaces any existing key hold binding for this key. To unbind
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
116 the key entirely, pass ``None`` for method.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
117 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
118 if method is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
119 self._key_release_map[key, modifiers & self.MODIFIER_MASK] = method
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
120 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
121 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
122 del self._key_release_map[key, modifiers & self.MODIFIER_MASK]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
123 except KeyError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
124 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
125
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
126 def step(self, dt):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
127 """invoke held key functions"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
128 already_run = set()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
129 for key in self.held_keys:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
130 func = self._key_hold_map.get(key)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
131 if func is not None and func not in already_run:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
132 already_run.add(func)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
133 func(dt)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
134
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
135 def on_key_press(self, key, modifiers):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
136 """Handle pyglet key press. Invoke key press methods and
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
137 activate key hold functions
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
138 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
139 key_mod = (key, modifiers & self.MODIFIER_MASK)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
140 if key_mod in self._key_press_map:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
141 self._key_press_map[key_mod]()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
142 self.held_keys.add(key_mod)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
143
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
144 def on_key_release(self, key, modifiers):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
145 """Handle pyglet key release. Invoke key release methods and
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
146 deactivate key hold functions
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
147 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
148 key_mod = (key, modifiers & self.MODIFIER_MASK)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
149 if key_mod in self._key_release_map:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
150 self._key_release_map[key_mod]()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
151 self.held_keys.discard(key_mod)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
152
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
153
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
154 if __name__ == '__main__':
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
155 import pyglet
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
156
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
157 class TestKeyControls(KeyControls):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
158
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
159 MODIFIER_MASK = ~(key.MOD_NUMLOCK | key.MOD_SCROLLLOCK | key.MOD_CTRL)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
160
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
161 remapped = False
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
162
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
163 @KeyControls.key_hold(key.UP)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
164 @KeyControls.key_hold(key.W)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
165 def up(self, dt):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
166 print 'UP!'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
167
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
168 @KeyControls.key_hold(key.LEFT)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
169 @KeyControls.key_hold(key.A)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
170 def left(self, dt):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
171 print 'LEFT!'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
172
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
173 @KeyControls.key_hold(key.RIGHT)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
174 @KeyControls.key_hold(key.D)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
175 def right(self, dt):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
176 print 'RIGHT!'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
177
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
178 @KeyControls.key_hold(key.DOWN)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
179 @KeyControls.key_hold(key.S)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
180 def down(self, dt):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
181 print 'DOWN!'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
182
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
183 @KeyControls.key_press(key.SPACE)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
184 def fire(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
185 print 'FIRE!'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
186
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
187 @KeyControls.key_press(key.R)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
188 def remap_keys(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
189 if not self.remapped:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
190 self.bind_key_hold(None, key.W)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
191 self.bind_key_hold(None, key.A)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
192 self.bind_key_hold(None, key.S)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
193 self.bind_key_hold(None, key.D)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
194 self.bind_key_hold(self.up, key.I)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
195 self.bind_key_hold(self.left, key.J)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
196 self.bind_key_hold(self.right, key.L)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
197 self.bind_key_hold(self.down, key.K)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
198 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
199 self.bind_key_hold(None, key.I)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
200 self.bind_key_hold(None, key.J)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
201 self.bind_key_hold(None, key.K)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
202 self.bind_key_hold(None, key.L)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
203 self.bind_key_hold(self.up, key.W)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
204 self.bind_key_hold(self.left, key.A)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
205 self.bind_key_hold(self.right, key.D)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
206 self.bind_key_hold(self.down, key.S)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
207 self.remapped = not self.remapped
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
208
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
209
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
210 window = pyglet.window.Window()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
211 window.clear()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
212 controls = TestKeyControls()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
213 window.push_handlers(controls)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
214 pyglet.clock.schedule_interval(controls.step, 0.5)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
215 pyglet.app.run()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
216