-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrummolo.py
218 lines (166 loc) · 5.7 KB
/
scrummolo.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"A virtual talking stick."
from datetime import datetime
from random import shuffle
import pyglet
from pyglet.window import key
import settings
# index all resources. Resources folder must be on same level as this file.
pyglet.resource.path = settings.RESOURCES
pyglet.resource.reindex()
# create window object
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN = pyglet.window.Window(SCREEN_WIDTH, SCREEN_HEIGHT)
# create Team
TEAM = settings.TEAM
shuffle(TEAM)
def resize(width, height, max_width, max_height):
"Calculates new width and height but leave w/h ratio invariant."
delta_width = width - max_width
delta_height = height - max_height
if delta_height <= 0 and delta_width <= 0:
return width, height
if delta_height >= delta_width:
return width * max_height / height, max_height
return max_width, height * max_width / width
# image dimensions
WIDTH = SCREEN_WIDTH // 2
HEIGHT = SCREEN_HEIGHT // 2
HEIGHT_BORDER = 10
def resize_image(image):
"Resize but leave the ratio invariant."
width, height = resize(image.width, image.height, WIDTH, HEIGHT)
image.width = width
image.height = height
# create player objects. Player image must be in resource folder.
PLAYER_IMAGES = []
for player in TEAM:
player = player.replace(".", "")
player = player.replace(" ", "_")
player_image = pyglet.resource.image(player.lower() + ".png")
resize_image(player_image)
PLAYER_IMAGES.append(player_image)
class GameOverAnimation:
"""Animation for GIF files."""
def __init__(self, animation_resource_name, pos_x, pos_y):
"""Constructor."""
animation = pyglet.resource.animation(animation_resource_name)
self.sprite = pyglet.sprite.Sprite(animation, pos_x, pos_y)
def draw(self):
"""Draw game over animation."""
self.sprite.draw()
class GameOverImage:
"""Image for PNG files."""
def __init__(self, image_resource_name, pos_x, pos_y):
"""Constructor."""
self.image = pyglet.resource.image(image_resource_name)
resize_image(self.image)
self.pos_x = pos_x
self.pos_y = pos_y
def draw(self):
"""Draw game over animation."""
self.image.blit(self.pos_x, self.pos_y)
def create_game_over_image(image_path):
if image_path.split(".")[1] == "gif":
return GameOverAnimation(settings.GAME_OVER_IMAGE, 0, HEIGHT * 2 // 3)
else:
return GameOverImage(settings.GAME_OVER_IMAGE, WIDTH, HEIGHT - HEIGHT_BORDER)
GAME_OVER_IMAGE = create_game_over_image(settings.GAME_OVER_IMAGE)
GAME_OVER_SOUND = pyglet.resource.media(settings.GAME_OVER_SOUND, streaming=False)
# create text labels for questions
QUESTIONS = settings.QUESTIONS
QUESTIONS = [
pyglet.text.Label(text="{0}".format(q), font_size=36, x=100, y=100)
for q in QUESTIONS
]
PLAYER_MESSAGES = [
pyglet.text.Label(
text="{0}, is your turn!".format(name), font_size=18, x=100, y=500
)
for name in TEAM
]
GAME_OVER_MESSAGE = pyglet.text.Label(
text=settings.GAME_OVER_TEXT, font_size=36, x=100, y=100
)
class GameLogic(object):
"Game Logic"
def __init__(self, number_of_questions, number_of_players):
"Constructor"
self.number_of_questions = number_of_questions
self.number_of_players = number_of_players
self.player = 0
self.question = 0
self.game_over = False
self.bye = False
def step(self):
"Move forward in the game"
if self.question < self.number_of_questions - 1:
self.question += 1
else:
if self.player == self.number_of_players - 1:
self.game_over = True
else:
self.question = 0
self.player += 1
def get_player(self):
"Returns the current player."
return self.player
def get_question(self):
"Returns the current question."
return self.question
def over(self):
"Returns True if the game is over."
return self.game_over
def goodbye(self):
"Set the end of the game."
self.bye = True
def said_goodbye(self):
"Check if is time to exit."
return self.bye
GAME = GameLogic(len(QUESTIONS), len(TEAM))
class Timer:
"""Timer class"""
def __init__(self, minutes, seconds, label):
"""Constructor."""
self.duration = minutes * 60 + seconds
self.label = label
self.tick = 0.0
def update(self, delta_time):
"""Update timer."""
self.tick = self.tick + delta_time
if self.tick > 1.0 and self.duration > 0:
self.duration -= 1
self.mins, self.secs = divmod(self.duration, 60)
self.label.text = "{:02d}:{:02d}".format(self.mins, self.secs)
self.tick = 0
TIME_LABEL = pyglet.text.Label(
text="{:02d}:00".format(settings.DURATION), font_size=36, x=100, y=300
)
TIMER = Timer(settings.DURATION, 0, TIME_LABEL)
# FPS = pyglet.clock.ClockDisplay()
@SCREEN.event
def on_draw():
"""Window draw event handler."""
SCREEN.clear()
# FPS.draw()
if GAME.over():
GAME_OVER_IMAGE.draw()
GAME_OVER_MESSAGE.draw()
if not GAME.said_goodbye():
GAME_OVER_SOUND.play()
GAME.goodbye()
else:
PLAYER_MESSAGES[GAME.get_player()].draw()
QUESTIONS[GAME.get_question()].draw()
PLAYER_IMAGES[GAME.get_player()].blit(WIDTH, HEIGHT - HEIGHT_BORDER)
TIMER.label.draw()
@SCREEN.event
def on_key_press(symbol, modifiers):
"""Key pressed event handler."""
if symbol == key.SPACE:
if GAME.said_goodbye():
pyglet.app.exit()
GAME.step()
if __name__ == "__main__":
pyglet.clock.schedule_interval(TIMER.update, 0.1)
pyglet.app.run()