-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroid.py
133 lines (121 loc) · 5.16 KB
/
asteroid.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from neural_network import neural_network
import rocket
import numpy as np
import enum
from constants import *
import pygame
import bullet
class ASTEROID_STATUS(enum.Enum):
ALIVE = 0
DESTROYED = 1
class asteroid(pygame.sprite.Sprite):
destroyed_by_player: bool
network: neural_network = None
position: np.array = None
velocity: np.array = None
status: ASTEROID_STATUS = ASTEROID_STATUS.ALIVE
network_inp_count: int
image: pygame.surface.Surface
radius: float
rect: pygame.rect.Rect
avg_dist_sqrt: float
avg_dist_samples: float
last_avg_dist_sample_tick: float
least_dist_sq: float
def __init__(self, asteroid_count: int, start_position: np.array,
radius: float, start_velocity: np.array):
"""
Initiate all asteroids with a usual parameters, generate a random
spawn location, and rotate the asteroid by a random angle for each asteroid to be slightly different
"""
super().__init__()
self.destroyed_by_player = False
self.network_inp_count = 2
self.network = neural_network([self.network_inp_count, 2])
self.position = start_position.flat.copy()
self.velocity = np.array(start_velocity)
self.status = ASTEROID_STATUS.ALIVE
self.image = pygame.transform.rotate(pygame.transform.smoothscale(
pygame.image.load('asteroid.png').convert_alpha(),
(2*int(generalise_height(radius)),
2*int(generalise_height(radius)))), np.random.random()*360)
self.rect = self.image.get_rect()
self.rect.center = self.position
self.radius = radius
self.least_dist_sq = 0
self.last_avg_dist_sample_tick = pygame.time.get_ticks()
self.avg_dist_samples = 0
self.avg_dist_sqrt = window_width**0.5
def evolve_from(self, parent):
self.network.from_parent(parent.network)
def die(self):
self.status = ASTEROID_STATUS.DESTROYED
def update(self, asteroids: pygame.sprite.Group, player: rocket.rocket,
dt: float, bullets: list):
if self.status == ASTEROID_STATUS.ALIVE:
"""
Calculates acceleration
"""
inp = [None] * self.network_inp_count
# inp[0] = self.velocity[0]
# inp[1] = self.velocity[1]
inp[0] = player.position[0] - self.position[0]
inp[1] = player.position[1] - self.position[1]
out = self.network.calculate(np.array(inp))
"""
Updates the velocity and position
"""
dx = player.position[0] - self.position[0]
dy = player.position[1] - self.position[1]
dist_sq = dx**2 + dy**2
self.velocity[0] += out[0] + 0.001* dx * asteroid_accel_coeff * dt
self.velocity[1] += out[1] + 0.001* dy * asteroid_accel_coeff * dt
self.position[0] += self.velocity[0] * asteroid_vel_coeff * dt
self.position[1] += self.velocity[1] * asteroid_vel_coeff * dt
self.rect.center = (int(self.position[0]), int(self.position[1]))
"""
Calculates the average square root of
the distance between asteroid and the player
"""
if dist_sq < self.least_dist_sq or self.least_dist_sq < 0:
self.least_dist_sq = dist_sq
"""
Checks if asteroid has collided with another
asteroid or bullet
"""
asteroids.remove(self)
for asteroid in asteroids:
dx = asteroid.position[0] - self.position[0]
dy = asteroid.position[1] - self.position[1]
if dx**2 + dy**2 <= 4*(asteroid.radius+self.radius)**2:
self.die()
asteroid.die()
asteroids.add(self)
return
asteroids.add(self)
for bult in bullets:
if self.rect.collidepoint(bult.position[0], bult.position[1]):
bullets.remove(bult)
self.destroyed_by_player = True
self.die()
return
if (pygame.time.get_ticks() - self.last_avg_dist_sample_tick) /\
1000 >= 0.01:
dx = self.position[0] - target_position[0]
dy = self.position[1] - target_position[1]
if self.avg_dist_sqrt < 0:
self.avg_dist_sqrt
total = self.avg_dist_sqrt * \
self.avg_dist_samples + (dx**2 + dy**2)**0.5
self.avg_dist_samples += 1
self.avg_dist_sqrt = total/self.avg_dist_samples
self.last_avg_dist_sample_tick = pygame.time.get_ticks()
if self.position[0] < 0-2*self.radius or\
self.position[0] > window_width+2*self.radius or\
self.position[1] < 0-2*self.radius or\
self.position[1] > window_height+2*self.radius:
self.die()
return
def draw(self, surface: pygame.surface.Surface):
surface.blit(self.image, self.image.get_rect(
center=(int(self.position[0]), int(self.position[1]))))