-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.py
44 lines (37 loc) · 1.57 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
import pygame
from math import sin
class Entity(pygame.sprite.Sprite):
def __init__(self, groups):
super().__init__(groups)
self.frame_index = 0
self.animation_speed = 0.15
self.direction = pygame.math.Vector2()
def move(self, speed):
if self.direction.magnitude() != 0:
self.direction = self.direction.normalize()
self.hitbox.x += self.direction.x * speed
self.collision('horizontal')
self.hitbox.y += self.direction.y * speed
self.collision('vertical')
self.rect.center = self.hitbox.center
def collision(self, direction):
if direction == 'horizontal':
for sprite in self.obstacle_sprites:
if sprite.hitbox.colliderect(self.hitbox):
if self.direction.x > 0: # moving right
self.hitbox.right = sprite.hitbox.left
if self.direction.x < 0: # moving left
self.hitbox.left = sprite.hitbox.right
if direction == 'vertical':
for sprite in self.obstacle_sprites:
if sprite.hitbox.colliderect(self.hitbox):
if self.direction.y > 0: # moving down
self.hitbox.bottom = sprite.hitbox.top
if self.direction.y < 0: # moving up
self.hitbox.top = sprite.hitbox.bottom
def wave_value(self): # implement sine wave at 5:23:06
value = sin(pygame.time.get_ticks())
if value >= 0:
return 255
else:
return 0