-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemies.py
74 lines (62 loc) · 2.57 KB
/
enemies.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
import pygame
from settings import *
from math import atan, degrees
import time
class Enemy:
def __init__(self, enemy_images, pos, screen):
self.pos = pygame.math.Vector2(pos)
self.speed = enemy_speed
self.screen = screen
self.enemy_images = enemy_images
self.img = enemy_images[0]
self.img_height = self.img.get_height()
self.img_width = self.img.get_width()
self.total_time = 0
self.direction = pygame.math.Vector2(-1, 0)
self.past_player = False
self.shooting = False
self.last_shoot_time = time.time()
def move(self, dt):
x_collide = True
y_collide = True
if self.pos.x > screen_width - self.img_width / 2:
self.pos.x = screen_width - self.img_width / 2
else:
x_collide = False
if self.pos.y < self.img_height / 2:
self.pos.y = self.img_height / 2
elif self.pos.y > screen_height - self.img_height / 2:
self.pos.y = screen_height - self.img_height / 2
else:
y_collide = False
if not (x_collide or y_collide):
if self.direction.magnitude() > 0:
self.direction = self.direction.normalize()
self.pos += self.direction * dt * self.speed
def update_direction(self, player_pos):
self.direction.x += (player_pos.x - self.pos.x) * \
enemy_turn_speed / 100000
self.direction.y += (player_pos.y - self.pos.y) * \
enemy_turn_speed / 100000
def render(self):
if self.direction.x > 0:
self.img = pygame.transform.flip(self.img, True, False)
self.img = pygame.transform.rotate(self.img, degrees(-atan(self.direction.y / self.direction.x)))
self.img.set_colorkey('white')
self.img_height = self.img.get_height()
self.img_width = self.img.get_width()
self.screen.blit(
self.img, (self.pos.x - self.img_height / 2, self.pos.y - self.img_width / 2))
self.img = self.enemy_images[round(self.total_time) % 8]
def update(self, dt, player_pos):
if enemy_sight_radius ** 2 > ((self.pos.x - player_pos.x) ** 2 + (self.pos.y - player_pos.y) ** 2):
self.update_direction(player_pos)
else:
self.direction = pygame.math.Vector2(-1, 0)
self.shooting = enemy_sight_radius ** 2 > (
(self.pos.x - player_pos.x) ** 2 + (self.pos.y - player_pos.y) ** 2)
self.move(dt)
self.total_time += dt
if self.pos.x < - self.img_height:
return False
return True