-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.py
More file actions
53 lines (41 loc) · 1.63 KB
/
Copy pathcanvas.py
File metadata and controls
53 lines (41 loc) · 1.63 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
import pygame
BLACK = (0,0,0)
WHITE = (255,255,255)
class Canvas():
"""Represents a window with a certain WIDTH and HEIGHT.
Wraps pygame and offers primitives for drawing in that area.
If WIDTH or HEIGHT changes, don't change object members, but
create a new Canvas object altogether.
"""
def __init__(self, width, height):
# init pygame
pygame.init()
self._game_display = pygame.display.set_mode((width,height))
# init drawing surface
self.width = width
self.height = height
self._sf = pygame.Surface((width, height))
self.fill()
def set_title(self, title):
"Set the window title."
pygame.display.set_caption(title)
def update(self):
"Update the display."
self._game_display.blit(self._sf, (0,0))
pygame.display.update()
def pixel(self, pos, color=BLACK):
"Color one pixel."
self._sf.set_at(pos, color)
def rectangle(self, pos_from, pos_to, color=BLACK):
"Color a rectangle starting at POS_FROM and ending at POS_TO."
rect = pygame.Rect(pos_from, (abs(pos_to[0] - pos_from[0]),
abs(pos_to[1] - pos_from[1])))
self._sf.fill(color, rect)
def fill(self, color=WHITE):
"Fill the entire canvas with one color."
self._sf.fill(color)
def square(self, pos, size, color=BLACK):
"Color a square at POS with a size of SIZE x SIZE pixel."
pos_from = (pos[0] - size / 2, pos[1] - size / 2)
pos_to = (pos[0] + size / 2, pos[1] + size / 2)
self.rectangle(pos_from, pos_to, color=color)