-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrabble_pretest.py
367 lines (323 loc) · 13.8 KB
/
scrabble_pretest.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#! /usr/bin/env python
import pygame
import string
import csv
import os
import sys
import random
from timeit import default_timer as timer
# Constants
color_font = (30, 30, 30)
background_color = (255, 255, 255)
box_edge_color = (0, 0, 0)
box_back_color = (240, 240, 240)
box_text_color = (100, 100, 100)
button_edge_color = (0, 0, 0)
button_back_color = (200, 200, 200)
button_text_color = (40, 40, 40)
incorrect_color = (255, 0, 40)
correct_color = (50, 255, 0)
counter_text_color = (150, 150, 150)
box_width = 250
box_height = 60
button_width = 150
button_height = 35
screen_w = 640
screen_h = 500
fps = 60
screen = pygame.display.set_mode((screen_w, screen_h), pygame.HWSURFACE |
pygame.DOUBLEBUF | pygame.FULLSCREEN)
class Stimulus:
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width() / 2
self.y = surface.get_height() / 2
self.font = pygame.font.Font(None, 60)
def draw_letterset(self, letters):
self.surface.fill(background_color)
text = self.font.render(letters, 1, color_font)
self.surface.blit(text, (self.x - 110, self.y - 150))
class Input:
"""This class takes care of user input"""
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width() / 2
self.y = surface.get_height() / 2
self.current_string = []
self.previous_string = ""
self.past_correct_rt = []
self.past_incorrect_rt = []
self.past_correct_words = []
self.past_incorrect_words = []
self.begin_word = 0
self.end_word = 0
self.n_correct_words = 0
self.n_incorrect_words = 0
self.prev_n_correct = 0
self.prev_n_incorrect = 0
self.total_correct_words = 0
self.total_incorrect_words = 0
self.font = pygame.font.Font(None, 50)
self.font_feedback = pygame.font.Font(None, 40)
self.font_counter = pygame.font.Font(None, 40)
def draw_text_box(self, message):
pygame.draw.rect(self.surface, box_back_color,
((self.x - (box_width / 2)), self.y,
box_width, box_height), 0)
pygame.draw.rect(self.surface, box_edge_color,
((self.x - (box_width / 2)), self.y,
box_width, box_height), 1)
if len(message) != 0:
self.surface.blit(self.font.render(message, 1, box_text_color),
(self.x - 100, self.y + 10))
pygame.display.flip()
def draw_input(self, correct):
self.text = self.font_counter.render("Score: " + str(self.total_correct_words), 1, color_font)
self.textpos = self.text.get_rect(topleft=(10, 10))
self.surface.blit(self.text, self.textpos)
"""Draw user input"""
while True:
event = pygame.event.poll()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
self.current_string = self.current_string[:-1]
elif event.key == pygame.K_RETURN:
self.end_word = timer()
self.checker(string.join(self.current_string, ""),
correct)
self.current_string = []
elif event.key <= 127:
self.current_string.append(chr(event.key))
elif event.type == pygame.MOUSEBUTTONUP:
self.prev_n_correct = self.n_correct_words
self.prev_n_incorrect = self.n_incorrect_words
self.n_correct_words = 0
self.n_incorrect_words = 0
self.current_string = []
if self.x - (button_width / 2) <= event.pos[0] <= \
self.x + (button_width / 2) and \
self.y + 100 <= event.pos[1] <= self.y + 100 + \
button_height:
break
self.draw_text_box(string.join(self.current_string, ""))
def checker(self, word, cor_words):
correct = cor_words
word = word
# check whether this word has been entered earlier
if word in self.past_correct_words:
self.surface.blit(self.font_feedback.render("Dit woord heb je "
"al gehad",
1, incorrect_color),
(self.x - 160, self.y - 70))
pygame.display.flip()
pygame.time.delay(800)
pygame.draw.rect(self.surface, (255, 255, 255),
((self.x - 200), self.y - 80,
400, 60), 0)
# check whether this word is in the correct words list
elif word in correct:
self.surface.blit(self.font_feedback.render("Correct!",
1, correct_color),
(self.x - 55, self.y - 70))
self.past_correct_rt.append(self.end_word - self.begin_word)
self.past_correct_words.append(word)
self.n_correct_words = self.n_correct_words + 1
self.total_correct_words = self.total_correct_words + 1
pygame.display.flip()
pygame.time.delay(800)
pygame.draw.rect(self.surface, (255, 255, 255), (5, 5, 140, 30), 0)
pygame.draw.rect(self.surface, (255, 255, 255), ((self.x - 200), self.y - 80, 400, 60), 0)
self.text = self.font_counter.render("Score: " + str(self.total_correct_words), 1, color_font)
self.surface.blit(self.text, self.textpos)
# else this is not a correct word
else:
self.surface.blit(self.font_feedback.render("Incorrect",
1, incorrect_color),
(self.x - 60, self.y - 70))
self.past_incorrect_rt.append(self.end_word - self.begin_word)
self.past_incorrect_words.append(word)
self.n_incorrect_words = self.n_incorrect_words + 1
self.total_incorrect_words = self.total_incorrect_words + 1
pygame.display.flip()
pygame.time.delay(800)
pygame.draw.rect(self.surface, (255, 255, 255),
((self.x - 200), self.y - 80,
400, 60), 0)
self.begin_word = timer()
class Wait:
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width() / 2
self.y = surface.get_height() / 2
self.font = pygame.font.Font(None, 25)
def intro(self, image):
self.surface.fill(background_color)
self.surface.blit(image, (50, 50))
pygame.display.flip()
while True:
event = pygame.event.poll()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
return
def waiter(self, time):
self.surface.fill(background_color)
text = self.font.render("", 1,
color_font)
self.surface.blit(text, (self.x - 140, self.y))
pygame.display.flip()
pygame.time.delay(time)
self.surface.fill(background_color)
def outro(self):
self.surface.fill(background_color)
text = self.font.render("Dat was de eerste scrabbletaak!",
1, color_font)
self.surface.blit(text, (self.x - 140, self.y - 30))
text = self.font.render("Druk op de spatiebalk om verder te gaan",
1, color_font)
self.surface.blit(text, (self.x - 170, self.y + 20))
pygame.display.flip()
while True:
event = pygame.event.poll()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
return
class Button:
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width() / 2
self.y = surface.get_height() / 2
self.font = pygame.font.Font(None, 30)
def next_set(self):
pygame.draw.rect(self.surface, button_back_color,
((self.x - (button_width / 2)), self.y + 100,
button_width, button_height), 0)
pygame.draw.rect(self.surface, button_edge_color,
((self.x - (button_width / 2)), self.y + 100,
button_width, button_height), 1)
text = self.font.render("volgende set", 1, color_font)
screen.blit(text, (self.x - button_width / 2 + 12, self.y + 105))
pygame.display.flip()
class Main:
def __init__(self, letters, words, expStartTime, subjectID, condition, wait_time):
# Init data collection
self.start_time = timer()
self.expStartTime = expStartTime
self.subjectID = subjectID
self.condition = condition
self.total_correct_n = 0
self.total_incorrect_n = 0
self.correct_input = []
self.incorrect_input = []
self.set_counter = -1
self.wait_time = wait_time
self.subjectID = self.zerofill(self.subjectID, 4)
filename = os.path.join("output", "ERC_WP3_Year1_Study1_") + str(self.subjectID) + "_" + str(self.expStartTime) + "_scrabble_pretest"+".txt"
f = open(filename, 'w')
output = 'expStartTime;subjectID;condition;nth_set;letterset;time_start;time_end;' \
'time_in_set;correct_n;correct_words;rt_correct_words;incorrect_n;' \
'incorrect_words;rt_incorrect_words\n'
f.write(output)
f.close()
# Init task
pygame.init()
self.surface = screen
self.surface.fill(background_color)
self.letters = letters
self.word = words
# Initiate objects
self.indicator = list(range(len(stimulus_set)))
self.stimulus = Stimulus(screen)
self.user_input = Input(screen)
self.wait = Wait(screen)
self.button = Button(screen)
def main(self):
# Main loop
clock = pygame.time.Clock()
image_intro01 = pygame.image.load(os.path.join("images", "intro_scrabble_pretest01.png")).convert()
image_intro02 = pygame.image.load(os.path.join("images", "intro_scrabble_pretest02.png")).convert()
self.wait.intro(image_intro01)
self.wait.intro(image_intro02)
self.user_input.begin_word = timer()
for number in self.indicator:
"""Loop through letter sets and check input until next set
button is clicked. Repeat until last set has been shown"""
self.begin = timer()
self.set_counter += 1
self.stimulus.draw_letterset(self.letters[number])
self.button.next_set()
self.user_input.draw_input(correct_words[number])
self.correct_input.append(self.user_input.past_correct_words)
self.incorrect_input.append(self.user_input.past_incorrect_words)
self.time = (timer() - self.begin)
self.write_data(number)
self.user_input.past_correct_rt = []
self.user_input.past_incorrect_rt = []
self.user_input.past_correct_words = []
self.user_input.past_incorrect_words = []
if self.set_counter + 1 == len(self.indicator): # Check if this was the final set
self.wait.waiter(time=self.wait_time)
image_outro01 = pygame.image.load(os.path.join("images", "intro_scrabble_pretest03.png")).convert()
self.wait.intro(image_outro01)
else:
self.wait.waiter(time=self.wait_time)
screen.fill(background_color)
clock.tick(fps)
def zerofill(self, number, width):
width -= len(str(number))
if (width > 0) :
i = 0
while i < width:
number = str(number).join("0")+ str(number)
i += 1
return number
def write_data(self, number, filename=None):
if filename is None:
filename = os.path.join("output", "ERC_WP3_Year1_Study1_") + str(self.subjectID) + "_" + str(self.expStartTime) + "_scrabble_pretest"+".txt"
f = open(filename, 'a')
output = str(self.expStartTime) + ";" + \
str(self.subjectID) + ";" + \
str(self.condition) + ";" + \
str(self.set_counter + 1) + ";" + \
str(self.letters[number]) + ";" + \
str(self.begin) + ";" + \
str(timer()) + ";" + \
str(self.time) + ";" + \
str(self.user_input.prev_n_correct) + ";" + \
str(self.correct_input[self.set_counter]) + ";" + \
str(self.user_input.past_correct_rt) + ";" + \
str(self.user_input.prev_n_incorrect) + ";" + \
str(self.incorrect_input[self.set_counter]) + ";" + \
str(self.user_input.past_incorrect_rt) + "\n"
f.write(output)
f.close()
if __name__ == '__main__':
debug = sys.argv[4]
if debug == "f":
wait_time = 15000
else:
wait_time = 100
expStartTime = sys.argv[1]
subject_ID = sys.argv[2]
if sys.argv[3] == "r":
if int(subject_ID) % 2 == 1:
condition = "c"
else:
condition = "d"
else:
condition = sys.argv[3]
# Read letter set file
stimulus_set = []
with open(os.path.join('csv_lettersets', 'lettersets_pretest.csv'), 'rb') as csvfile:
letters = csv.reader(csvfile, delimiter=' ')
for row in letters:
stimulus_set.append(' '.join(row))
# Read correct words file
correct_words = []
with open(os.path.join('csv_lettersets', 'words_pretest.csv'), 'rU') as f:
reader = csv.reader(f, delimiter=' ')
for row in reader:
correct_words.append(' '.join(row))
for i in range(len(correct_words)):
correct_words[i] = correct_words[i].split(',')
run = Main(stimulus_set, correct_words, expStartTime, subject_ID, condition, wait_time)
run.main()