summaryrefslogtreecommitdiff
path: root/master_thesis/frensch_task.py
blob: 0c6625424dee57df90873e32fccfdf62e0be75d7 (plain)
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
#!/usr/bin/env python3
from __future__ import annotations

from psychopy import constants, core, event, gui, visual
from collections import namedtuple


DisplayVariable = namedtuple("DisplayVariable", ["name", "values"])
DisplayProcedure = namedtuple("DisplayProcedure", ["procedure", "solution"])


def experiment_shutdown():
    WIN.close()
    core.quit()


WIN = visual.Window((800, 600), fullscr=True, units="pix")
MONITOR_FPS = 60

# Cancel experiment anytime with Esc
event.globalKeys.add(key="escape", func=experiment_shutdown, name="shutdown")


def generate_variable_display(varx: list[DisplayVariable], x_positions: list[int]):
    assert len(varx) == len(x_positions)

    stims = []

    def gen_value_stims(values, x, y, offset):
        for value in values:
            y -= offset
            value_stim = visual.TextBox2(
                WIN,
                pos=(x, y),
                text=value,
                # size=200,
                letterHeight=100,
                alignment="center",
            )
            stims.append(value_stim)

    y = 400
    offset = 100

    for var, x_pos in zip(varx, x_positions):
        stim_var = visual.TextBox2(
            WIN,
            pos=[x_pos, y],
            text=var.name,
            # size=[1000, 1000],
            letterHeight=50,
            alignment="center",
        )
        stims.append(stim_var)

        gen_value_stims(var.values, x_pos, y, offset)

    return stims


def generate_procedure_display(procedure: DisplayProcedure):
    stim_procedure = visual.TextBox2(
        WIN,
        pos=[-600, -200],
        text=procedure.procedure,
        # size=[1000, 1000],
        letterHeight=50,
        alignment="center",
    )
    return stim_procedure


solid = DisplayVariable("SOLID", [6])
algae = DisplayVariable("ALGAE", [8])
lime = DisplayVariable("LIME", [3, 5, 1, 9])
toxin = DisplayVariable("TOXIN", [4, 8, 7, 2])
x_positions = [-600, -300, 0, 300]
# x_positions = [-600, -300]

stims = generate_variable_display([solid, algae, lime, toxin], x_positions)

p1 = DisplayProcedure("Index 1 = Solid * (Lime_4 - Lime_2) = ", 1)
p1 = generate_procedure_display(p1)
stims.append(p1)

stim_answer_box = visual.TextBox2(
    WIN,
    "",
    letterHeight=50,
    pos=(0, -200),
    size=[150, 70],
    editable=True,
    fillColor="white",
    color="black",
    alignment="center",
)
stims.append(stim_answer_box)

not_finished = True
answer = "not answered"
while not_finished:
    stim_answer_box.hasFocus = True
    for stim in stims:
        stim.draw()
    WIN.flip()
    answer = stim_answer_box.text
    if "\n" in stim_answer_box.text:
        not_finished = False


event.waitKeys(keyList=["space"])

print(f"Answer: {answer}")