-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.py
388 lines (334 loc) · 16 KB
/
Character.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import pygame
import database
from datetime import datetime, timedelta
from random import randint
from time import time
from Clod import Clod
import twitch_api
time_counter = timedelta(minutes=1)
WALK_LEFT = [pygame.image.load('images/basic/1.png'), pygame.image.load('images/basic/2.png'),
pygame.image.load('images/basic/3.png')]
SIT = pygame.image.load('images/basic_sitting_lowres.png')
CHAIR_POOF = [pygame.image.load('images/chair_poof/chair_poof_01_lowres.png'),
pygame.image.load('images/chair_poof/chair_poof_02_lowres.png'),
pygame.image.load('images/chair_poof/chair_poof_03_lowres.png'),
pygame.image.load('images/chair_poof/chair_poof_04_lowres.png'),
pygame.image.load('images/chair_poof/chair_poof_05_lowres.png'),
pygame.image.load('images/chair_poof/chair_poof_06_lowres.png'),
pygame.image.load('images/chair_poof/chair_poof_07_lowres.png')]
CHAIR_POOF_REVERSED = list(reversed(CHAIR_POOF))
CHAIR = pygame.image.load('images/chair_poof/chair_poof_07_lowres.png')
WAVE = [pygame.image.load('images/wave_anim/wave_01_lowres.png'),
pygame.image.load('images/wave_anim/wave_02_lowres.png')] * 10
ClAP = [pygame.image.load('images/clap_anim/clap_alt_01_lowres.png'),
pygame.image.load('images/clap_anim/clap_alt_02_lowres.png')] * 10
THROW = (([pygame.image.load('images/throw_anim/throw_lowres.png')] * 6) +
([pygame.image.load('images/throw_anim/count_5_lowres.png')] * 3) +
([pygame.image.load('images/throw_anim/count_4_lowres.png')] * 3) +
([pygame.image.load('images/throw_anim/count_3_lowres.png')] * 3) +
([pygame.image.load('images/throw_anim/count_2_lowres.png')] * 3) +
([pygame.image.load('images/throw_anim/count_1_lowres.png')] * 3))
CATCH = [pygame.image.load('images/catch/catch.png'), ] * 25
CAUGHT = [pygame.image.load('images/caught/pt_1_lowres.png'),
pygame.image.load('images/caught/pt_2_lowres.png'),
pygame.image.load('images/caught/pt_3_lowres.png'),
pygame.image.load('images/caught/pt_4_lowres.png')] * 4
OUCH = [pygame.image.load('images/ouch/fail_overlay_1.png'), pygame.image.load('images/ouch/fail_overlay_2.png')] * 8
class Character:
lurking_list = []
positions = {1920: True, 2020: True, 2120: True, 2220: True, 2320: True, 2420: True, 2520: True, 2620: True,
2720: True, 2820: True, 2920: True, 3020: True, 3120: True, 3220: True, 3320: True, 3420: True,
3520: True, 3620: True, 3720: True}
def __init__(self, name, screen, points=0):
self.name = name
self.screen = screen
self.points = points
self.time = datetime.now()
self.image = SIT
self.image_rect = self.image.get_rect()
self.screen_rect = screen.get_rect() # rectangle (x_axis, y_axis, width, height)
for pos in Character.positions: # finding free seat
if Character.positions[pos]:
self.pos_place = pos
Character.positions[pos] = False
break
self.image_rect.centerx = self.pos_place # middle of the image on the x axis
self.image_rect.centery = 1020 # middle of the image on the y axis
self.font = pygame.font.SysFont('font', 20)
self.text = self.font.render(self.name, True, 'white')
self.text_width = self.text.get_rect().width # text width
self.position = 310 # counter while the character is going left
self.seat_point = self.image_rect.centerx - self.position * 6 # the final point for a character
self.animation_speed = 3 # speed of changing frames (should be a multiple of len(frames), but NOT SHURE)
self.move_animation_count = 0 # counter for MOVE frames
self.leave_animation_count = 0 # counter for LEAVING frames
self.chair_animation_count = 0 # counter for CHAIR frames
self.wave_animation_count = 0 # counter for WAVE frames
self.clap_animation_count = 0 # counter for CLAP frames
self.catch_animation_count = 0 # counter for CATCH frames
self.throw_animation_count = 0 # counter for THROW frames
self.caught_animation_count = 0 # counter for CAUGHT frames
self.ouch_animation_count = 0 # counter for OUCH frames
self.clod_amount = 0 # how many clods the character has
self.target = None # character's target
self.calling_for_play = False # character's game status
self.bet = None # character's bet for game
self.pick = None # paper, scissors or rock
self.add_lurker(self)
def points_gain(self):
if datetime.now() >= self.time + time_counter:
self.points += 1
self.time = datetime.now()
@property
def all_animations(self):
return {self.leave_animation_count, self.wave_animation_count, self.clap_animation_count,
self.catch_animation_count, self.throw_animation_count}
def move(self):
"""
The character is going to the seat.
"""
if self.move_animation_count >= self.animation_speed * len(WALK_LEFT):
self.move_animation_count = 0
if self.position > 0:
self.screen.blit(WALK_LEFT[self.move_animation_count // self.animation_speed], self.image_rect)
self.screen.blit(self.text, (self.image_rect.centerx - self.text_width / 2, self.image_rect.centery - 70))
self.move_animation_count += 1
self.image_rect.centerx -= 6
self.position -= 1
else:
if not any(self.all_animations): # Checks if other animations aren't working
self.screen.blit(SIT, self.image_rect)
self.screen.blit(self.text,
(self.image_rect.centerx - self.text_width / 2, self.image_rect.centery - 70))
def wave(self):
"""
The character is waving if other animations isn't called.
"""
if self.wave_animation_count > 0 and self.position <= 0:
self.screen.blit(WAVE[self.wave_animation_count // self.animation_speed], self.image_rect)
self.screen.blit(self.text, (self.image_rect.centerx - self.text_width / 2, self.image_rect.centery - 70))
self.wave_animation_count -= 1
def wave_update(self):
"""
Updates the counter for waving.
"""
if not any(self.all_animations): # checks if other animations aren't working
if self.position <= 0:
self.wave_animation_count = (len(WAVE) * self.animation_speed) - 1
def clap(self):
"""
The character is clapping if other animations isn't called.
"""
if self.clap_animation_count > 0 and self.position <= 0:
self.screen.blit(ClAP[self.clap_animation_count // self.animation_speed], self.image_rect)
self.screen.blit(self.text, (self.image_rect.centerx - self.text_width / 2, self.image_rect.centery - 70))
self.clap_animation_count -= 1
def clap_update(self):
"""
Updates the counter for clapping.
"""
if not any(self.all_animations): # checks if other animations aren't working
if self.position <= 0:
self.clap_animation_count = (len(ClAP) * self.animation_speed) - 1
def catch(self):
"""
The character is trying to catch a clod if other animations isn't called.
"""
if self.catch_animation_count > 0 and self.position <= 0:
self.screen.blit(CATCH[self.catch_animation_count // self.animation_speed], self.image_rect)
self.screen.blit(self.text, (self.image_rect.centerx - self.text_width / 2, self.image_rect.centery - 70))
self.catch_animation_count -= 1
def catch_update(self):
"""
Updates the counter for catching.
"""
if not any(self.all_animations): # checks if other animations aren't working
if self.position <= 0:
self.catch_animation_count = (len(CATCH) * self.animation_speed) - 1
def get_a_clod(self):
"""
The character gets 1 clod.
"""
self.clod_amount += 1
self.points -= 10
def throw(self):
"""
Throws a clod to another character.
"""
if self.throw_animation_count > 0 and self.position <= 0:
self.screen.blit(THROW[self.throw_animation_count // self.animation_speed], self.image_rect)
self.screen.blit(self.text, (self.image_rect.centerx - self.text_width / 2, self.image_rect.centery - 70))
self.throw_animation_count -= 1
if self.throw_animation_count == 14:
self.clod_amount -= 1
Clod.clod_list.append(Clod(self.screen, self.seat_point, self.target, self.name))
def throw_update(self, target):
"""
Updates the counter for throwing.
:param target: coordinates of another character
"""
self.target = target
if not any(self.all_animations): # checks if other animations aren't working
if self.position <= 0:
self.throw_animation_count = (len(THROW) * self.animation_speed) - 1
def caught(self):
"""
Animation if the character caught a clod.
"""
if self.caught_animation_count > 0 and self.position <= 0 and not self.ouch_animation_count:
self.screen.blit(CAUGHT[self.caught_animation_count // self.animation_speed], self.image_rect)
self.caught_animation_count -= 1
def ouch(self):
"""
Animation if the character didn't catch a clod.
"""
if self.ouch_animation_count > 0 and self.position <= 0 and not self.caught_animation_count:
self.screen.blit(OUCH[self.ouch_animation_count // self.animation_speed], self.image_rect)
self.ouch_animation_count -= 1
def clod_collision(self):
"""
Gets a clod if animation of catching is called.
Clod hits the character if animation wasn't called.
"""
for clod in Clod.clod_list:
if ((self.seat_point - 64) <= clod.x_axis <= (self.seat_point + 64) and clod.y_axis >= 1020
and clod.who_threw != self.name):
clod.stop()
if self.catch_animation_count:
print(self.name + ' got the clod')
self.caught_animation_count = (len(CAUGHT) * self.animation_speed) - 1
self.clod_amount += 1
self.points += 5
else:
print(self.name + ' did not get the clod')
self.ouch_animation_count = (len(OUCH) * self.animation_speed) - 1
def chair_puff(self):
"""
The chair animation.
"""
if self.image_rect.centerx < 0 and self.chair_animation_count < (
len(CHAIR_POOF) * self.animation_speed): # The chair fades out
self.screen.blit(CHAIR_POOF_REVERSED[self.chair_animation_count // self.animation_speed],
(self.seat_point - self.image_rect.width / 2,
self.image_rect.centery - self.image_rect.height / 2))
self.chair_animation_count += 1
elif self.position <= 50 and self.chair_animation_count >= (
len(CHAIR_POOF) * self.animation_speed) or self.leave_animation_count > 0: # The chair stands
self.screen.blit(CHAIR, (
self.seat_point - self.image_rect.width / 2, self.image_rect.centery - self.image_rect.height / 2))
elif 0 < self.position <= 49: # The chair fades in
self.screen.blit(CHAIR_POOF[self.chair_animation_count // self.animation_speed],
(self.seat_point - self.image_rect.width / 2,
self.image_rect.centery - self.image_rect.height / 2))
self.chair_animation_count += 1
def get_vip_status(self):
"""
Purchase VIP status
"""
username_id = twitch_api.get_user_id(self.name.lower())
if username_id:
status = twitch_api.grant_vip_status(username_id)
if status:
if status != 422:
self.points -= 300
database.update_vip_time(self.name, int(time()))
return True
else:
return 422
def lose_vip_status(self):
"""
Purchase VIP status
"""
username_id = twitch_api.get_user_id(self.name.lower())
if username_id:
status = twitch_api.remove_vip_status(username_id)
if status:
if status != 422:
return True
else:
return 422
def give_timeout(self, username: str, reason):
"""
Sets timeout for user
"""
username_id = twitch_api.get_user_id(username.lower())
if not reason:
reason = 'Just because'
if username_id:
status = twitch_api.timeout_user(username_id, reason)
if status >= 400:
return False
else:
self.points -= 100
return status
def ready_to_play(self, bet: str):
"""
User is ready to play
"""
self.calling_for_play = True
self.bet = int(bet)
def play_round(self, target, bet: int):
"""
User throws paper, scissors or rock
"""
options = ['paper', 'scissors', 'rock']
self.pick = options[randint(0, 2)]
target.pick = options[randint(0, 2)]
target.calling_for_play = False
if self.pick == options[0] and target.pick == options[1]:
self.points -= bet
target.points += bet
return False
elif self.pick == options[0] and target.pick == options[2]:
self.points += bet
target.points -= bet
return True
elif self.pick == options[1] and target.pick == options[0]:
self.points += bet
target.points -= bet
return True
elif self.pick == options[1] and target.pick == options[2]:
self.points -= bet
target.points += bet
return False
elif self.pick == options[2] and target.pick == options[0]:
self.points -= bet
target.points += bet
return False
elif self.pick == options[2] and target.pick == options[1]:
self.points += bet
target.points -= bet
return True
else:
pass
def leave(self):
"""
The character leaves the seat.
"""
if self.leave_animation_count > 0 and self.position <= 0:
if self.position <= 0:
self.screen.blit(WALK_LEFT[self.leave_animation_count // self.animation_speed], self.image_rect)
self.screen.blit(self.text,
(self.image_rect.centerx - self.text_width / 2, self.image_rect.centery - 70))
self.leave_animation_count -= 1
self.image_rect.centerx -= 6
if self.leave_animation_count <= 0:
self.leave_animation_count = (len(WALK_LEFT) * self.animation_speed) - 1
if self.image_rect.centerx <= -1 * (len(CHAIR_POOF_REVERSED) * self.animation_speed * 6):
database.update_points(self.name, self.points)
Character.positions[self.pos_place] = True
Character.lurking_list.remove(self)
def leave_update(self):
"""
Updates the counter for leaving.
"""
if not any(self.all_animations): # checks if other animations aren't working
if self.position <= 0:
self.leave_animation_count = (len(WALK_LEFT) * self.animation_speed) - 1
self.chair_animation_count = 0
@classmethod
def add_lurker(cls, user_name):
cls.lurking_list.append(user_name)
@classmethod
def show_lurkers(cls):
return ', '.join([something.name for something in cls.lurking_list])