-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.py
69 lines (55 loc) · 2.08 KB
/
entity.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
__author__ = "Alon B.R."
import pygame
from settings import SCALE, GRAVITY
from vector2 import Vector2
pygame.init()
class Entity:
def __init__(self, x, y, img, speed, movement_range, health, mass) -> None:
self.img = img
self.width = self.img.get_width()
self.height = self.img.get_height()
self.rect = self.img.get_rect()
self.rect.x = x
self.rect.y = y
self.health = health
self.movement_range = movement_range
self.speed = speed
self.move_counter = 0
self.move_direction: Vector2 = Vector2(1, 0)
self.mass = mass
self.acceleration: float = (self.mass * GRAVITY) / SCALE
self.velocity = Vector2(0, 0)
def update(self, window, world, dt) -> None:
dx = 0
dy = 0
# movement
dx += self.speed * self.move_direction.x
self.move_counter += abs(self.speed)
if abs(self.move_counter) > self.movement_range:
self.move_direction.x *= -1
self.move_counter *= -1
self.velocity.y += self.acceleration * dt
dx += self.velocity.x
dy += self.velocity.y
for tile in world.tile_data:
# check for collision in y
if tile.rect.colliderect(
self.rect.x, self.rect.y + dy, self.width, self.height
):
if dy >= 0:
dy = tile.rect.top - self.rect.bottom
self.velocity.y = 0
elif dy < 0:
dy = tile.rect.bottom - self.rect.top
self.velocity.y = 0
# check collision in x
if tile.rect.colliderect(
self.rect.x + dx, self.rect.y, self.width, self.height
):
if dx >= 0:
dx = tile.rect.x - (self.rect.x + self.width)
if dx < 0:
dx = (tile.rect.x + tile.width) - self.rect.x
self.rect.x += dx
self.rect.y += dy
window.blit(self.img, self.rect)