serial
This commit is contained in:
parent
bda97667fc
commit
9e84a17b8d
40
NS.py
40
NS.py
|
@ -5,6 +5,8 @@ from math import pi
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from glob import iglob
|
from glob import iglob
|
||||||
from os.path import basename
|
from os.path import basename
|
||||||
|
from threading import Thread
|
||||||
|
from serial import Serial
|
||||||
|
|
||||||
from pygame import Surface, Color
|
from pygame import Surface, Color
|
||||||
from pygame.event import clear
|
from pygame.event import clear
|
||||||
|
@ -77,6 +79,13 @@ class NS(Game, Animation):
|
||||||
self.dialogue = Dialogue(self)
|
self.dialogue = Dialogue(self)
|
||||||
self.chemtrails = Chemtrails(self)
|
self.chemtrails = Chemtrails(self)
|
||||||
self.boss = Boss(self)
|
self.boss = Boss(self)
|
||||||
|
self.get_configuration().type_declarations.add("bool", "input", "serial")
|
||||||
|
if self.serial_enabled():
|
||||||
|
self.serial_kill = False
|
||||||
|
self.serial_data = 0
|
||||||
|
self.serial_reader = Serial(self.get_configuration("input", "arduino-port"), timeout=.1)
|
||||||
|
self.serial_thread = Thread(target=self.read_serial)
|
||||||
|
self.serial_thread.start()
|
||||||
self.last_press = get_ticks()
|
self.last_press = get_ticks()
|
||||||
self.register(self.blink_score, interval=500)
|
self.register(self.blink_score, interval=500)
|
||||||
self.play(self.blink_score)
|
self.play(self.blink_score)
|
||||||
|
@ -89,6 +98,26 @@ class NS(Game, Animation):
|
||||||
for path in iglob(self.get_resource("sfx/") + "*.wav"):
|
for path in iglob(self.get_resource("sfx/") + "*.wav"):
|
||||||
sfx[basename(path.split(".")[0])] = SoundEffect(self, path)
|
sfx[basename(path.split(".")[0])] = SoundEffect(self, path)
|
||||||
|
|
||||||
|
def serial_enabled(self):
|
||||||
|
return self.get_configuration("input", "serial") and not self.check_command_line("-no-serial")
|
||||||
|
|
||||||
|
def read_serial(self):
|
||||||
|
while not self.serial_kill:
|
||||||
|
output = self.serial_reader.readline().strip()
|
||||||
|
if output:
|
||||||
|
self.serial_data = int(output, 2)
|
||||||
|
else:
|
||||||
|
self.serial_data = 0
|
||||||
|
|
||||||
|
def end(self, evt):
|
||||||
|
if evt.type == QUIT or self.delegate.compare(evt, "quit"):
|
||||||
|
self.serial_kill = True
|
||||||
|
Game.end(self, evt)
|
||||||
|
|
||||||
|
def apply_serial(self):
|
||||||
|
for ii, light in enumerate(self.platform.lights):
|
||||||
|
light.pressed = bool(self.serial_data & (2 ** ii))
|
||||||
|
|
||||||
def reset(self, leave_wipe_running=False):
|
def reset(self, leave_wipe_running=False):
|
||||||
self.score_hidden = False
|
self.score_hidden = False
|
||||||
self.idle_elapsed = 0
|
self.idle_elapsed = 0
|
||||||
|
@ -118,7 +147,7 @@ class NS(Game, Animation):
|
||||||
|
|
||||||
def respond(self, event):
|
def respond(self, event):
|
||||||
if not self.suppressing_input and event.type in (KEYDOWN, KEYUP):
|
if not self.suppressing_input and event.type in (KEYDOWN, KEYUP):
|
||||||
# if self.last_press <= get_ticks() - int(self.get_configuration("input", "buffer")):
|
if self.last_press <= get_ticks() - int(self.get_configuration("input", "buffer")):
|
||||||
pressed = True if event.type == KEYDOWN else False
|
pressed = True if event.type == KEYDOWN else False
|
||||||
lights = self.platform.lights
|
lights = self.platform.lights
|
||||||
self.idle_elapsed = 0
|
self.idle_elapsed = 0
|
||||||
|
@ -139,6 +168,8 @@ class NS(Game, Animation):
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
Animation.update(self)
|
Animation.update(self)
|
||||||
|
if self.serial_enabled():
|
||||||
|
self.apply_serial()
|
||||||
self.title.update()
|
self.title.update()
|
||||||
self.introduction.update()
|
self.introduction.update()
|
||||||
self.ending.update()
|
self.ending.update()
|
||||||
|
@ -1060,6 +1091,10 @@ class Boss(Animation):
|
||||||
self.visitor_avatar = load(self.get_resource("Visitor_avatar.png")).convert()
|
self.visitor_avatar = load(self.get_resource("Visitor_avatar.png")).convert()
|
||||||
self.spoopy_avatar = load(self.get_resource("Spoopy_avatar.png")).convert()
|
self.spoopy_avatar = load(self.get_resource("Spoopy_avatar.png")).convert()
|
||||||
self.advance_prompt = AdvancePrompt(self)
|
self.advance_prompt = AdvancePrompt(self)
|
||||||
|
self.backgrounds = [Sprite(self), Sprite(self), Sprite(self)]
|
||||||
|
self.backgrounds[0].load_from_path(self.get_resource("local/bg001.png"))
|
||||||
|
self.backgrounds[1].load_from_path(self.get_resource("local/bg002.png"))
|
||||||
|
self.backgrounds[2].load_from_path(self.get_resource("local/bg003.png"))
|
||||||
|
|
||||||
def cancel_flash(self):
|
def cancel_flash(self):
|
||||||
if self.level_index == 0:
|
if self.level_index == 0:
|
||||||
|
@ -1375,7 +1410,8 @@ class Boss(Animation):
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
if self.active:
|
if self.active:
|
||||||
self.get_display_surface().fill((0, 0, 0))
|
self.backgrounds[self.level_index].update()
|
||||||
|
# self.get_display_surface().fill((0, 0, 0))
|
||||||
dialogue = self.get_game().dialogue
|
dialogue = self.get_game().dialogue
|
||||||
if dialogue.active:
|
if dialogue.active:
|
||||||
if self.advance_prompt.check_first_press():
|
if self.advance_prompt.check_first_press():
|
||||||
|
|
4
config
4
config
|
@ -24,4 +24,6 @@ up = K_u
|
||||||
sfx-volume = .8
|
sfx-volume = .8
|
||||||
|
|
||||||
[input]
|
[input]
|
||||||
buffer = 200
|
buffer = 0
|
||||||
|
arduino-port = /dev/ttyACM0
|
||||||
|
serial = yes
|
||||||
|
|
|
@ -13,3 +13,10 @@
|
||||||
177012
|
177012
|
||||||
126061
|
126061
|
||||||
157603
|
157603
|
||||||
|
136643
|
||||||
|
177268
|
||||||
|
151171
|
||||||
|
131298
|
||||||
|
130375
|
||||||
|
134183
|
||||||
|
174976
|
||||||
|
|
Loading…
Reference in New Issue