Commit ad208926 authored by Nelso Jost's avatar Nelso Jost

ADD: basic code for maze lines

parent 0c44db64
from paigamu import *
import serial
import time
class AppLabirintoGame(GameApp):
......@@ -9,13 +8,8 @@ class AppLabirintoGame(GameApp):
def on_setup(self):
self.main_scene = MainScene(app=self, surface=self.screen)
self.joystick_respirometro = JoystickRespirometro()
# self.timer = Timer(dt=0.5, callback=lambda: print("Oi"))
def on_gameloop(self):
# self.timer.update()
self.main_scene.update()
self.main_scene.draw()
......@@ -23,6 +17,8 @@ class AppLabirintoGame(GameApp):
if event.key == pygame.K_SPACE:
self.main_scene['player'].centery = self.main_scene.rect.centery
# self.main_scene['player'].handle_keydown()
class MainScene(Scene):
......@@ -33,23 +29,46 @@ class MainScene(Scene):
size=(50, 50),
left=100,
centery=self.rect.centery,
vel=(0, 100))
vel=(0, 400),
control_with='keyboard',
keymap={'up': 'K_w', 'down': 'K_s'})
self['top_maze'] = MazeSprite(
points=[(0, 200), (200, 0), (400, 200), (600, 0), (800, 200)],
line_color='white',
vel=[-100, 0])
self['top_maze'].shift(dy=50)
self['bottom_maze'] = MazeSprite(
points=[(0, 400), (200, 200), (400, 400), (600, 200), (800, 400)],
line_color='white',
vel=[0, 0])
self['bottom_maze'].shift(dy=50)
self.last_maze = self['top_maze']
def on_gameloop(self):
if self.last_maze[-1].x <= self.rect.right:
if 'bottom_maze' in self:
del self['bottom_maze']
else:
self.old_maze = self.last_maze
self.last_maze = self['new_top_maze'] = MazeSprite(
points=[(0, 200), (200, 0), (400, 200), (600, 0), (800, 200)],
line_color='white',
vel=[-100, 0])
self['new_top_maze'].shift(dx=self.rect.w, dy=50)
if self.
# self.old_maze = self['top_maze']
# self.last_maze = self['top_maze2']
class Timer:
def __init__(self, dt, callback):
self.current_time = time.time()
self.dt, self.callback = dt, callback
self.elapsed = 0
class MazeSprite(PathSprite):
running_x = True
def update(self):
t = time.time()
self.elapsed += t - self.current_time
self.current_time = t
if self.elapsed > self.dt:
self.callback()
self.elapsed = 0
# print('elapsed', self.elapsed)
self.run(self.app.dt)
class PlayerSprite(Sprite):
......@@ -57,11 +76,13 @@ class PlayerSprite(Sprite):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.handle_controls = {'joystick': self.handle_joystick,
'keyboard': self.handle_keypress}\
[kwargs.get('control_with', 'keyboard')]
self.joystick_respirometro = JoystickRespirometro()
def update(self, dt):
self.handle_joystick()
def update(self):
self.handle_controls()
if self.running:
self.vel.y += self.app.dt * self.accel.y
self.y += self.app.dt * self.vel.y
......@@ -72,23 +93,23 @@ class PlayerSprite(Sprite):
if event.key >= pygame.K_UP:
self.vel.j, self.running = -1, True
def handle_keypressed(self):
if self.app.is_key_pressed('K_DOWN'):
def handle_keypress(self):
if self.app.is_key_pressed(self.keymap['down']):
self.vel.j, self.running = 1, True
elif self.app.is_key_pressed('K_UP'):
elif self.app.is_key_pressed(self.keymap['up']):
self.vel.j, self.running = -1, True
else:
self.running = False
def handle_joystick(self):
_MAX_VEL = 300
self.app.joystick_respirometro.update()
self.joystick_respirometro.update()
if self.app.joystick_respirometro.inhaling():
if self.joystick_respirometro.inhaling():
self.accel.y = 200
self.vel.j, self.running = 1, True
# print('inhaling', self.pos, self.vel, self.app.dt)
elif self.app.joystick_respirometro.exhaling():
elif self.joystick_respirometro.exhaling():
self.accel.y = -200
self.vel.j, self.running = -1, True
# print('exhaling', self.pos, self.vel, self.app.dt)
......@@ -108,8 +129,14 @@ class JoystickRespirometro:
def __init__(self):
self._state = 0
self._last_read = 0
self._sercon = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
print(self._sercon)
try:
self._sercon = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
print(self._sercon)
self._enabled = True
except:
print("Unable to open serial port at '/dev/ttyACM0'. "
"Joystick will be disabled.")
self._enabled = False
self.timer = Timer(dt=0.2, callback=self.refresh)
def inhaling(self):
......@@ -119,11 +146,14 @@ class JoystickRespirometro:
return self._state == self._STATES['ex']
def refresh(self):
if not self._enabled: return
self._sercon.write(b'p')
try:
raw_read = self._sercon.readline()
read = int(raw_read.decode('ascii'))
except:
print('[FAIL] unable to read from serial on refresh')
return
dv = (read - self._last_read)
......@@ -139,7 +169,8 @@ class JoystickRespirometro:
print(read, dv, self._STATESR[self._state])
def update(self):
self.timer.update()
if self._enabled:
self.timer.update()
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment