-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprites.py
More file actions
76 lines (53 loc) · 2.68 KB
/
sprites.py
File metadata and controls
76 lines (53 loc) · 2.68 KB
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
import pygame
from settings import *
class Tile:
def __init__(self, x, y, letter="", colour=None):
self.x, self.y = x, y
self.letter = letter
self.colour = colour
self.width, self.height = TILESIZE, TILESIZE
self.font_size = int(60 * (TILESIZE/100))
self.create_font()
def create_font(self):
font = pygame.font.SysFont("Consolas", self.font_size)
self.render_letter = font.render(self.letter, True, WHITE)
self.font_width, self.font_height = font.size(self.letter)
def draw(self, screen):
if self.colour is None:
pygame.draw.rect(screen, WHITE, (self.x, self.y, self.width, self.height), 2)
else:
pygame.draw.rect(screen, self.colour, (self.x, self.y, self.width, self.height))
if self.letter != "":
self.font_x = self.x + (self.width / 2) - (self.font_width / 2)
self.font_y = self.y + (self.height / 2) - (self.font_height / 2)
letter = pygame.transform.scale(self.render_letter, (self.font_width, self.font_height))
screen.blit(letter, (self.font_x, self.font_y))
class UIElement:
def __init__(self, x, y, text, colour, font_size=40):
self.x, self.y = x, y
self.text = text
self.colour = colour
self.font_size = font_size
self.alpha = 0
self.create_font()
def create_font(self):
font = pygame.font.SysFont("Consolas", self.font_size)
self.original_surface = font.render(self.text, True, self.colour)
self.text_surface = self.original_surface.copy()
# this surface is used to adjust the alpha of the text_surface
self.alpha_surface = pygame.Surface(self.text_surface.get_size(), pygame.SRCALPHA)
def draw(self, screen):
self.text_surface = self.original_surface.copy()
self.alpha_surface.fill((255, 255, 255, self.alpha))
self.text_surface.blit(self.alpha_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
screen.blit(self.text_surface, (self.x, self.y))
def fade_out(self):
self.alpha = max(self.alpha - 10, 0)
self.text_surface = self.original_surface.copy()
self.alpha_surface.fill((255, 255, 255, self.alpha))
self.text_surface.blit(self.alpha_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
def fade_in(self):
self.alpha = min(self.alpha + 10, 255)
self.text_surface = self.original_surface.copy()
self.alpha_surface.fill((255, 255, 255, self.alpha))
self.text_surface.blit(self.alpha_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)