-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.py
161 lines (126 loc) · 4.47 KB
/
level.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
import random
import time
import pygame as pg
from reaction import WrongNumber, NoNumber, Timeout, Correct, CorrectPicture, DeadSheep, Winning, LevelProgression
class LevelInterface:
def start(self):
pass
def is_started(self) -> bool:
pass
def check_time_left(self) -> int:
pass
def register_answer(self, answer: str, all_objects: pg.sprite.Group) -> pg.sprite.Group:
pass
def __on_wrong_number(self):
pass
def __on_no_number(self):
pass
def __on_timeout(self):
pass
def __on_correct_answer(self, all_objects: pg.sprite.Group):
pass
def get_amount_of_objects(self) -> int:
pass
def is_stopped(self) -> bool:
pass
def get_question(self) -> str:
pass
def reset(self):
pass
def __on_winning(self):
pass
def __check_for_level_progression(self) -> bool:
pass
def __on_level_progression(self):
pass
class GoatLevel(LevelInterface):
def __init__(self, question: str, difficulty: int):
self._startLevelTime: int = -1
self._stopLevelTime: int = -1
self.answer: str = ""
self.question = question
self.difficulty = difficulty
self._amountOfObjects = random.randint(difficulty * 10 + 5, difficulty * 20 + 10)
self._initialTimer = 10
self.number_correct = 0
self.number_incorrect = 0
def start(self):
self._startLevelTime = int(time.time())
return
def is_started(self) -> bool:
return self._startLevelTime != -1
def check_time_left(self) -> int:
if self._stopLevelTime != -1:
return self._initialTimer - (self._stopLevelTime - self._startLevelTime)
elif int(time.time()) - self._startLevelTime >= self._initialTimer:
self._stopLevelTime = int(time.time())
self.__on_timeout()
return 0
return self._initialTimer - (int(time.time()) - self._startLevelTime)
def register_answer(self, answer: str, all_objects: pg.sprite.Group):
self._stopLevelTime = int(time.time())
self.answer = answer
if not answer.isdigit():
self.__on_no_number()
return all_objects
elif f'{int(self._amountOfObjects):0b}' == answer:
self.__on_winning()
return all_objects
elif int(answer) == self._amountOfObjects:
return self.__on_correct_answer(all_objects)
else:
self.__on_wrong_number()
return all_objects
def __on_wrong_number(self):
self.number_incorrect += 1
if self.__check_for_level_progression():
self.__on_level_progression()
else:
WrongNumber().execute()
return
def __on_no_number(self):
self.number_incorrect += 1
if self.__check_for_level_progression():
self.__on_level_progression()
else:
NoNumber().execute()
return
def __on_timeout(self):
self.number_incorrect += 1
if self.__check_for_level_progression():
self.__on_level_progression()
else:
Timeout().execute()
return
def __on_correct_answer(self, all_objects: pg.sprite.Group) -> pg.sprite.Group:
self.number_correct += 1
return random.choice([Correct(all_objects), CorrectPicture(all_objects), DeadSheep(all_objects)]).execute()
def get_amount_of_objects(self) -> int:
return self._amountOfObjects
def is_stopped(self) -> bool:
return self._stopLevelTime != -1
def get_question(self) -> str:
return self.question
def get_number_correct(self) -> int:
return self.number_correct
def reset(self):
self._startLevelTime: int = -1
self._stopLevelTime: int = -1
self.answer: str = ""
self._amountOfObjects = random.randint(self.difficulty * 10 + 5, self.difficulty * 20 + 10)
self.start()
def __on_winning(self):
Winning().execute()
return
def __check_for_level_progression(self) -> bool:
return self.number_incorrect != 0 and self.number_incorrect % 5 == 0
def __on_level_progression(self):
next_level = int(self.number_incorrect/5) % 3
if next_level == 1:
self.difficulty = 0
elif next_level == 2:
self.difficulty = 10
elif next_level == 0:
self.difficulty = 1
LevelProgression(next_level).execute()
return