-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserialpygame.py
194 lines (156 loc) · 5.35 KB
/
serialpygame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Code for the display for the Bloominglabs/Wonderlab Laser Tunnel
SDC 8/21/2014
TODO:
"""
import pygame
from pygame import *
import pygame.mixer
from credits import Credit
import sys
import time
import serial
text = """CREDITS
_ _
_Construction Team_\\Jay Sissom
\\Stephen Charlesworth
\\Naomi Charlesworth
\\Kristy Kallback-Rose
\\David Rose
\\Tomoko Oishi
_Arduino and Raspberry Pi Programming_\\Jay Sissom
\\Stephen Charlesworth
_ _
©Copyright 2014 by Bloominglabs
http://bloominglabs.org
For Wonderlab"""
#~ utiliser '\\' pour aligner les lignes de texte
# key constants
DISPLAY_WIDTH = 990
DISPLAY_HEIGHT = 500
DISPLAY_SURF = display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
FPS = 30
NUM_SENSORS = 6
SENSOR_STATE = [0] * NUM_SENSORS
#serial stuff. for debugging purposes also allows keyboard input
SERIAL_MODE = False
SERIAL_PORT = "/dev/ttyACM0" #"/dev/tty.usbmodem1421" # change as appropriate
# Fonts
small_font = font.Font("./fonts/Roboto-MediumItalic.ttf",20)
medium_font = font.Font("./fonts/Roboto-MediumItalic.ttf",32)
big_font = font.Font("./fonts/Roboto-MediumItalic.ttf",64)
huge_font = font.Font("./fonts/Roboto-MediumItalic.ttf",80)
big_computer_font = font.Font("./fonts/Computer.ttf",100)
medium_computer_font = font.Font("./fonts/Computer.ttf",35)
big_LCD_font = font.Font("./fonts/LCD2 Bold.ttf",80)
# Images
wonderlab_img = pygame.image.load("./pix/wonderlab400x128.png")
blabs_img = pygame.image.load("./pix/blabsweirdfont990x180.png")
#sounds - pre init solved laggy sounds (mostly)
pygame.mixer.pre_init(44100,-16,2 ,256)
pygame.init()
pygame.mixer.music.set_volume(1)
click = pygame.mixer.Sound("./sounds/goodClick.ogg")
motion_detected = pygame.mixer.Sound("./sounds/motionDetected.ogg")
activating_alarm = pygame.mixer.Sound("./sounds/activatingAlarm.ogg")
intruder_alert = pygame.mixer.Sound("./sounds/IntruderAlert.ogg")
def draw_splash(display_surf):
display_surf.fill((255,255,255))
display_surf.blit(blabs_img,(0,0))
display_surf.blit(wonderlab_img,(display_surf.get_size()[0]/2 -
wonderlab_img.get_size()[0]/2,240))
time_surface_obj = big_font.render('and',True,(0,0,0),(255,255,255))
text_rect_obj = time_surface_obj.get_rect()
text_rect_obj.center = (
display_surf.get_size()[0]/2 ,200)
display_surf.blit(time_surface_obj,text_rect_obj)
time_surface_obj = big_font.render('present...',True,(0,0,0),(255,255,255))
text_rect_obj = time_surface_obj.get_rect()
text_rect_obj.center = (
display_surf.get_size()[0]/2 ,
display_surf.get_size()[1] - text_rect_obj.h)
display_surf.blit(time_surface_obj,text_rect_obj)
pygame.display.update()
time.sleep(2)
display_surf.fill((0,0,0))
time_surface_obj = big_computer_font.render('Laser',True,(0,255,0),(0,0,0))
text_rect_obj = time_surface_obj.get_rect()
text_rect_obj.center = (
display_surf.get_size()[0]/2 ,
display_surf.get_size()[1]/3)
display_surf.blit(time_surface_obj,text_rect_obj)
time_surface_obj = big_computer_font.render('Tunnel',True,(0,255,0),(0,0,0))
text_rect_obj = time_surface_obj.get_rect()
text_rect_obj.center = (
display_surf.get_size()[0]/2 ,
display_surf.get_size()[1]*2/3)
display_surf.blit(time_surface_obj,text_rect_obj)
pygame.display.update()
time.sleep(2)
def get_serial_command(serial_port,stuff):
line = ""
ed = serial_port.read(10)
while ed:
print "serial read: [%s]" % ed
stuff = stuff + ed
idx = stuff.find("\n")
if idx > -1:
line = stuff[:idx]
print "got a line: [%s]" % line
print "line has len [%s]" % len(line)
if len(line):
print "first char: [%s]" % line[0]
line = line[0]
stuff = stuff[idx+1:]
break
ed = serial_port.read(10)
return (stuff, line)
FPS_CLOCK = pygame.time.Clock()
cred = Credit(text,small_font,(255,255,255),(0,0,0))
pygame.display.set_caption('Laser Tunnel')
DISPLAY_SURF.fill((255,255,255))
#draw_splash(DISPLAY_SURF)
GAME_STATE = "ATTRACT"
# also ARMED DISARMED ALERT
GAME_TIME = 0
try:
serial_port = serial.Serial(SERIAL_PORT, 9600, timeout = 0)
SERIAL_MODE = True
print "Serial be live"
# clean shit out
while serial_port.read(10):
pass
except:
pass
serial_stuff = ""
"""
commands
A 'arm' (red button)
D 'disarm' (green button)
1 - 8 (indicates a line was touched)
X - quit
"""
armed = False
alert = False
time_increment = 0
while True: # main game loop
current_key = ""
current_serial = ""
serial_command = ""
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYUP:
current_key = str(unichr(event.key))
# play the key sound
print "%s pressed" % current_key
if SERIAL_MODE == True:
(serial_stuff, serial_command) = get_serial_command(serial_port,serial_stuff)
if serial_command:
print "Serial: [%s] %s" % (serial_command,len(serial_command))
current_key = serial_command
time_increment = FPS_CLOCK.tick(FPS)
pygame.display.update()