-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_classes.py
122 lines (95 loc) · 3.36 KB
/
base_classes.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
import pygame
from enum import Enum
from os import path
import settings
from abc import ABC, abstractmethod
class Direction(Enum):
stop = 0
left = -1
right = 1
'''Описания классов вывода текста'''
class Text():
def __init__(self, screen, font_name,
font_size, font_color, alpha=255) -> None:
self.font = pygame.font.SysFont(font_name, font_size)
self.alpha = alpha
self.color = font_color
self.screen = screen
self.current_string = 0
def print(self, text, x, y):
text_surf = self.font.render(text, True, self.color)
text_surf.set_alpha(self.alpha)
self.screen.blit(text_surf, text_surf.get_rect(topleft=(x, y)))
class TextScreenInfo(Text):
def __init__(self, screen, x, y) -> None:
self.font_name = 'Arial'
self.font_size = 12
self.font_color = (255, 255, 255)
self.x = x
self.y = y
self.alpha = 127
self.color = settings.WHITE
self.screen = screen
self.current_string = 0
super().__init__(screen, self.font_name, self.font_size,
self.font_color, self.alpha)
def print(self, text):
y_string = (self.font_size + 2) * self.current_string
super().print(text, self.x, self.y + y_string)
self.current_string += 1
def reset(self):
self.current_string = 0
'''Описание базовых классов подводношо мира'''
class UnderwaterSubject(pygame.sprite.Sprite):
def __init__(self, x, y, sprites_group, screen):
pygame.sprite.Sprite.__init__(self)
self.view_info = False
self.lives = 100 # Жизни в процентах 0-100
self.x = x
self.y = y
self.set_sprite()
self.text = Text(screen, 'Arial', 10, settings.WHITE)
sprites_group.add(self)
def show_info(self, view_info: bool):
if not view_info:
return
x, y = self.rect.topright[0], self.rect.topright[1]
height_string = 10
self.text.print(f'жизни: {self.lives}', x, y)
self.text.print(f'x: {self.rect.x}, y: {self.rect.y}',
x, y + height_string)
def update(self):
if self.lives <= 0:
self.kill()
keystate = pygame.key.get_pressed()
if keystate[pygame.K_1]:
self.view_info = True
elif keystate[pygame.K_2]:
self.view_info = False
self.show_info(self.view_info)
def apply_sprite(self, file_name: str):
self.image = pygame.image.load(path.join(settings.img_dir,
file_name)).convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
def set_sprite(self):
self.apply_sprite('underwater_subject.png')
class Group():
def __init__(self) -> None:
self.list = list()
def update(self):
for obj in self.list:
obj.update()
def add(self, obj):
self.list.append(obj)
def remove(self, obj):
try:
self.list.remove(obj)
except ValueError:
print(ValueError)
raise Exception("ERROR: Attempt to remove a missing element from a group") # noqa: E501
class Generator(ABC):
@abstractmethod
def update():
pass