-
Notifications
You must be signed in to change notification settings - Fork 5
/
object_renderer.py
64 lines (53 loc) · 2.59 KB
/
object_renderer.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
import pygame as pg
from settings import *
class ObjectRenderer:
def __init__(self, game):
self.game = game
self.screen = game.screen
self.wall_textures = self.load_wall_textures()
self.sky_image = self.get_texture('resources/textures/sky.png', (WIDTH, HALF_HEIGHT))
self.sky_offset = 0
self.blood_screen = self.get_texture('resources/textures/blood_screen.png', RES)
self.digit_size = 90
self.digit_images = [self.get_texture(f'resources/textures/digits/{i}.png', [self.digit_size] * 2)
for i in range(11)]
self.digits = dict(zip(map(str, range(11)), self.digit_images))
self.game_over_image = self.get_texture('resources/textures/game_over.png', RES)
self.win_image = self.get_texture('resources/textures/win.png', RES)
def draw(self):
self.draw_background()
self.render_game_objects()
self.draw_player_health()
def win(self):
self.screen.blit(self.win_image, (0, 0))
def game_over(self):
self.screen.blit(self.game_over_image, (0, 0))
def draw_player_health(self):
health = str(self.game.player.health)
for i, char in enumerate(health):
self.screen.blit(self.digits[char], (i * self.digit_size, 0))
self.screen.blit(self.digits['10'], ((i + 1) * self.digit_size, 0))
def player_damage(self):
self.screen.blit(self.blood_screen, (0, 0))
def draw_background(self):
self.sky_offset = (self.sky_offset + 4.5 * self.game.player.rel) % WIDTH
self.screen.blit(self.sky_image, (-self.sky_offset, 0))
self.screen.blit(self.sky_image, (-self.sky_offset + WIDTH, 0))
# floor
pg.draw.rect(self.screen, FLOOR_COLOR, (0, HALF_HEIGHT, WIDTH, HEIGHT))
def render_game_objects(self):
list_objects = sorted(self.game.raycasting.objects_to_render, key=lambda t: t[0], reverse=True)
for depth, image, pos in list_objects:
self.screen.blit(image, pos)
@staticmethod
def get_texture(path, res=(TEXTURE_SIZE, TEXTURE_SIZE)):
texture = pg.image.load(path).convert_alpha()
return pg.transform.scale(texture, res)
def load_wall_textures(self):
return {
1: self.get_texture('resources/textures/1.png'),
2: self.get_texture('resources/textures/2.png'),
3: self.get_texture('resources/textures/3.png'),
4: self.get_texture('resources/textures/4.png'),
5: self.get_texture('resources/textures/5.png'),
}