-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrit_window.py
More file actions
77 lines (56 loc) · 2.2 KB
/
Copy pathrit_window.py
File metadata and controls
77 lines (56 loc) · 2.2 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
import pygame
import signal
class RitWindow:
def __init__(self, width, height, title="CSCI 610"):
# window-related info
self.width = width
self.height = height
self.title = title
self.started = False
self.screen = None
def doRun(self, action):
if self.started == False:
pygame.init()
# create window
self.screen = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption(self.title)
self.started = True
clock = pygame.time.Clock()
while self.started:
# --- Event handling ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.started = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.started = False
# --- Call your per-frame callback ---
action(self)
# --- Update display ---
pygame.display.flip()
# --- Limit to ~60 FPS (optional) ---
clock.tick(30)
pygame.quit()
def clearFB (self, r, g, b):
for x in range(self.width):
for y in range(self.height):
self.set_pixel(x, y, r, g, b)
def drawRect (self, top, bottom, right, left, r, g, b):
#top and bottom edges
for i in range (left, right):
self.set_pixel (i, top, r, g, b)
self.set_pixel (i, bottom, r, g, b)
# left and right
for j in range (bottom, top):
self.set_pixel (left, j, r, g, b)
self.set_pixel (right, j, r, g, b)
def set_pixel(self, x, y, r, g, b):
if (x <0 or y <0 or x >= self.width or y >= self.height):
print ("set_pixel error: pixel [", x, ",", y, "] is out of range")
else:
# sets the pixel at (x,y) to (r, g, b, 255), with 8-bit integer colors
rr = int (r *255)
gg = int (g * 255)
bb = int (b * 255)
c = (rr,gg,bb)
self.screen.set_at((x, (self.height-1) - y), c)