-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.py
67 lines (49 loc) · 2.72 KB
/
services.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
import pygame
from utils import Position
class FontService:
def __init__(self, filename: str, font_size: int):
self.__font = pygame.font.Font(filename, font_size)
self.__font_size = font_size
def draw_text(self, text: str, surface: pygame.Surface, position: Position, color: pygame.Color) -> None:
text_surface = self.__font.render(text, True, color)
surface.blit(text_surface, (position.x, position.y))
def draw_text_at_center(self, text: str, surface: pygame.Surface, color: pygame.Color) -> None:
surface_width, surface_height = surface.get_size()
text_surface = self.__font.render(text, True, color)
text_surface_height = text_surface.get_size()[1]
text_rect = text_surface.get_rect()
text_rect.midtop = (surface_width // 2, surface_height // 2 - text_surface_height)
surface.blit(text_surface, text_rect)
def draw_text_at_bottom_center(self, text: str, surface: pygame.Surface, color: pygame.Color) -> None:
surface_width, surface_height = surface.get_size()
text_surface = self.__font.render(text, True, color)
text_surface_height = text_surface.get_size()[1]
text_rect = text_surface.get_rect()
text_rect.midtop = (surface_width // 2, surface_height - text_surface_height)
surface.blit(text_surface, text_rect)
def get_font(self):
return self.__font
class SmallFontService(FontService):
def __init__(self, filename: str = "./fonts/KidpixiesRegular.ttf"):
super().__init__(filename, font_size=18)
class LargeFontService(FontService):
def __init__(self, filename: str = "./fonts/KidpixiesRegular.ttf"):
super().__init__(filename, font_size=44)
class GameSoundService:
def __init__(self) -> None:
self.fruit_eaten_sound_channel = pygame.mixer.Channel(0)
self.fruit_eaten_sound_channel.set_volume(0.9)
self.main_channel = pygame.mixer.Channel(1)
self.main_channel.set_volume(0.6)
self.background_music_sound = pygame.mixer.Sound("./sounds/background.ogg")
self.fruit_eaten_sound = pygame.mixer.Sound("./sounds/fruit_eaten.wav")
self.game_over_sound = pygame.mixer.Sound("./sounds/game_over.wav")
self.snake_hissing_sound = pygame.mixer.Sound("./sounds/snake-hissing-sound.mp3")
def play_background_music(self) -> None:
self.main_channel.play(self.background_music_sound, loops=-1)
def play_fruit_eaten_sound(self) -> None:
self.fruit_eaten_sound_channel.play(self.fruit_eaten_sound)
def play_game_over_sound(self) -> None:
self.main_channel.play(self.game_over_sound)
def play_snake_hissing_sound(self) -> None:
self.main_channel.play(self.snake_hissing_sound, loops=-1)