-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_app.py
141 lines (108 loc) · 4.3 KB
/
main_app.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
from multiprocessing import dummy
from kivy.core.window import Window
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.properties import NumericProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivymd.app import MDApp
import random
import time
random.seed(time.process_time())
#Designate .kv design file
Builder.load_file('attentionapp.kv')
class AttentionGame(FloatLayout):
clickCount = NumericProperty(0)
def __init__(self, **kwargs):
super().__init__(**kwargs)
class StartScreen(Screen):
def on_enter(self):
brainLabel = Label(text="Find the " + attentionApp.brainColor + " brain!", pos=(15,100))
self.add_widget(brainLabel)
pass
class GameScreen(Screen):
def on_enter(self):
attentionApp.start = time.time()
for x in range(6):
dummy = DummyBrain()
self.add_widget(dummy)
def on_touch_up(self, touch):
attentionApp.clickCount += 1
pass
class EndScreen(Screen):
def on_enter(self):
timeElapsed = Label(text="Time elapsed: " + attentionApp.timeDiff + " seconds")
timeElapsed.pos = (20, 150)
self.add_widget(timeElapsed)
numOfClicks = Label(text = "Number of clicks: " + str(attentionApp.clickCount + 1))
attentionApp.totalClicks += attentionApp.clickCount + 1
numOfClicks.pos = (20, 100)
self.add_widget(numOfClicks)
accuracy = Label(text = "Accuracy: " + str(attentionApp.rounds / attentionApp.totalClicks) + " (" + str(attentionApp.rounds) + " correct clicks / " + str(attentionApp.totalClicks) + " total clicks)")
accuracy.pos = (20, 50)
attentionApp.accuracy = attentionApp.rounds / attentionApp.totalClicks
self.add_widget(accuracy)
def restart(self):
attentionApp.restart(self)
pass
class Brain(ButtonBehavior, Image):
def __init__(self, **kwargs):
super(Brain, self).__init__(**kwargs)
self.source = attentionApp.brainColor + "brain.png"
random_x = random.uniform(0, Window.width)
random_y = random.uniform(0, Window.height)
self.pos = (random_x, random_y)
def on_press(self):
self.source = 'Brain.png'
attentionApp.end = time.time()
attentionApp.timeDiff = str(attentionApp.end - attentionApp.start)
class DummyBrain(ButtonBehavior, Image):
def __init__(self, **kwargs):
super(DummyBrain, self).__init__(**kwargs)
otherColor = random.choice(attentionApp.colorOptions)
self.source = otherColor + "brain.png"
random_x = random.uniform(0, Window.width)
random_y = random.uniform(0, Window.height)
self.pos = (random_x, random_y)
self.size_hint = .09, .09
class attentionApp(MDApp, App):
start = 0
end = 0
rounds = 1
timeDiff = ""
clickCount = 0
totalClicks = 0
colorOptions = ["yellow", "blue", "green"]
brainColor = random.choice(colorOptions)
colorOptions.remove(brainColor)
accuracy = 0
global sm
sm = ScreenManager()
def restart(self, v):
attentionApp.clickCount = 0
attentionApp.start = 0
attentionApp.end = 0
attentionApp.timeDiff = ""
attentionApp.colorOptions = ["yellow", "blue", "green"]
attentionApp.brainColor = random.choice(attentionApp.colorOptions)
attentionApp.colorOptions.remove(attentionApp.brainColor)
attentionApp.rounds += 1
sm.clear_widgets()
sm.add_widget(StartScreen(name='start'))
sm.add_widget(GameScreen(name='game'))
sm.add_widget(EndScreen(name='end'))
sm.current = 'start'
def build(self):
sm.add_widget(StartScreen(name='start'))
sm.add_widget(GameScreen(name='game'))
sm.add_widget(EndScreen(name='end'))
return sm
if __name__ == '__main__':
attentionApp = attentionApp()
attentionApp.run()