added 2 new classes
This commit is contained in:
parent
295bdb722e
commit
2ace3e9cea
1 changed files with 58 additions and 0 deletions
58
main.py
58
main.py
|
|
@ -9,6 +9,64 @@ pygame.display.set_caption("Sugarmania")
|
|||
clock = pygame.time.Clock()
|
||||
font = pygame.font.SysFont(None, 36)
|
||||
|
||||
class ChaserEnemy:
|
||||
def __init__(self, avoid_rect):
|
||||
self.rect = pygame.Rect(0, 0, 36, 36)
|
||||
while True:
|
||||
self.rect.x = random.randint(0, WIDTH - 36)
|
||||
self.rect.y = random.randint(0, HEIGHT - 36)
|
||||
if abs(self.rect.x - avoid_rect.x) > 150 or abs(self.rect.y - avoid_rect.y) > 150:
|
||||
break
|
||||
self.speed = 2
|
||||
self.spawn_timer = 60
|
||||
|
||||
def update(self, target):
|
||||
if self.rect.centerx < target.centerx:
|
||||
self.rect.x += self.speed
|
||||
elif self.rect.centerx > target.centerx:
|
||||
self.rect.x -= self.speed
|
||||
if self.rect.y < target.centery:
|
||||
self.rect.y += self.speed
|
||||
elif self.rect.centery > target.centery:
|
||||
self.rect.y -= self.speed
|
||||
class DasherEnemy:
|
||||
def __init__(self, avoid_rect):
|
||||
self.rect = pygame.Rect(0, 0, 36, 36)
|
||||
while True:
|
||||
self.rect.x = random.randint(0, WIDTH - 36)
|
||||
self.rect.y = random.randint(0, HEIGHT - 36)
|
||||
if abs(self.rect.x - avoid_rect.x) > 150 or abs(self.rect.y - avoid_rect.y) > 150:
|
||||
break
|
||||
self.x = float(self.rect.x)
|
||||
self.y = float(self.rect.y)
|
||||
self.vx = 0
|
||||
self.vy = 0
|
||||
self.state = "wait"
|
||||
self.timer = 90
|
||||
self.spawn_timer = 60
|
||||
|
||||
def update(self, target):
|
||||
self.timer -= 1
|
||||
if self.state == "wait":
|
||||
dx = target.centerx - self.rect.centerx
|
||||
dy = target.centery - self.rect.centery
|
||||
dist = max(1, (dx ** 2 + dy ** 2) ** 0,5)
|
||||
self.vx = dx / dist * 9
|
||||
self.vy = dy / dist * 9
|
||||
self.state = "dash"
|
||||
self.timer = 30
|
||||
else:
|
||||
self.x += self.vx
|
||||
self.y += self.vy
|
||||
self.rect.x = int(self.x)
|
||||
self.rect.y = int(self.y)
|
||||
self.rect.clamp_ip(screen.get_rect())
|
||||
self.x, self.y = float(self.rect.x), float(self.rect.y)
|
||||
if self.timer <= 0:
|
||||
self.state = "wait"
|
||||
self.timer = 90
|
||||
|
||||
|
||||
player_img = pygame.image.load("player.png").convert_alpha()
|
||||
player_img = pygame.transform.scale(player_img, (40, 60))
|
||||
player = pygame.Rect(300, 220, 40, 60)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue