This commit is contained in:
Frank DeMarco 2018-06-27 21:21:51 -04:00
parent bda97667fc
commit 9e84a17b8d
3 changed files with 48 additions and 3 deletions

40
NS.py
View File

@ -5,6 +5,8 @@ from math import pi
from copy import copy
from glob import iglob
from os.path import basename
from threading import Thread
from serial import Serial
from pygame import Surface, Color
from pygame.event import clear
@ -77,6 +79,13 @@ class NS(Game, Animation):
self.dialogue = Dialogue(self)
self.chemtrails = Chemtrails(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.register(self.blink_score, interval=500)
self.play(self.blink_score)
@ -89,6 +98,26 @@ class NS(Game, Animation):
for path in iglob(self.get_resource("sfx/") + "*.wav"):
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):
self.score_hidden = False
self.idle_elapsed = 0
@ -118,7 +147,7 @@ class NS(Game, Animation):
def respond(self, event):
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
lights = self.platform.lights
self.idle_elapsed = 0
@ -139,6 +168,8 @@ class NS(Game, Animation):
def update(self):
Animation.update(self)
if self.serial_enabled():
self.apply_serial()
self.title.update()
self.introduction.update()
self.ending.update()
@ -1060,6 +1091,10 @@ class Boss(Animation):
self.visitor_avatar = load(self.get_resource("Visitor_avatar.png")).convert()
self.spoopy_avatar = load(self.get_resource("Spoopy_avatar.png")).convert()
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):
if self.level_index == 0:
@ -1375,7 +1410,8 @@ class Boss(Animation):
def update(self):
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
if dialogue.active:
if self.advance_prompt.check_first_press():

4
config
View File

@ -24,4 +24,6 @@ up = K_u
sfx-volume = .8
[input]
buffer = 200
buffer = 0
arduino-port = /dev/ttyACM0
serial = yes

View File

@ -13,3 +13,10 @@
177012
126061
157603
136643
177268
151171
131298
130375
134183
174976