-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygame_gui_app.py
118 lines (93 loc) · 4.01 KB
/
pygame_gui_app.py
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
# TODO:
# A. Placeholder
# Library Imports
import pygame
from timeit import default_timer
# Local Imports
import nsvt_config as config
class PygameGuiApp():
def __init__(self, wrapper_, gIndex_=0):
# Initialization method for PyGame library
pygame.init()
# TODO: Implement the following pygame methods
# A. pygame.pixelcopy
# 1. pygame.pixelcopy.make_surface(<array>) -> <surface>
# a. Should be used to create surfaces but not necessarily to update them, see pygame.surfarray
# 2. pygame.pixelcopy.array_to_surface(<surface>, <array>) -> None
# B. pygame.surfarray
# 1. pygame.surfarray.blit_array(<surface>, <array>) -> None
# a. FASTER than converting array to a surface and then blitting
# MEMBER VARIABLE DEFINITIONS
# Graphics Engine Index
self.gIndex = gIndex_
# References to external objects
self.wrapper = wrapper_
# PyGame Parameters
self.display_width = config.DEFAULT_GUI_WIDTH
self.display_height = config.DEFAULT_GUI_HEIGHT
self.gameDisplay = pygame.display.set_mode((self.display_width, self.display_height))
pygame.display.set_caption('PyGame App Test')
# App Control
self.frameTime = default_timer()
self.clock = pygame.time.Clock()
self.crashed = False
self.pgSurface = pygame.pixelcopy.make_surface(self.wrapper.gEngines[-1].numpyImage)
# Run the Pygame GUI App
self.runApp()
def runApp(self):
fpsVals = []
while not self.crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.crashed = True
# These lines track the amount of time that has elapsed since the last self.after() call
self.timeDelta = default_timer() - self.frameTime
self.frameTime = default_timer()
self.currentFPS = int(round(1 / self.timeDelta))
fpsVals.append(self.currentFPS)
# Updates the graphics
self.advance()
# REFERENCE: Fill the display with white (UNUSED)
#self.gameDisplay.fill(self.white)
# REFERENCE: Wait for 60 milliseconds (UNUSED)
#self.clock.tick(60)
pygame.quit()
print(fpsVals)
quit()
def advance(self):
# Updates the graphics
self.drawGraphicsPyGame()
# Computes the upcoming frame
self.computeNextFramePyGame()
def stopAnimating(self):
self.animating = False
def startAnimating(self):
self.animating = True
self.frameTime = default_timer()
self.animate()
def animate(self):
while not self.nextFrameComputed:
self.after(10)
if self.animating:
# NOTE: self.after() call of self.animate() should be the FIRST ACTION in this method
self.after(self.msPerFrame, self.animate)
# These lines track the amount of time that has elapsed since the last self.after() call
self.timeDelta = default_timer() - self.frameTime
self.frameTime = default_timer()
self.currentFPS = int(round(1 / self.timeDelta))
# Updates the graphics
self.drawGraphics()
# Computes the upcoming frame
self.computeNextFrame()
def drawGraphicsPyGame(self):
# REFRESH AND DISPLAY:
# Update self.pgSurface to match the new Numpy image
pygame.surfarray.blit_array(self.pgSurface, self.wrapper.gEngines[self.gIndex].numpyImage)
# Blit the image to self.gameDisplay
self.gameDisplay.blit(self.pgSurface, (0, 0))
# Update the display
pygame.display.update()
def computeNextFramePyGame(self):
# Compute a new frame to prepare for future display
self.wrapper.gEngines[self.gIndex].numpyImage = self.wrapper.gEngines[self.gIndex].manipulateImageNumba(self.wrapper.gEngines[self.gIndex].numpyImage)
self.nextFrameComputed = True