-
Notifications
You must be signed in to change notification settings - Fork 5
/
game_class_2.py
156 lines (135 loc) · 4.74 KB
/
game_class_2.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
import pygame
import random
import os
import random
font_name = pygame.font.match_font('arial')
def draw_text(text, size, x, y, mode = "midtop"):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, (0, 0, 0))
text_rect = text_surface.get_rect()
if mode == "midtop":
text_rect.midtop = (x, y)
elif mode == "topleft":
text_rect.topleft = (x, y)
elif mode == "center":
text_rect.center = (x, y)
return text_surface, text_rect
class InputBox:
COLOR_INACTIVE = (0, 0, 0)
COLOR_ACTIVE = (255, 0, 0)
def __init__(self, x, y, w, h, text='', font_name = pygame.font.match_font('arial')):
self.FONT = pygame.font.Font(font_name, 15)
self.rect = pygame.Rect(x, y, w, h)
self.color = self.COLOR_INACTIVE
self.w = w
self.h = h
self.text = text
self.txt_surface = self.FONT.render(text, True, self.color)
self.active = False
self.visible =True
def handle_event(self, event):
if not self.visible:
return
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = self.COLOR_ACTIVE if self.active else self.COLOR_INACTIVE
if event.type == pygame.KEYDOWN:
if self.active:
if event.key == pygame.K_RETURN:
print(self.text)
self.text = ''
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = self.FONT.render(self.text, True, self.color)
def update(self):
self.txt_surface = self.FONT.render(self.text, True, self.color)
pass
def draw(self, screen):
if not self.visible:
return
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+self.w // 2, self.rect.y+self.h // 3))
# Blit the rect.
pygame.draw.rect(screen, self.color, self.rect, 2)
class RoundIter():
def __init__(self, cnt = 1):
self.filePath = os.path.join(os.path.dirname(__file__), 'video')
self.videoPath = []
random.seed()
for i in range(cnt):
numRound = random.randint(1, 3)
video, timeCode = os.path.join(self.filePath, "round" + str(numRound) + '.mov'), os.path.join(self.filePath, "round" + str(numRound) + '.txt')
while (video, timeCode) in self.videoPath:
numRound = random.randint(1, cnt)
video, timeCode = os.path.join(self.filePath, "round" + str(numRound) + '.mov'), os.path.join(self.filePath, "round" + str(numRound) + '.txt')
self.videoPath.append((video, timeCode))
self._start = 0
self._end = cnt -1
def __iter__(self):
return self
def __next__(self):
if self._start > self._end:
raise StopIteration()
tmp = self._start
self._start = tmp + 1
time = open(self.videoPath[tmp][1], 'r').read().split('|')
timecode = []
for t in time:
t = t.split(':')
mn = 1000
ans = 0
for el in t[::-1]:
ans += int(el) * mn
mn *= 60
timecode.append(ans)
return (self.videoPath[tmp][0], timecode)
class CleverSurf(pygame.sprite.Sprite):
def __init__(self, surf = None, rect = None, x = None, y = None):
pygame.sprite.Sprite.__init__(self)
self.surf = surf
self.rect = rect
self.visible = True
self.pos = (x, y)
@property
def surf(self):
return self.s;
@surf.setter
def surf(self, x):
self.s = x
@property
def rect(self):
return self.r;
@rect.setter
def rect(self, x):
self.r = x
@property
def pos(self):
return self.p;
@pos.setter
def pos(self, pos):
self.p = pos
if (pos != (None, None)):
self.rect.center = pos
@property
def size(self):
return self.si;
@size.setter
def size(self, size):
self.si = size
self.surf = pygame.transform.scale(self.surf, size)
self.rect = self.surf.get_rect()
def update(self):
pass
def draw(self, screen):
if not self.visible:
return
screen.blit(self.surf, self.rect)