-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvader.py
83 lines (59 loc) · 2.6 KB
/
invader.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
import pygame, random
class Invader(pygame.sprite.Sprite):
def __init__(self, type, x, y):
super().__init__()
self.type = type
path = f"images/invader_{type}.png"
self.image = pygame.image.load(path)
self.rect = self.image.get_rect(topleft = (x,y))
def update(self, direction):
self.rect.x += direction
class BonusShip(pygame.sprite.Sprite):
def __init__(self,screen_width):
super().__init__()
self.screen_width = screen_width
self.image = pygame.image.load("images/bonus.png")
x = random.choice([0, screen_width - self.image.get_width()])
if x == 0:
self.speed = 3
else:
self.speed = -3
self.rect = self.image.get_rect(topleft = (x,40))
def update(self):
self.rect.x += self.speed
if self.rect.right > self.screen_width:
self.kill()
elif self.rect.left < 0:
self.kill()
class Boss(pygame.sprite.Sprite):
def __init__(self, screen_width, screen_height):
super().__init__()
self.screen_width = screen_width
self.screen_height = screen_height
self.image = pygame.image.load("images/mucpng.png")
self.rect = self.image.get_rect(topleft=((screen_width - self.image.get_width()) / 2, 40))
self.health = 5
self.hit_count = 0
self.speed = 4
self.direction_x = random.choice([-1, 1])
self.direction_y = random.choice([-1, 1])
self.change_direction_timer = pygame.time.get_ticks() # Thời gian thay đổi hướng
self.change_direction_interval = random.randint(500, 1500) # Thay đổi hướng sau 500-1500 ms
def update(self):
self.rect.x += self.speed * self.direction_x
self.rect.y += self.speed * self.direction_y
if self.rect.left < 0 or self.rect.right > self.screen_width:
self.direction_x *= -1
if self.rect.top < 0 or self.rect.bottom > self.screen_height:
self.direction_y *= -1
if pygame.time.get_ticks() - self.change_direction_timer > self.change_direction_interval:
self.direction_x = random.choice([-1, 1])
self.direction_y = random.choice([-1, 1])
self.change_direction_timer = pygame.time.get_ticks()
self.change_direction_interval = random.randint(500, 1500)
def take_damage(self):
self.hit_count += 1
if self.hit_count >= 15:
self.speed += 1 # Tăng tốc độ mỗi khi bị bắn
if self.hit_count >= self.health:
self.kill() # Giết boss khi hết máu