-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
73 lines (60 loc) · 2.2 KB
/
player.py
File metadata and controls
73 lines (60 loc) · 2.2 KB
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
import pygame
# defination
SCREEN_WIDTH = 0
SCREEN_HEIGHT = 1
class Player(pygame.sprite.Sprite):
def __init__(self, plane_img, player_rect, init_pos, player_size):
pygame.sprite.Sprite.__init__(self)
self.image = []
for i in range(len(player_rect)):
self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
self.rect = player_rect[0]
self.rect.topleft = init_pos
self.speed = 8
self.bullets = pygame.sprite.Group()
self.img_index = 0
self.is_hit = False
self.screen_size = player_size
def shoot(self, bullet_img):
bullet = Bullet(bullet_img, self.rect.midtop)
self.bullets.add(bullet)
def move_up(self):
if self.rect.top <= 0:
self.rect.top = 0
else:
self.rect.top -= self.speed
def move_down(self):
if self.rect.bottom >= self.screen_size[SCREEN_HEIGHT]:
self.rect.bottom = self.screen_size[SCREEN_HEIGHT]
else:
self.rect.bottom += self.speed
def move_left(self):
if self.rect.left <= 0:
self.rect.left = 0
else:
self.rect.left -= self.speed
def move_right(self):
if self.rect.right >= self.screen_size[SCREEN_WIDTH]:
self.rect.right = self.screen_size[SCREEN_WIDTH]
else:
self.rect.right += self.speed
class Bullet(pygame.sprite.Sprite):
def __init__(self, bullet_img, init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.midbottom = init_pos
self.speed = 10
def move(self):
self.rect.top -= self.speed
def init_player(plane_image, play_size):
player_rect = []
player_rect.append(pygame.Rect(0, 99, 102, 126))
player_rect.append(pygame.Rect(165, 360, 102, 126))
player_rect.append(pygame.Rect(165, 234, 102, 126))
player_rect.append(pygame.Rect(330, 624, 102, 126))
player_rect.append(pygame.Rect(330, 498, 102, 126))
player_rect.append(pygame.Rect(432, 624, 102, 126))
player_pos = [200, 600]
player = Player(plane_image, player_rect, player_pos, play_size)
return player