-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite_object.py
58 lines (48 loc) · 2.28 KB
/
sprite_object.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
import pygame as pg
import settings
import math
class SpriteObject:
def __init__(self, game, path='resources/sprites/donut.png', pos=(3.5, 3.5), scale=0.5, shift=0.5, color='red'):
self.game = game
self.player = game.player
self.x, self.y = pos
self.image = pg.image.load(path).convert_alpha()
self.IMAGE_WIDTH = self.image.get_width()
self.IMAGE_HALF_WIDTH = self.IMAGE_WIDTH // 2
self.IMAGE_RATIO = self.IMAGE_WIDTH / self.image.get_height()
self.dx, self.dy, self.theta, self.screen_x, self.dist, self.norm_dist = 0, 0, 0, 0, 1, 1
self.sprite_half_width = 0
self.SPRITE_SCALE = scale
self.SPRITE_HEIGHT_SHIFT = shift
self.color = color
def get_sprite_projection(self):
proj = settings.SCREEN_DIST / self.norm_dist * self.SPRITE_SCALE
proj_width, proj_height = proj * self.IMAGE_RATIO, proj
image = pg.transform.scale(self.image, (proj_width, proj_height))
self.sprite_half_width = proj_width // 2
height_shift = proj_height * self.SPRITE_HEIGHT_SHIFT
pos = self.screen_x - self.sprite_half_width, settings.HALF_HEIGHT - proj_height // 2 + height_shift
self.game.raycasting.objects_to_render.append((self.norm_dist, image, pos))
def get_sprite(self):
dx = self.x - self.player.x
dy = self.y - self.player.y
self.dx, self.dy = dx, dy
self.theta = math.atan2(dy, dx)
delta = self.theta - self.player.angle
if (dx > 0 and self.player.angle > math.pi) or (dx < 0 and dy < 0):
delta += math.tau
delta_rays = delta / settings.DELTA_ANGLE
self.screen_x = (settings.HALF_NUM_RAYS + delta_rays) * settings.SCALE
self.dist = math.hypot(dx, dy)
self.norm_dist = self.dist * math.cos(delta)
if -self.IMAGE_HALF_WIDTH < self.screen_x < (settings.WIDTH + self.IMAGE_HALF_WIDTH) and self.norm_dist > 0.5:
self.get_sprite_projection()
def update(self):
self.get_sprite()
def draw(self):
if self.game.mini_map_enabled:
pg.draw.circle(self.game.screen, self.color,
(self.x * settings.MINI_MAP_SCALE, self.y * settings.MINI_MAP_SCALE), 2)
@property
def pos(self):
return self.x, self.y