-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1770 lines (1498 loc) · 67 KB
/
main.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame
from random import randint
import random
import sys
pygame.init()
size = width, height = 1280, 720
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
all_sprites = pygame.sprite.Group()
player_group = pygame.sprite.Group()
building_group = pygame.sprite.Group()
settings_buttons_group = pygame.sprite.Group()
button_group = pygame.sprite.Group()
terrain_group = pygame.sprite.Group()
background_group = pygame.sprite.Group()
bank_buttons = pygame.sprite.Group()
pharm_buttons = pygame.sprite.Group()
pharm_group = pygame.sprite.Group()
background_pharm = pygame.sprite.Group()
pharm_products = pygame.sprite.Group()
shop_buttons = pygame.sprite.Group()
shop_group = pygame.sprite.Group()
background_shop = pygame.sprite.Group()
shop_products = pygame.sprite.Group()
house_buttons = pygame.sprite.Group()
house_group = pygame.sprite.Group()
background_house = pygame.sprite.Group()
house_products = pygame.sprite.Group()
product_buttons = pygame.sprite.Group()
npc_group = pygame.sprite.Group()
building_collide_step = 0
near_building_message = None
near_building = None
gravity = 0.5
menu_is_on = False
music_on = True
effects_on = True
level = None
speeches = {'intro': pygame.mixer.Sound('data/speech/intro.wav'),
'1': pygame.mixer.Sound('data/speech/1.wav'),
'2': pygame.mixer.Sound('data/speech/2.wav'),
'3': pygame.mixer.Sound('data/speech/3.wav'),
'autro': pygame.mixer.Sound('data/speech/autro.wav'),
'news': pygame.mixer.Sound('data/speech/news.wav')}
music = {'main': pygame.mixer.Sound('data/music/main_music.ogg')}
music['main'].play(-1)
sounds = {'apple': pygame.mixer.Sound('data/sounds/apple_crunch.wav'),
'atm_button': pygame.mixer.Sound('data/sounds/atm_button.wav'),
'bottle_open': pygame.mixer.Sound('data/sounds/bottle_open.wav'),
'cashbox': pygame.mixer.Sound('data/sounds/cashbox.wav'),
'close_door': pygame.mixer.Sound('data/sounds/close_door.wav'),
'drink_gulp': pygame.mixer.Sound('data/sounds/drink_gulp.wav'),
'open_door': pygame.mixer.Sound('data/sounds/open_door.wav'),
'step': pygame.mixer.Sound('data/sounds/step.wav')}
player_params = dict()
products = dict()
def stop_speeches():
for sound in speeches.values():
sound.stop()
def load_image(path, colorkey=None, size=None) -> pygame.Surface:
image = pygame.image.load(path).convert_alpha()
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey)
if size:
image = pygame.transform.scale(image, size)
return image
def distance(first, second):
res = ((second[0] - first[0]) ** 2 + (second[1] - first[1]) ** 2) ** 0.5
return res
def check_collisions(player):
check_near_building(player)
for i in all_sprites:
if id(player) == id(i):
continue
if i.is_obstacle():
if pygame.sprite.collide_mask(player, i):
return True
return False
def check_near_building(player):
global near_building, near_building_message
near_building, near_building_message = None, None
for building in building_group:
if pygame.sprite.collide_mask(player, building):
near_building = building
near_building_message = f'Нажмите [E], чтобы войти в {building.name}'
def render_text(line, size=50, color=(255, 255, 255)):
font = pygame.font.Font(None, size)
text = font.render(line, 1, color)
return text
class Player(pygame.sprite.Sprite):
speed = 8
jump_power = 10
infection_rate = 1100
def __init__(self, x, y, *groups):
super().__init__(groups)
self.frames = {'left': [], 'right': []}
for i in range(6):
self.frames['left'].append(
pygame.transform.flip(load_image(f'data/characters/citizen_right_{i + 1}.png', size=(80, 110)), 1, 0))
for i in range(6):
self.frames['right'].append(
load_image(f'data/characters/citizen_right_{i + 1}.png', size=(80, 110)))
self.image = self.frames['right'][0]
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
self.side = 'right'
self.image_num = 0
self.prev_coords = x, y
self.is_moving = False
self.grav = 0
self.is_jumping = False
self.clock = pygame.time.Clock()
self.health = 100
self.hazard_risk = 0
self.danger_level = 1
self.hazard_timer = 0
self.speed = Player.speed
self.infection_rate = Player.infection_rate
if level == 1:
self.card_money = 500
self.cash = 0
elif level == 2:
self.card_money = 1500
self.cash = 0
elif level == 3:
self.card_money = 0
self.cash = 3000
self.infection_rate = 1100
self.objects = []
pin = str(randint(1000, 9999))
products['card'].description = (r'Пин код:\n' + str(pin)).split(r'\n')
products['card'].pin = pin
self.objects.append(products['card'])
def get_objects(self):
return self.objects
def add_objects(self, *objects):
for i in objects:
self.objects.append(i)
def get_cash(self):
return self.cash
def set_cash(self, value):
self.cash = value
def give_money(self, amount):
if amount <= self.cash:
self.cash -= amount
return True
return False
def set_card_money(self, value):
self.card_money = value
def get_card_money(self):
return self.card_money
def spend_money(self, amount):
if amount <= self.card_money:
self.card_money -= amount
return True
return False
def set_position(self, pos):
self.rect.x, self.rect.y = pos
def set_moving(self, value):
self.is_moving = value
def get_coords(self):
return self.rect.x, self.rect.y
def get_card_balance(self):
return self.card_money
def is_obstacle(self):
return False
def move_left(self):
self.side = 'left'
self.image = self.frames[self.side][0]
self.prev_coords = self.get_coords()
self.rect.x -= self.speed
if check_collisions(self):
self.rect.y -= 5
if check_collisions(self):
self.rect.y -= 10
if check_collisions(self):
self.rect.y -= 11
if check_collisions(self):
self.rect.x = self.prev_coords[0]
self.rect.y = self.prev_coords[1]
def move_right(self):
self.side = 'right'
self.image = self.frames[self.side][0]
self.prev_coords = self.get_coords()
self.rect.x += self.speed
if check_collisions(self):
self.rect.y -= 5
if check_collisions(self):
self.rect.y -= 10
if check_collisions(self):
self.rect.y -= 11
if check_collisions(self):
self.rect.x = self.prev_coords[0]
self.rect.y = self.prev_coords[1]
def move_down(self, value):
self.prev_coords = self.get_coords()
self.rect.y -= value
if check_collisions(self):
if self.grav < -13:
self.health -= abs(self.grav) - 5
self.rect.x = self.prev_coords[0]
self.rect.y = self.prev_coords[1]
self.grav = -5
self.is_jumping = False
def jump(self):
if not self.is_jumping:
self.grav = Player.jump_power
self.is_jumping = True
def render_info(self, color=(255, 255, 255), background=(0, 0, 0)):
canvas = pygame.Surface((width // 2 + 175, 20))
canvas.fill(background)
font = pygame.font.Font(None, 30)
canvas.blit(
font.render(
f'Здоровье: {int(self.health)}% Риск заражения: {self.hazard_risk}% Наличные: {self.cash} Р На карте: {self.card_money} Р',
1, color), (0, 0))
return canvas
def update_params(self):
self.speed = Player.speed * self.health / 100
if self.speed <= self.speed // 2:
self.speed //= 2
prev_infect = self.infection_rate
self.danger_level = 1 - (100 - self.health) / 100
self.hazard_timer += self.clock.tick()
for i in npc_group:
if distance(self.get_coords(), i.get_coords()) < 100:
self.infection_rate -= 450
if self.infection_rate < 350:
self.infection_rate = 350
if self.hazard_timer > self.infection_rate * self.danger_level:
self.hazard_risk += 1
self.hazard_timer = 0
self.infection_rate = prev_infect
if self.health <= 0:
self.health = 0
if self.hazard_risk >= 100:
self.hazard_risk = 100
if self.health == 0 or self.hazard_risk == 100 or self.grav < -50:
screen.fill((0, 0, 0))
# background_group.draw(screen)
screen.blit(self.render_info(), (0, 0))
text = render_text('Вы мертвы!!!')
screen.blit(text, (width // 2 - text.get_width() // 2, height // 2))
pygame.display.flip()
for i in range(5):
self.clock.tick(1)
global level
level = None
def update(self, *args):
if self.is_moving:
self.image_num += 1
if self.image_num >= len(self.frames['right']) * 6:
self.image_num = 0
# if effects_on:
# sounds['step'].play()
self.image = self.frames[self.side][self.image_num // 6]
else:
self.image = self.frames[self.side][4]
self.move_down(self.grav)
self.grav -= gravity
self.update_params()
class Character(pygame.sprite.Sprite):
speed = 3
jump_power = 10
def __init__(self, *groups):
super().__init__(groups)
self.first_update = True
role = 'citizen'
self.frames = {'left': [], 'right': []}
for i in range(6):
self.frames['left'].append(
pygame.transform.flip(load_image(f'data/characters/citizen_right_{i + 1}.png',
size=(80 + randint(-10, 10), 110 + randint(-10, 10))), 1, 0))
for i in range(6):
self.frames['right'].append(
load_image(f'data/characters/citizen_right_{i + 1}.png',
size=(80 + randint(-10, 10), 110 + randint(-10, 10))))
self.image = self.frames['right'][0]
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
if random.random() > 0.5:
self.rect.x, self.rect.y = randint(100, 4000) + terrain.rect.x, randint(200, 300) + terrain.rect.y
else:
self.rect.x, self.rect.y = randint(4000, 7500) + terrain.rect.x, randint(100, 200) + terrain.rect.y
self.image_num = 0
self.prev_coords = 0, 0
self.is_moving = False
self.grav = 0
self.is_jumping = False
self.clock = pygame.time.Clock()
self.side = 'left'
self.health = 100
self.hazard_risk = 0
self.danger_level = 1
self.hazard_timer = 0
self.infected = 1
self.infection_rate = 1250
self.direction = True
def set_position(self, pos):
self.rect.x, self.rect.y = pos
def set_moving(self, value):
self.is_moving = value
def get_coords(self):
return self.rect.x, self.rect.y
def is_obstacle(self):
return False
def move_left(self):
self.side = 'left'
self.image = self.frames[self.side][0]
self.prev_coords = self.get_coords()
self.rect.x -= Character.speed
if check_collisions(self):
self.rect.y -= 5
if check_collisions(self):
self.rect.y -= 10
if check_collisions(self):
self.rect.y -= 15
if check_collisions(self):
self.rect.x = self.prev_coords[0]
self.rect.y = self.prev_coords[1]
def move_right(self):
self.side = 'right'
self.image = self.frames[self.side][0]
self.rect.x += Character.speed
if check_collisions(self):
self.rect.y -= 5
if check_collisions(self):
self.rect.y -= 10
if check_collisions(self):
self.rect.y -= 15
if check_collisions(self):
self.jump()
self.rect.x = self.prev_coords[0]
self.rect.y = self.prev_coords[1]
def move_down(self, value):
self.prev_coords = self.get_coords()
self.rect.y -= value
if check_collisions(self):
if self.grav < -20:
self.health -= abs(self.grav) - 10
self.rect.x = self.prev_coords[0]
self.rect.y = self.prev_coords[1]
self.grav = -5
self.is_jumping = False
def jump(self):
if not self.is_jumping:
self.grav = Character.jump_power
self.is_jumping = True
# reducing health, increasing hazard_risk (risk of infection)
def update_params(self):
self.danger_level = 1 - (100 - self.health) / 100
self.hazard_timer += self.clock.tick()
if self.hazard_timer > self.infection_rate * self.danger_level:
self.hazard_risk += 1
self.hazard_timer = 0
if self.health <= 0:
self.health = 0
if self.hazard_risk >= 100:
self.hazard_risk = 100
# cheking dead state (added gravity here, in this way we check if player is falling too fast)
if self.health == 0 or self.grav < -50:
Character(npc_group, all_sprites)
self.kill()
elif self.hazard_risk == 100:
self.infected = 3
def update(self, *args):
if self.first_update:
if check_collisions(self):
Character(npc_group, all_sprites)
self.kill()
return
else:
self.first_update = False
if self.direction:
self.move_left()
else:
self.move_right()
if random.random() > 0.5:
self.image_num += 1
if random.random() > 0.65:
self.jump()
if self.image_num >= len(self.frames['right']) * 6:
self.image_num = 0
# if effects_on:
# sounds['step'].play()
self.image = self.frames[self.side][self.image_num // 6]
self.move_down(self.grav)
self.grav -= gravity
self.update_params()
class Terrain(pygame.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(groups)
self.image = load_image('data/textures/city_terrain.png')
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
def is_obstacle(self):
return True
class Bank(pygame.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(groups)
self.image = load_image('data/buildings/bank.png', size=(300, 250))
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
self.mask = pygame.mask.from_surface(self.image)
self.name = 'Банк'
def is_obstacle(self):
return False
def enter(self):
button_group.empty()
if level == 1 or level == 2:
return
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, button_group)
running = True
mode = ''
right_pin = products['card'].pin
current_pin = ''
deposit_summ = ''
main_display = pygame.Surface((width // 2, height // 3 + 150))
main_display.fill((0, 0, 0))
card_size = (100, 150)
card = Button(width - card_size[0], height - card_size[1], *card_size,
load_image('data/objects/bank_card.png', size=card_size), None, 'card', bank_buttons)
card_moving = False
hole_rect = pygame.Rect(888, 107, 300, 20)
Button(width - images['exit_sign'].get_width(), height - images['exit_sign'].get_height(),
100, 50, images['exit_sign'], lambda: False, None, bank_buttons)
Button(50, 100, 50, 50, images['right_arrow'], None, 'first', bank_buttons)
Button(50, 200, 50, 50, images['right_arrow'], None, 'second', bank_buttons)
Button(50, 300, 50, 50, images['right_arrow'], None, 'third', bank_buttons)
Button(100 + main_display.get_width(), 100, 50, 50, images['left_arrow'], None, 'fourth', bank_buttons)
Button(100 + main_display.get_width(), 200, 50, 50, images['left_arrow'], None, 'fifth', bank_buttons)
Button(100 + main_display.get_width(), 300, 50, 50, images['left_arrow'], None, 'sixth', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('1', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4, 470, 50, 50, image, None, '1', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('2', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75, 470, 50, 50, image, None, '2', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('3', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75 * 2, 470, 50, 50, image, None, '3', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('4', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4, 545, 50, 50, image, None, '4', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('5', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75, 545, 50, 50, image, None, '5', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('6', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75 * 2, 545, 50, 50, image, None, '6', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('7', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4, 620, 50, 50, image, None, '7', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('8', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75, 620, 50, 50, image, None, '8', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('9', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75 * 2, 620, 50, 50, image, None, '9', bank_buttons)
image = load_image('data/objects/digit_button.png')
image.blit(render_text('0', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75, 670, 50, 50, image, None, '0', bank_buttons)
image = load_image('data/objects/long_button.png')
image.blit(render_text('Enter', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75 * 3, 620, 208, 47, image, None, 'enter', bank_buttons)
image = load_image('data/objects/long_button.png')
image.blit(render_text('Clear', color=(0, 0, 0)), (10, 5))
Button(100 + main_display.get_width() // 4 + 75 * 3, 545, 208, 47, image, None, 'clear', bank_buttons)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
if card_moving:
card.rect.x, card.rect.y = event.pos
if card.rect.colliderect(hole_rect):
mode = 'pin'
bank_buttons.remove(card)
if event.type == pygame.MOUSEBUTTONDOWN:
for btn in bank_buttons:
if btn.rect.collidepoint(event.pos):
if effects_on:
sounds['atm_button'].play()
if not btn.id:
running = btn.run()
elif btn.id == 'card':
card_moving = True
card.rect.x, card.rect.y = event.pos
elif btn.id == 'clear':
current_pin = ''
deposit_summ = ''
elif btn.id.isdigit() and mode == 'pin':
current_pin += btn.id
if len(current_pin) == 4 and current_pin == right_pin:
mode = 'main'
current_pin = ''
elif mode == 'main':
if btn.id == 'first':
mode = 'balance'
elif btn.id == 'second':
mode = 'deposit'
elif mode == 'balance':
if btn.id == 'sixth':
mode = 'main'
elif mode == 'deposit':
if btn.id == 'enter':
if deposit_summ.isdigit() and player.give_money(int(deposit_summ)):
player.card_money += int(deposit_summ)
mode = 'success'
else:
mode = 'error'
deposit_summ = ''
elif btn.id.isdigit():
deposit_summ += btn.id
elif mode == 'error' or mode == 'success':
mode = 'main'
# button_group.empty()
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, button_group)
data = pygame.key.get_pressed()
player.set_moving(False)
if data[27]:
menu(pause=True)
button_group.empty()
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, button_group)
main_display.fill((0, 0, 0))
if mode == 'pin':
main_display.blit(render_text('Введите пин код:', color=(232, 208, 79)),
(main_display.get_width() // 4, main_display.get_height() // 2 - 50))
main_display.blit(render_text(('_' * (4 - len(current_pin))).rjust(4, '*'), color=(232, 208, 79)),
(main_display.get_width() // 2, main_display.get_height() // 2))
elif mode == 'main':
main_display.blit(render_text('Показать баланс', color=(232, 208, 79)), (60, 30))
main_display.blit(render_text('Внести наличные', color=(232, 208, 79)), (60, 130))
elif mode == 'balance':
main_display.blit(
render_text(f'Ваш баланс: {player.get_card_balance()} рублей', color=(232, 208, 79)),
(0, main_display.get_height() // 3))
main_display.blit(render_text('Назад', color=(232, 208, 79)), (main_display.get_width() - 125, 250))
elif mode == 'success':
main_display.blit(render_text('Успешно!', color=(232, 208, 79)),
(main_display.get_width() // 3, main_display.get_height() // 2))
elif mode == 'error':
main_display.blit(render_text('Ошибка!', color=(232, 208, 79)),
(main_display.get_width() // 3, main_display.get_height() // 2))
elif mode == 'deposit':
main_display.blit(render_text('Введите сумму:', color=(232, 208, 79)),
(main_display.get_width() // 4, main_display.get_height() // 2 - 50))
main_display.blit(render_text(deposit_summ, color=(232, 208, 79)),
(main_display.get_width() // 2, main_display.get_height() // 2))
screen.fill((156, 65, 10))
player.update_params()
button_group.draw(screen)
bank_buttons.draw(screen)
screen.blit(player.render_info(background=(156, 65, 10)), (0, 0))
screen.blit(main_display, (100, 70))
pygame.draw.rect(screen, (0, 0, 0), hole_rect)
pygame.display.flip()
clock.tick(fps)
bank_buttons.empty()
button_group.empty()
class MainHouse(pygame.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(groups)
self.image = load_image('data/buildings/main_house.png', size=(250, 500))
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
self.mask = pygame.mask.from_surface(self.image)
self.name = 'Дом'
self.clock = pygame.time.Clock()
def is_obstacle(self):
return False
def enter(self):
def play_radio_info():
stop_speeches()
speeches['news'].play()
return True
def success():
screen.fill((177, 170, 142))
text = render_text('Поздравляем! Вы выполнили задание уровня успешно!')
screen.blit(text, (width // 2 - text.get_width() // 2, height // 2))
pygame.display.flip()
for i in range(3):
self.clock.tick(1)
button_group.empty()
global level
if level == 1:
aims = ['Маска']
elif level == 2:
aims = ['Спирт', 'Мыло']
elif level == 3:
aims = ['Морковь', 'Картофель', 'Яблоко', 'Маска']
if not aims:
level = None
return
for i in player.get_objects():
if i.name == 'Маска' and not i.is_used:
break
if i.name in aims:
aims.remove(i.name)
if not aims:
success()
level = None
return
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, house_buttons)
radio = Button(width - 350, height // 2 - 137, 170, 145, images['radio'], play_radio_info, None, house_buttons)
backgr = pygame.sprite.Sprite(background_house)
backgr.image = load_image('data/inside/room.png', size=size)
backgr.rect = backgr.image.get_rect()
running = True
Button(width - images['exit_sign'].get_width(), height - images['exit_sign'].get_height(),
100, 50, images['exit_sign'], lambda: False, None, house_buttons)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for btn in house_buttons:
if btn.rect.collidepoint(event.pos):
running = btn.run()
data = pygame.key.get_pressed()
player.set_moving(False)
if data[27]:
menu(pause=True)
button_group.empty()
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, house_buttons)
screen.fill((177, 170, 142))
background_house.draw(screen)
button_group.draw(screen)
house_buttons.draw(screen)
house_group.draw(screen)
screen.blit(player.render_info(background=(177, 170, 142)), (0, 0))
pygame.display.flip()
clock.tick(fps)
house_buttons.empty()
house_group.empty()
class Shop(pygame.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(groups)
self.image = load_image('data/buildings/shop_1.png', size=(250, 200))
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
self.mask = pygame.mask.from_surface(self.image)
self.name = 'Продуктовый магазин'
def is_obstacle(self):
return False
def enter(self):
def checkout():
def buy():
summ = sum([i.get_price() for i in cart])
if player.spend_money(summ):
player.add_objects(*cart)
for i in cart:
i.buy()
return False, True, 'success'
return False, True, 'error'
running = True
shop_buttons.empty()
Button(width // 3, height - 100, 200, 50, render_text('Назад'), lambda: (False, True, 'ok'), None,
shop_buttons)
Button(width // 3 * 2, height - 100, 200, 50, render_text('Купить'), buy, None, shop_buttons)
Button(width - images['exit_sign'].get_width(), height - images['exit_sign'].get_height(),
100, 50, images['exit_sign'], lambda: (False, False, 'ok'), None, shop_buttons)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for btn in shop_buttons:
if btn.rect.collidepoint(event.pos):
running, run, status = btn.run()
data = pygame.key.get_pressed()
player.set_moving(False)
if data[27]:
menu(pause=True)
button_group.empty()
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, button_group)
screen.fill((156, 65, 10))
shop_buttons.draw(screen)
screen.blit(player.render_info(background=(156, 65, 10)), (0, 0))
for num in range(len(cart)):
screen.blit(render_text(f'{num + 1} -- {cart[num].name} ------ {cart[num].get_price()}'),
(width // 2, height // 2 - 200 + 35 * num))
pygame.display.flip()
clock.tick(fps)
shop_buttons.empty()
return run, status
def reset():
carrot = products['carrot']
if carrot.can_be_bought():
carrot.set_pos((350, 180))
carrot.add_to_groups(shop_products)
potato = products['potato']
if potato.can_be_bought():
potato.set_pos((550, 180))
potato.add_to_groups(shop_products)
if level == 3:
apple = products['apple']
if apple.can_be_bought():
apple.set_pos((350, 300))
apple.add_to_groups(shop_products)
button_group.empty()
if level == 1:
return
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, pharm_buttons)
cancel_button = Button(width - 150, 350, 50, 50,
load_image('data/objects/cancel_button.png', size=(50, 50)),
None, 'clear', shop_buttons)
backgr = pygame.sprite.Sprite(background_shop)
backgr.image = load_image('data/inside/shop_1_inside.png', size=size)
backgr.rect = backgr.image.get_rect()
running = True
reset()
cart = []
cart_rect = pygame.Rect(950, 300, 150, 450)
Button(width - images['exit_sign'].get_width(), height - images['exit_sign'].get_height(),
100, 50, images['exit_sign'], lambda: False, None, shop_buttons)
status = 'ok'
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for btn in shop_buttons:
if btn.rect.collidepoint(event.pos):
if btn.id == 'clear':
cart = []
reset()
else:
running = btn.run()
for product in shop_products:
if product.rect.collidepoint(event.pos):
cart.append(product)
for i in cart:
i.reset_groups()
if cart_rect.collidepoint(event.pos):
running, status = checkout()
if status == 'success':
cart = []
Button(width - images['exit_sign'].get_width(), height - images['exit_sign'].get_height(),
100, 50, images['exit_sign'], lambda: False, None, shop_buttons)
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, shop_buttons)
data = pygame.key.get_pressed()
player.set_moving(False)
if data[27]:
menu(pause=True)
button_group.empty()
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, button_group)
screen.fill((156, 65, 10))
player.update_params()
background_shop.draw(screen)
button_group.draw(screen)
shop_buttons.draw(screen)
shop_group.draw(screen)
shop_products.draw(screen)
if status == 'error':
screen.blit(render_text('Недостаточно средств! Посетите банк!', color=(255, 0, 0)), (200, 100))
screen.blit(player.render_info(background=(179, 185, 206)), (0, 0))
pygame.display.flip()
clock.tick(fps)
shop_buttons.empty()
shop_group.empty()
shop_products.empty()
class SecondShop(pygame.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(groups)
self.image = load_image('data/buildings/shop_2.png', size=(250, 200))
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
self.mask = pygame.mask.from_surface(self.image)
self.name = 'Хозяйственный магазин'
def is_obstacle(self):
return False
def enter(self):
def checkout():
def buy():
summ = sum([i.get_price() for i in cart])
if player.spend_money(summ):
player.add_objects(*cart)
for i in cart:
i.buy()
return False, True, 'success'
return False, True, 'error'
running = True
shop_buttons.empty()
Button(width // 3, height - 100, 200, 50, render_text('Назад'), lambda: (False, True, 'ok'), None,
shop_buttons)
Button(width // 3 * 2, height - 100, 200, 50, render_text('Купить'), buy, None, shop_buttons)
Button(width - images['exit_sign'].get_width(), height - images['exit_sign'].get_height(),
100, 50, images['exit_sign'], lambda: (False, False, 'ok'), None, shop_buttons)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for btn in shop_buttons:
if btn.rect.collidepoint(event.pos):
running, run, status = btn.run()
data = pygame.key.get_pressed()
player.set_moving(False)
if data[27]:
menu(pause=True)
button_group.empty()
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, button_group)
screen.fill((156, 65, 10))
shop_buttons.draw(screen)
screen.blit(player.render_info(background=(156, 65, 10)), (0, 0))
for num in range(len(cart)):
screen.blit(render_text(f'{num + 1} -- {cart[num].name} ------ {cart[num].get_price()}'),
(width // 2, height // 2 - 200 + 35 * num))
pygame.display.flip()
clock.tick(fps)
shop_buttons.empty()
return run, status
def reset():
if level == 2 or level == 3:
soap = products['soap']
if soap.can_be_bought():
soap.set_pos((950, 180))
soap.add_to_groups(shop_products)
button_group.empty()
if level == 1:
return
pause_button = Button(width - 50, 0, 50, 50, images['pause_button'], menu, None, pharm_buttons)
cancel_button = Button(400, 350, 50, 50,
load_image('data/objects/cancel_button.png', size=(50, 50)),
None, 'clear', shop_buttons)
backgr = pygame.sprite.Sprite(background_shop)
backgr.image = load_image('data/inside/shop_2_inside.png', size=size)
backgr.rect = backgr.image.get_rect()
running = True
reset()
cart = []
cart_rect = pygame.Rect(300, 327, 80, 200)
Button(width - images['exit_sign'].get_width(), height - images['exit_sign'].get_height(),
100, 50, images['exit_sign'], lambda: False, None, shop_buttons)