-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.py
115 lines (100 loc) · 4.32 KB
/
player.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
import pygame
from file_manager import get_image, get_sound
class Player(pygame.sprite.Sprite):
def __init__(self, *groups):
super().__init__(*groups)
self.width = 60
self.pl_right = pygame.image.load(get_image('right_1.png')).convert_alpha()
self.pl_left = pygame.image.load(get_image('left_1.png')).convert_alpha()
self.pl_left_pr = pygame.image.load(get_image('left.png')).convert_alpha()
self.pl_right_pr = pygame.image.load(get_image('right.png')).convert_alpha()
self.image = self.pl_right
self.is_jump = False
self.jump = 0
self.rect = pygame.rect.Rect(0, 0, 60, 10)
self.rect.x = 400
self.rect.y = 270
self.speed_down = 5
self.speed_up = 5
self.shoot_pass = 0
def down(self, boosts, monsters):
for el in boosts:
if pygame.sprite.collide_rect(self, el) and not self.is_jump:
self.is_jump = True
self.jump = el.jump_range
self.speed_up = el.jump_speed
el.play_sound()
el.jump()
if not self.is_jump:
self.rect.y += self.speed_down
elif self.jump == 0 or self.jump < 0:
self.is_jump = False
self.speed_up = 5
elif self.is_jump:
if self.rect.y <= 400:
for el in boosts:
el.rect.y += self.speed_up
for monster in monsters:
monster.rect.y += self.speed_up
self.rect.y += self.speed_up
self.rect.y -= self.speed_up
self.jump -= self.speed_up
if self.image == self.pl_right and self.jump > 100:
self.image = self.pl_right_pr
elif self.image == self.pl_left and self.jump > 100:
self.image = self.pl_left_pr
elif self.jump <= 100 and self.image == self.pl_right_pr:
self.image = self.pl_right
elif self.jump <= 100 and self.image == self.pl_left_pr:
self.image = self.pl_left
if self.rect.x < -80:
self.rect.x = 580
elif self.rect.x > 680:
self.rect.x = -40
def shoot(self):
self.shoot_pass = 60
return Bullet(self.rect.x, self.rect.y - 82)
def draw(self, screen: pygame.Surface):
screen.blit(self.image, (self.rect.x - 35, self.rect.y - 110))
def update_images(self):
self.pl_right = pygame.image.load(get_image('right_1.png')).convert_alpha()
self.pl_left = pygame.image.load(get_image('left_1.png')).convert_alpha()
self.pl_left_pr = pygame.image.load(get_image('left.png')).convert_alpha()
self.pl_right_pr = pygame.image.load(get_image('right.png')).convert_alpha()
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(*groups)
self.speed = 10
self.image = pygame.image.load(get_image('bullet.png')).convert_alpha()
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.x = x
self.rect.y = y
self.sound = pygame.mixer.Sound(get_sound('pistol_shoot.mp3'))
self.play_sound()
def play_sound(self):
self.sound.play()
def update(self):
self.rect.y -= self.speed
def draw(self, screen):
screen.blit(self.image, (self.rect.x, self.rect.y))
class Monster(pygame.sprite.Sprite):
def __init__(self, x, y, *groups):
super().__init__(*groups)
self.image_one = pygame.image.load(get_image('bat1.png')).convert_alpha()
self.image_two = pygame.image.load(get_image('bat2.png')).convert_alpha()
self.image_three = pygame.image.load(get_image('bat3.png')).convert_alpha()
self.images = [self.image_one, self.image_two, self.image_three]
self.image = self.images[0]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 5
self.direction = 1
def update(self):
self.image = self.images[(self.images.index(self.image) + 1) % 3]
if self.rect.x < 55 or self.rect.x >= 520:
self.direction = -self.direction
self.rect.x += self.direction * self.speed
def draw(self, screen: pygame.Surface):
screen.blit(self.image, (self.rect.x, self.rect.y))