-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.py
More file actions
109 lines (82 loc) · 3.01 KB
/
graphics.py
File metadata and controls
109 lines (82 loc) · 3.01 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
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
import pygame
from typing import Tuple
import pygame.gfxdraw
class Point:
def __init__(self, x: int, y: int) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
return f"Point({self.x}, {self.y})"
def xy(self) -> Tuple[int, int]:
return self.x, self.y
class Line:
def __init__(self, p1: Point, p2: Point) -> None:
self.p1 = p1
self.p2 = p2
def __repr__(self) -> str:
return f"Line({self.p1}, {self.p2})"
def draw(
self, surface: pygame.Surface, fill_color: str, width: int, aa: bool
) -> None:
if aa:
pygame.draw.aaline(
surface, pygame.Color(fill_color), self.p1.xy(), self.p2.xy(), 1
)
return
pygame.draw.line(
surface, pygame.Color(fill_color), self.p1.xy(), self.p2.xy(), width
)
class Text:
def __init__(self, text: str, p1: Point) -> None:
self.p1 = p1
self.text = text
def __repr__(self) -> str:
return f"Text('{self.text}', {self.p1})"
def draw(self, surface: pygame.Surface) -> None:
font = pygame.font.Font(None, 24)
text_surface = font.render(self.text, True, pygame.Color("black"))
surface.blit(text_surface, self.p1.xy())
class Window:
def __init__(self, width: int, height: int) -> None:
self.width = width
self.height = height
pygame.init()
self.__screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("hexgen")
self.__background = pygame.Surface((width, height))
self.__background.fill(pygame.Color("#3199b7"))
self.__screen.blit(self.__background, (0, 0))
self.__running = False
self.__clock = pygame.time.Clock()
self.tickspeed = 60 # Default tickspeed
def clear(self) -> None:
self.__screen.blit(self.__background, (0, 0))
self.redraw()
def center(self) -> Tuple[int, int]:
return self.width // 2, self.height // 2
def redraw(self) -> None:
pygame.display.flip()
self.__clock.tick(self.tickspeed) # Use the current tickspeed
self._handle_events()
def draw(self, line: Line, fill_color: str = "black", width=2, aa=False) -> None:
line.draw(self.__screen, fill_color, width, aa)
self.redraw()
def draw_text(self, text: Text) -> None:
text.draw(self.__screen)
self.redraw()
def _handle_events(self) -> None:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.__running = False
def wait_for_close(self) -> None:
self.__running = True
print("window opened")
while self.__running:
self._handle_events()
self.__clock.tick(60) # Keep this at 60 FPS for smooth event handling
print("window closed")
pygame.quit()
def close(self) -> None:
self.__running = False
def set_tickspeed(self, speed: int) -> None:
self.tickspeed = speed