blob: 600ea261e6e8ebd5624341f40718afbfaffb9c8e (
plain)
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
|
""" enemy.enemy - Enemy super class. Wall hitting and other commons """
import pygame
from sliceitoff.display import Scaling
class Enemy(pygame.sprite.Sprite):
""" Enemy super class. Just common movements. """
def __init__(self):
super().__init__()
self.position = (0, 0)
self.movement = (0, 0)
self.rect = None
def update(self, dt = 0, wall_hit = None):
""" hit walls, update position and rect """
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())
|