class GameLevelScene(BaseLevelScene):
def enter(self, world):
+ self._held_keys = set()
if self._level is not None:
for generator in self._generators:
generator.unpause()
self._init_items()
self._init_friendly_roaches()
self._init_generators()
+ self._key_rate = 0.2
+ self._last_key_down = 0
return self._init_roaches(world)
def _init_items(self):
for item in self._items:
item.pos = self.calc_offset(
item.game_pos[0] * TILE_SIZE, item.game_pos[1] * TILE_SIZE)
+ more = self._check_held_keys(dt)
+ if more:
+ events.extend(more)
return events
- def _movement_key(self, key):
+ def _check_held_keys(self, dt):
+ for key in self._held_keys:
+ self._last_key_down += dt
+ if key in (keys.DOWN, keys.UP, keys.LEFT, keys.RIGHT):
+ return self._movement_key(key, dt)
+ elif key == keys.X:
+ return self._fire_key(dt)
+
+ def _movement_key(self, key, dt):
x, y = self._player_pos
for k, dp, angle in (
(keys.DOWN, (0, 1), 180),
(keys.RIGHT, (1, 0), -90),
):
if key == k:
- nx, ny = x + dp[0], y + dp[1]
- if self._can_move(nx, ny):
- self._set_pos(nx, ny)
- offset = (TILE_SIZE * dp[0], TILE_SIZE * dp[1])
+ if self._angle == angle and self._last_key_down > self._key_rate:
+ nx, ny = x + dp[0], y + dp[1]
+ if self._can_move(nx, ny):
+ self._set_pos(nx, ny)
+ offset = (TILE_SIZE * dp[0], TILE_SIZE * dp[1])
+ self._set_angle(angle)
+ self._last_key_down = 0
+ return [MoveViewportEvent(offset)]
+ else:
+ # just turn
self._set_angle(angle)
- return [MoveViewportEvent(offset)]
def _activate_key(self):
x, y = self._player_pos
next_level = self._level.get_exit_level()
return [WorldEvent('set', {'level.name': next_level}), ChangeSceneEvent(GameLevelScene())]
- def _fire_key(self):
- print('Boom')
+ def _fire_key(self, dt):
+ if self._last_key_down > self._key_rate:
+ self._last_key_down = 0
+ print('Boom')
def _vehicle_management_key(self):
from .roach_management import RoachesScene
def on_key_down(self, key, mod, unicode):
x, y = self._player_pos
if key in (keys.DOWN, keys.UP, keys.LEFT, keys.RIGHT):
- return self._movement_key(key)
+ self._held_keys.clear()
+ self._held_keys.add(key)
+ # We do this so pressing the key has an instant effect, and can then be held
+ self._last_key_down = self._key_rate + 0.01
+ return self._movement_key(key, 0.01)
elif key == keys.C:
return self._activate_key()
elif key == keys.X:
- return self._fire_key()
+ self._held_keys.clear()
+ self._held_keys.add(key)
+ self._last_key_down = self._key_rate + 0.01
+ return self._fire_key(0.01)
elif key == keys.V:
return self._change_vehicle()
elif key == keys.Z:
return self._vehicle_management_key()
return super(GameLevelScene, self).on_key_down(key, mod, unicode)
+
+ def on_key_up(self, key, mode):
+ self._held_keys.discard(key)
+ self._last_key_down = 0