-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBoxForQuestions.py
More file actions
36 lines (32 loc) · 1.38 KB
/
TextBoxForQuestions.py
File metadata and controls
36 lines (32 loc) · 1.38 KB
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
from Constants import BLACK,WHITE
from TextBoxBase import TextBoxBase
from InvalidArgumentException import InvalidArgumentException
import inspect
import pygame
class TextBoxForQuestions(TextBoxBase):
def __init__(self, x, y, width, height, action, correct_answer):
super().__init__(x,y,width,height)
signature = inspect.signature(action)
if len(signature.parameters) != 2:
raise InvalidArgumentException
self.action = action
self.correct_answer = correct_answer
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.active = not self.active
else:
self.active = False
self.color = WHITE if self.active else BLACK
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
found_answer=self.action(self.text,self.correct_answer)
self.text = ''
self.txt_surface = self.font.render(self.text, True, BLACK)
return found_answer
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
self.txt_surface = self.font.render(self.text, True, BLACK)