-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.py
More file actions
52 lines (40 loc) · 2.24 KB
/
Copy pathgraphics.py
File metadata and controls
52 lines (40 loc) · 2.24 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
from __future__ import annotations
import pygame
from typing import Tuple, TYPE_CHECKING
if TYPE_CHECKING:
from pygame.surface import Surface
class GraphicsArea:
def __init__(self, screen: Surface, top_left: Tuple[int, int], pixel_width: int, pixel_height: int,
real_width: float = None, real_height: float = None):
self.screen = screen
self.top_left = top_left
self.width = pixel_width
self.real_width = real_width if real_width else pixel_width
self.height = pixel_height
self.real_height = real_height if real_height else pixel_height
def draw_circle(self, color, center, radius, width=0):
if center[0] > self.width or center[1] > self.height:
raise ValueError("Invalid circle coordinate")
pygame.draw.circle(self.screen, color, self.__convert_point(center), self.__convert_dimensions(radius, 0)[0],
width)
def draw_rect(self, color, rect):
left, top, width, height = rect
if left > self.width or top > self.height:
raise ValueError("Invalid rectangle coordinate")
new_left, new_top = self.__convert_point((left, top))
new_width, new_height = self.__convert_dimensions(width, height)
pygame.draw.rect(self.screen, color, (new_left, new_top-new_height, new_width, new_height))
def fill(self, color):
pygame.draw.rect(self.screen, color, (self.top_left[0], self.top_left[1], self.width, self.height))
def draw_text(self, text: str, pos, size, color):
font = pygame.font.Font(pygame.font.get_default_font(), size)
self.screen.blit(font.render(text, True, color), self.__convert_point(pos))
def draw_line(self, color, start_pos, end_pos, width=1):
pygame.draw.line(self.screen, color, self.__convert_point(start_pos), self.__convert_point(end_pos), width)
def __convert_point(self, point):
x, y = point
x = (x / self.real_width) * self.width
y = (y / self.real_height) * self.height
return int(self.top_left[0] + x), int(self.top_left[1] + (self.height - y))
def __convert_dimensions(self, width, height):
return int((width / self.real_width) * self.width), int((height / self.real_height) * self.height)