-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.py
165 lines (143 loc) · 6.39 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import pygame
import gradients
class Renderer:
# SIZES
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 640
WORDS_MARGIN_TOP = 90
TIME_TO_HIT_MARGIN_TOP = 60
BAR_MARGIN = 10
BAR_BORDER_WIDTH = 2
HEALTH_BAR_HEIGHT = 40
EXPERIENCE_BAR_HEIGHT = 20
EXPERIENCE_HEALTH_BAR_PADDING = 5
HIT_HEALTH_BAR_PADDING = 5
HIT_BAR_HEIGHT = 20
# COLORS
WORD_COLOR = (52, 52, 52)
BACKGROUND_COLOR = (56, 119, 119)
WORD_HIGHLIGHT_COLOR = (153, 179, 179)
HEALTH_BAR_COLOR_FROM = (255, 0, 0, 255)
HEALTH_BAR_COLOR_TO = (100, 10, 10, 255)
HEALTH_BAR_BACKGROUND_COLOR = (50, 50, 50)
EXPERIENCE_BAR_COLOR_FROM = (255, 165, 0, 255)
EXPERIENCE_BAR_COLOR_TO = (100, 65, 0, 255)
EXPERIENCE_BAR_BACKGROUND_COLOR = (50, 50, 50)
HIT_BAR_COLOR = (100, 200, 200)
HIT_BAR_BACKGROUND_COLOR = (50, 50, 50)
ENEMY_NAME_TEXT_COLOR = (200, 200, 200)
# FONTS
DEFAULT_FONT_BOLD = 'Inconsolata-Bold.ttf'
DEFAULT_FONT_REGULAR = 'Inconsolata-Regular.ttf'
WORD_FONT_SIZE = 32
# WIN AND LOSS SCREEN
WIN_BACKGROUND_COLOR = (210, 210, 210)
WIN_TEXT_COLOR = (50, 50, 50)
WIN_TEXT = "You win! :)"
LOSS_BACKGROUND_COLOR = (35, 35, 35)
LOSS_TEXT_COLOR = (255, 0, 0)
LOSS_TEXT = "You died! :("
def __init__(self):
self.screen = pygame.display.set_mode((Renderer.WINDOW_WIDTH, Renderer.WINDOW_HEIGHT))
self.font = pygame.font.Font(Renderer.DEFAULT_FONT_BOLD, Renderer.WORD_FONT_SIZE)
self.width, self.height = pygame.display.get_surface().get_size()
def text_objects(self, text, color, text_center):
rendered = self.font.render(text, True, color)
return rendered, rendered.get_rect(center=text_center)
def draw_background(self):
self.screen.fill(Renderer.BACKGROUND_COLOR)
def render_time_bar(self, max_time, current_time):
background, foreground = self.create_bar(
0,
Renderer.HIT_HEALTH_BAR_PADDING + Renderer.HEALTH_BAR_HEIGHT,
Renderer.HIT_BAR_HEIGHT,
current_time / max_time
)
pygame.draw.rect(self.screen, Renderer.HIT_BAR_BACKGROUND_COLOR, background)
pygame.draw.rect(self.screen, Renderer.HIT_BAR_COLOR, foreground)
def render_player_health_bar(self, max_health, current_health):
background, foreground = self.create_bar(
0,
self.height - Renderer.HEALTH_BAR_HEIGHT - 2 * Renderer.BAR_MARGIN - Renderer.EXPERIENCE_BAR_HEIGHT - Renderer.EXPERIENCE_HEALTH_BAR_PADDING,
Renderer.HEALTH_BAR_HEIGHT,
current_health / max_health
)
pygame.draw.rect(self.screen, Renderer.HEALTH_BAR_BACKGROUND_COLOR, background)
# pygame.draw.rect(self.screen, Renderer.HEALTH_BAR_COLOR, foreground)
self.draw_gradient(foreground, Renderer.HEALTH_BAR_COLOR_FROM, Renderer.HEALTH_BAR_COLOR_TO)
def render_enemy_health_bar(self, max_health, current_health, name):
background, foreground = self.create_bar(
0,
0,
Renderer.HEALTH_BAR_HEIGHT,
current_health / max_health
)
pygame.draw.rect(self.screen, Renderer.HEALTH_BAR_BACKGROUND_COLOR, background)
self.draw_gradient(foreground, Renderer.HEALTH_BAR_COLOR_FROM, Renderer.HEALTH_BAR_COLOR_TO)
label, rectangle = self.text_objects(
name,
Renderer.ENEMY_NAME_TEXT_COLOR,
(self.width / 2, Renderer.HEALTH_BAR_HEIGHT / 2 + Renderer.BAR_MARGIN)
)
self.screen.blit(label, rectangle)
def render_experience_bar(self, max_experience, current_experience):
background, foreground = self.create_bar(
0,
self.height - Renderer.EXPERIENCE_BAR_HEIGHT - 2 * Renderer.BAR_MARGIN,
Renderer.EXPERIENCE_BAR_HEIGHT,
current_experience / max_experience
)
pygame.draw.rect(self.screen, Renderer.EXPERIENCE_BAR_BACKGROUND_COLOR, background)
self.draw_gradient(foreground, Renderer.EXPERIENCE_BAR_COLOR_FROM, Renderer.EXPERIENCE_BAR_COLOR_TO)
def draw_gradient(self, rectangle, from_color, to_color):
self.screen.blit(gradients.vertical((rectangle.width, rectangle.height), from_color, to_color), (rectangle.left, rectangle.top))
def render_words(self, words, valid_words, current_letter_count):
for i in range(len(words)):
top = i * Renderer.WORD_FONT_SIZE + Renderer.WORDS_MARGIN_TOP
label, rectangle = self.text_objects(
words[i].word.upper(),
Renderer.WORD_COLOR,
(self.width / 2, top + Renderer.WORD_FONT_SIZE / 2)
)
if words[i] in valid_words and current_letter_count > 0:
count_of_letters = len(words[i].word)
new_rectangle = pygame.Rect(
rectangle.left,
rectangle.top,
rectangle.width * (current_letter_count / count_of_letters),
rectangle.height
)
pygame.draw.rect(self.screen, Renderer.WORD_HIGHLIGHT_COLOR, new_rectangle)
self.screen.blit(label, rectangle)
def create_bar(self, x, y, height, percentage):
health_bar_background_width = self.width - 2 * Renderer.BAR_MARGIN
health_bar_width = (health_bar_background_width - 2 * Renderer.BAR_BORDER_WIDTH) * percentage
health_bar_background = pygame.Rect(
x + Renderer.BAR_MARGIN,
y + Renderer.BAR_MARGIN,
health_bar_background_width,
height
)
health_bar = pygame.Rect(
x + Renderer.BAR_MARGIN + Renderer.BAR_BORDER_WIDTH,
y + Renderer.BAR_MARGIN + Renderer.BAR_BORDER_WIDTH,
health_bar_width,
height - 2 * Renderer.BAR_BORDER_WIDTH
)
return health_bar_background, health_bar
def render_win_screen(self):
self.screen.fill(Renderer.WIN_BACKGROUND_COLOR)
label, rectangle = self.text_objects(
Renderer.WIN_TEXT,
Renderer.WIN_TEXT_COLOR,
(self.width / 2, self.height / 2)
)
self.screen.blit(label, rectangle)
def render_loss_screen(self):
self.screen.fill(Renderer.LOSS_BACKGROUND_COLOR)
label, rectangle = self.text_objects(
Renderer.LOSS_TEXT,
Renderer.LOSS_TEXT_COLOR,
(self.width / 2, self.height / 2)
)
self.screen.blit(label, rectangle)