-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_window_example.py
More file actions
139 lines (111 loc) · 4.35 KB
/
render_window_example.py
File metadata and controls
139 lines (111 loc) · 4.35 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
"""
Example demonstrating RenderWindow usage in the Patchwork context.
This shows how to refactor main.py to use RenderWindow instead of raw Pygame calls.
"""
import random
import pygame
from render_window import RenderWindow
from graphik import Graphik
# Example 1: Simple usage replacing raw Pygame initialization
def simple_example():
"""Demonstrates basic RenderWindow usage"""
# Instead of:
# pygame.init()
# gameDisplay = pygame.display.set_mode((800, 800))
# pygame.display.set_caption("Title")
# Use RenderWindow:
window = RenderWindow("Simple Example", 800, 800)
surface = window.get_surface()
# Create Graphik instance with the surface
graphik = Graphik(surface)
# Main loop - instead of manual event handling
while window.should_continue():
surface.fill((255, 255, 255))
graphik.drawText("RenderWindow Example", 400, 400, 30, (0, 0, 0))
pygame.display.update()
window.tick(60)
pygame.quit()
# Example 2: With custom event handlers
def event_handler_example():
"""Demonstrates custom event handler registration"""
window = RenderWindow("Event Handler Example", 800, 800)
surface = window.get_surface()
graphik = Graphik(surface)
# State
click_count = [0]
# Custom event handler
def handle_mouse_click(event):
if event.type == pygame.MOUSEBUTTONDOWN:
click_count[0] += 1
print(f"Mouse clicked! Total clicks: {click_count[0]}")
# Register the handler
window.register_event_handler(handle_mouse_click)
# Main loop
while window.should_continue():
surface.fill((255, 255, 255))
graphik.drawText(f"Clicks: {click_count[0]}", 400, 400, 30, (0, 0, 0))
graphik.drawText("Click anywhere or close window", 400, 450, 20, (100, 100, 100))
pygame.display.update()
window.tick(60)
pygame.quit()
# Example 3: Integration pattern for main.py
def integration_example():
"""
Shows how main.py could be refactored to use RenderWindow.
The refactored main() function would replace:
pygame.init()
gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
pygame.display.set_caption("Visualizing Environment With Random Colors")
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
With:
window = RenderWindow("Visualizing Environment With Random Colors", displayWidth, displayHeight)
surface = window.get_surface()
graphik = Graphik(surface)
while window.should_continue():
# ... existing drawing code ...
window.tick(60) # Optional: add frame rate limiting
"""
displayWidth = 800
displayHeight = 800
# Create window using RenderWindow
window = RenderWindow("Visualizing Environment With Random Colors", displayWidth, displayHeight)
surface = window.get_surface()
graphik = Graphik(surface)
# Example: drawing random rectangles (simulating environment visualization)
white = (255, 255, 255)
while window.should_continue():
surface.fill(white)
# Draw some example content
for i in range(10):
for j in range(10):
red = random.randrange(50, 200)
green = random.randrange(50, 200)
blue = random.randrange(50, 200)
x = i * (displayWidth / 10)
y = j * (displayHeight / 10)
graphik.drawRectangle(x, y, displayWidth / 10, displayHeight / 10, (red, green, blue))
pygame.display.update()
window.tick(60) # Limit to 60 FPS
pygame.quit()
if __name__ == "__main__":
print("RenderWindow Usage Examples")
print("=" * 60)
print("1. Simple example")
print("2. Event handler example")
print("3. Integration example (simulates main.py refactoring)")
print()
choice = input("Select example (1-3): ").strip()
if choice == "1":
simple_example()
elif choice == "2":
event_handler_example()
elif choice == "3":
integration_example()
else:
print("Invalid choice. Running integration example by default.")
integration_example()