3939
4040# Text info
4141trivia_font = bitmap_font .load_font ("/fonts/Arial-ItalicMT-17.bdf" )
42- answerChoices = ("A" ,"B" ,"C" ,"D" )
43- a1f_text = ''
44- a2f_text = ''
45- a3f_text = ''
46- a4f_text = ''
47- player1_text = "Player 1!"
48- player2_text = "Player 2!"
49- timesup_text = "TIME'S UP!"
50- loading_text = "loading question..."
51- tapagain_text = "Tap again for \n next question."
52- # Text locations (screen is 320 x 240)
53- loading_position = tapagain_position = (100 ,120 )
54- player_position = timesup_position = answerReveal_position = (120 , 75 )
55- answerReveal_position = (25 , 75 )
42+
43+ loading_color = 0x8080FF
44+ loading_position = (100 ,120 )
45+ loading_text_area = label .Label (trivia_font , max_glyphs = 30 , color = loading_color ,
46+ x = loading_position [0 ], y = loading_position [1 ])
47+
48+ q_color = 0x8080FF
5649q_position = (25 , 70 )
57- a1_position = (25 , 135 )
58- a2_position = (25 , 155 )
59- a3_position = (25 , 175 )
60- a4_position = (25 , 195 )
61- loading_color = q_color = 0x8080FF
50+ q_text_area = label .Label (trivia_font , max_glyphs = 120 ,
51+ x = q_position [0 ], y = q_position [1 ],
52+ color = q_color , line_spacing = 1 )
53+
54+ answer_choices = ("A" ,"B" ,"C" ,"D" )
55+ a_positions = ((25 , 135 ), (25 , 155 ), (25 , 175 ), (25 , 195 ))
6256a_color = 0xFFFFFF
57+ ans_text_areas = []
58+ for answernum in range (4 ):
59+ ans_text_areas .append (label .Label (trivia_font , max_glyphs = 80 ,
60+ color = a_color , line_spacing = 1.5 ,
61+ x = a_positions [answernum ][0 ],
62+ y = a_positions [answernum ][1 ]))
63+
64+ reveal_position = (25 , 75 )
65+ reveal_text_area = label .Label (trivia_font , max_glyphs = 120 , color = loading_color ,
66+ x = reveal_position [0 ], y = reveal_position [1 ])
6367
64- # Clear screen of all elements but background
65- def clear_splash ():
66- for _ in range ( len ( pyportal . splash ) - 1 ):
67- pyportal . splash . pop ( )
68+ timer_position = ( 25 , 215 )
69+ timer_color = 0xFF00FF
70+ timer_text_area = label . Label ( trivia_font , max_glyphs = 20 , color = timer_color ,
71+ x = timer_position [ 0 ], y = timer_position [ 1 ] )
6872
6973# A function to shuffle trivia questions
7074def shuffle (aList ):
71- for i in range (len (aList )- 1 , 0 , - 1 ):
72- # Pick a random index from 0 to i
73- j = random .randint (0 , i + 1 )
75+ for i in range (len (aList )):
76+ j = random .randint (0 , len (aList )- 1 )
7477 # Swap arr[i] with the element at random index
7578 aList [i ], aList [j ] = aList [j ], aList [i ]
76- return aList
79+ return aList
7780
78- #convert html codes to normal text
81+ # convert html codes to normal text
7982def unescape (s ):
8083 s = s .replace (""" , "''" )
8184 s = s .replace ("'" , "'" )
@@ -84,135 +87,87 @@ def unescape(s):
8487
8588# A function to handle the timer and determine which player answers first
8689def faceOff (timerLength ):
90+ timer_text = str (timerLength ) + " seconds!"
91+ timer_text_area .text = ''
92+ timer_text_area .text = str (timer_text )
8793 timerStart = time .monotonic ()
88- print (time .monotonic () - timerStart )
8994 while time .monotonic () - timerStart < timerLength :
9095 if button1 .value :
9196 led .value = False # For debugging
9297 else : # If button 1 pressed, print player 1 on screen and exit function
93- pyportal .splash .pop () # make room for player that wins
94- player1_text_area = label .Label (trivia_font , text = player1_text , color = loading_color )
95- player1_text_area .x = player_position [0 ]
96- player1_text_area .y = player_position [1 ]
97- pyportal .splash .append (player1_text_area )
9898 led .value = True # For debugging
99+ q_text_area .text = ''
100+ reveal_text_area .text = "Player 1!"
99101 break
100102 if button2 .value :
101103 led .value = False # For debugging
102- else : # If button 2 pressed, print player 1 on screen and exit function
104+ else : # If button 2 pressed, print player 2 on screen and exit function
103105 led .value = True # For debugging
104- pyportal .splash .pop () # make room for player that wins
105- player2_text_area = label .Label (trivia_font , text = player2_text , color = loading_color )
106- player2_text_area .x = player_position [0 ]
107- player2_text_area .y = player_position [1 ]
108- pyportal .splash .append (player2_text_area ) # push 5
106+ q_text_area .text = ''
107+ reveal_text_area .text = "Player 2!"
109108 break
110- if time .monotonic () - timerStart > (timerLength - 0.5 ): # Timer runs out
111- pyportal .splash .pop () # make room for player that wins
112- timesup_text_area = label .Label (trivia_font , text = timesup_text , color = loading_color )
113- timesup_text_area .x = timesup_position [0 ]
114- timesup_text_area .y = timesup_position [1 ]
115- pyportal .splash .append (timesup_text_area )
116- print (time .monotonic () - timerStart )
117- time .sleep (0.05 ) # debounce delay
118-
119- #function to format trivia question content
120- def format_trivia ():
121- pyportal .splash .pop ()
122- pyportal .splash .append (loading_text_area )
123- # Formatting with display text library
124- q_text = unescape (value [0 ])
125- q_text_formatted = pyportal .wrap_nicely (q_text , 35 )
126- qf_text = ''
127- for wrp in range (len (q_text_formatted )):
128- qf_text = qf_text + q_text_formatted [wrp ] + "\n "
129- q_text = qf_text
130- q_text_area = label .Label (trivia_font , text = q_text ,
131- color = q_color , line_spacing = 1 )
132- q_text_area .x = q_position [0 ]
133- q_text_area .y = q_position [1 ]
134- a1_text = unescape (value [1 ])
135- a1_text_area = label .Label (trivia_font , text = "A. " + a1_text ,
136- color = a_color , line_spacing = 1.5 )
137- a1_text_area .x = a1_position [0 ]
138- a1_text_area .y = a1_position [1 ]
139- a2_text = unescape (value [2 ])
140- a2_text_area = label .Label (trivia_font , text = "B. " + a2_text ,
141- color = a_color , line_spacing = 1.5 )
142- a2_text_area .x = a2_position [0 ]
143- a2_text_area .y = a2_position [1 ]
144- a3_text = unescape (value [3 ])
145- a3_text_area = label .Label (trivia_font , text = "C. " + a3_text ,
146- color = a_color , line_spacing = 1.5 )
147- a3_text_area .x = a3_position [0 ]
148- a3_text_area .y = a3_position [1 ]
149- a4_text = unescape (value [4 ])
150- a4_text_area = label .Label (trivia_font , text = "D. " + a4_text ,
151- color = a_color , line_spacing = 1.5 )
152- a4_text_area .x = a4_position [0 ]
153- a4_text_area .y = a4_position [1 ]
154- pyportal .splash .pop () # take off loading...
155- pyportal .splash .append (a1_text_area )
156- pyportal .splash .append (a2_text_area )
157- pyportal .splash .append (a3_text_area )
158- pyportal .splash .append (a4_text_area )
159- # append question last so it can be removed more easily later
160- pyportal .splash .append (q_text_area )
109+ time .sleep (0.05 ) # debounce delay
110+ else : # Timer runs out
111+ q_text_area .text = ''
112+ reveal_text_area .text = "Times up!"
161113
162114# PyPortal constructor
163115pyportal = PyPortal (url = DATA_SOURCE ,
116+ json_path = (Q_LOCATION , CA_LOCATION , WA_LOCATION1 , WA_LOCATION2 , WA_LOCATION3 ),
164117 status_neopixel = board .NEOPIXEL ,
165118 default_bg = cwd + "/trivia_title.bmp" )
166119
167120pyportal .preload_font () # speed things up by preloading font
168121
122+ pyportal .splash .append (loading_text_area ) #loading...
123+ pyportal .splash .append (q_text_area )
124+ pyportal .splash .append (reveal_text_area )
125+ pyportal .splash .append (timer_text_area )
126+ for textarea in ans_text_areas :
127+ pyportal .splash .append (textarea )
128+
169129while True :
170130 # Load new question when screen is touched
171- if pyportal .touchscreen .touch_point :
172- clear_splash () # clear all besides background screen
173- pyportal .set_background (cwd + "/trivia.bmp" )
174- loading_text_area = label .Label (trivia_font , text = loading_text , color = loading_color )
175- loading_text_area .x = loading_position [0 ]
176- loading_text_area .y = loading_position [1 ]
177- pyportal .splash .append (loading_text_area ) #loading...
178- answerList = [WA_LOCATION1 , WA_LOCATION2 , WA_LOCATION3 , CA_LOCATION ]
179- # For catching potential index error when shuffling question
180- try :
181- shuffle (answerList ) # Shuffle answers
182- except IndexError :
183- print ("Index Error" )
184- tapagain_text_area = label .Label (trivia_font , text = tapagain_text , color = loading_color )
185- tapagain_text_area .x = tapagain_position [0 ]
186- tapagain_text_area .y = tapagain_position [1 ]
187- pyportal .splash .pop () # take off loading...
188- pyportal .splash .append (tapagain_text_area )
189- continue
131+ while not pyportal .touchscreen .touch_point :
132+ pass
133+
134+ reveal_text_area .text = ''
135+ q_text_area .text = ''
136+ for textarea in ans_text_areas :
137+ textarea .text = ''
138+ timer_text_area .text = ''
139+
140+ pyportal .set_background (cwd + "/trivia.bmp" )
141+ loading_text_area .text = "Loading question..."
142+
143+ while True :
190144 try :
191- # set the JSON path here, now that answers are shuffled
192- # pylint: disable=protected-access
193- pyportal ._json_path = (Q_LOCATION ,
194- answerList [0 ],
195- answerList [1 ],
196- answerList [2 ],
197- answerList [3 ],)
198145 value = pyportal .fetch ()
199- print ("Response is" , value )
200- format_trivia ()
146+ break
201147 except RuntimeError as e :
202148 print ("Some error occured, retrying! -" , e )
203- faceOff (30 ) # 30 seconds with question
204- time .sleep (2 ) # pause for 2 seconds to show which player tapped first
205- faceOff (10 ) # 10 seconds to answer
206- # Show the correct answer
207- for ansr in range (len (answerList )):
208- if answerList [ansr ] is CA_LOCATION :
209- print (answerChoices [ansr ])
210- correctAnswerChoice = answerChoices [ansr ]
211- break
212- answerReveal_text = "Correct Answer: " + str (correctAnswerChoice )+ "\n (Tap for next question.)"
213- answerReveal_text_area = label .Label (trivia_font , text = answerReveal_text ,
214- color = loading_color )
215- answerReveal_text_area .x = answerReveal_position [0 ]
216- answerReveal_text_area .y = answerReveal_position [1 ]
217- pyportal .splash .pop () # Make room for answer
218- pyportal .splash .append (answerReveal_text_area )
149+ continue
150+ print ("Response is" , value )
151+ question = value [0 ]
152+ correct_answer = value [1 ]
153+ answers = shuffle (value [1 :5 ])
154+ loading_text_area .text = ''
155+
156+ # Format text and wrap with display text library
157+ try : # sometimes gives a runtime error: Group full
158+ q_text_area .text = '\n ' .join (pyportal .wrap_nicely (unescape (question ), 35 ))
159+ except RuntimeError as e :
160+ print ("Group full" , e )
161+ continue
162+ for i , answer in enumerate (answers ):
163+ ans_text_areas [i ].text = answer_choices [i ]+ ") " + unescape (answer )
164+
165+ faceOff (10 ) # 10 seconds with question
166+ time .sleep (2 ) # pause for 2 seconds to show which player tapped first
167+ faceOff (5 ) # 5 seconds to answer
168+ timer_text_area .text = ''
169+ # Show the correct answer
170+ i = answers .index (correct_answer )
171+ reveal_text = "Correct Answer:\n " + answer_choices [i ]+ ") " + unescape (answers [i ])+ "\n (Tap for next question.)"
172+ print (reveal_text )
173+ reveal_text_area .text = reveal_text
0 commit comments