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
|
import pygame
from random import randrange
from display import Scaling
from text import get_letter_surface
BALL_SPAWN_AREA = (0, 0, 300_000, 200_000)
BALL_MOVEMENT = (100, 100)
class EnemyBall(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.position = (
randrange(BALL_SPAWN_AREA[0], BALL_SPAWN_AREA[2]),
randrange(BALL_SPAWN_AREA[1], BALL_SPAWN_AREA[3]))
self.movement = (
randrange(0, BALL_MOVEMENT[0]*2) - BALL_MOVEMENT[0],
randrange(0, BALL_MOVEMENT[1]*2) - BALL_MOVEMENT[1])
self.image = get_letter_surface(
('8x8', 8_000, 0),
randrange(1,3)).subsurface(
(0,0,8_000*Scaling.factor,8_000*Scaling.factor))
self.update()
self.rect = None
def update(self, dt = 0, wall_hit = None, **kwargs):
if wall_hit:
if self.rect.x < wall_hit.x:
self.movement = (abs(self.movement[0]), self.movement[1])
if self.rect.y < wall_hit.y:
self.movement = (self.movement[0], abs(self.movement[1]))
if self.rect.x + self.rect.w >= wall_hit.x + wall_hit.w:
self.movement = (-abs(self.movement[0]), self.movement[1])
if self.rect.y + self.rect.h >= wall_hit.y + wall_hit.h:
self.movement = (self.movement[0], -abs(self.movement[1]))
return
if dt:
self.position = (
self.position[0] + self.movement[0] * dt,
self.position[1] + self.movement[1] * dt)
self.rect = pygame.Rect(
Scaling.scale_to_display(self.position),
self.image.get_size())
class Enemies(pygame.sprite.Group):
def __init__(self, field = None, count = 0):
super().__init__()
for _ in range(count):
self.add(EnemyBall())
self.field = field
def update(self, field_rects = [], **kwargs):
""" Do actions on enemies that are only partly on the fields """
super().update(**kwargs)
for enemy in self.sprites():
for field_rect in field_rects:
# if enemy is completely inside any field do next enemy
if field_rect.contains(enemy):
break
else:
# now find field that enemy is partly on
for field_rect in field_rects:
if field_rect.colliderect(enemy):
enemy.update(wall_hit = field_rect)
|