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
|
import pygame
from random import randrange
from display import Scaling
from images import Images
from stats import Stats
class EnemySprite(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.position = (0, 0)
self.movement = (0, 0)
def set_position(self, position: tuple):
self.position = position
self.update()
def set_movement(self, movement: tuple):
self.movement = movement
def update(self, move = 0):
if move:
self.position = (
self.position[0] + self.movement[0] * move,
self.position[1] + self.movement[1] * move)
self.rect = pygame.Rect(
Scaling.scale_to_display(self.position),
self.image.get_size())
def force_right(self):
self.movement = (abs(self.movement[0]), self.movement[1])
def force_left(self):
self.movement = (-abs(self.movement[0]), self.movement[1])
def force_down(self):
self.movement = (self.movement[0], abs(self.movement[1]))
def force_up(self):
self.movement = (self.movement[0], -abs(self.movement[1]))
class EnemyBall(EnemySprite):
def __init__(self):
super().__init__()
self.image = Images.surfaces['ball']
self.update()
class Enemies():
def __init__(self, field = None):
self.sprites = pygame.sprite.Group()
self.emerge_enemies()
self.field = field
def emerge_enemies(self):
for _ in range(3 + (Stats.level-1) * 2):
enemy = EnemyBall()
enemy.set_position( (randrange(0,380_000), randrange(0,260_000)) )
enemy.set_movement( (randrange(0,200)-100, randrange(0,200)-100) )
self.sprites.add(enemy)
def wall_hit(self, field, enemy):
if enemy.rect.x < field.rect.x:
enemy.force_right()
if enemy.rect.y < field.rect.y:
enemy.force_down()
if ( enemy.rect.x + enemy.rect.w >= field.rect.x + field.rect.w ):
enemy.force_left()
if ( enemy.rect.y + enemy.rect.h >= field.rect.y + field.rect.h ):
enemy.force_up()
def update(self, dt):
""" Update sprites basis of dt. dt = milliseconds from last update """
self.sprites.update(move=dt)
for enemy in self.sprites:
for field in self.field.sprites:
if field.rect.contains(enemy):
break
else:
for field in self.field.sprites:
if field.rect.colliderect(enemy):
self.wall_hit(field, enemy)
|