-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygame_structure.py
209 lines (170 loc) · 6.68 KB
/
pygame_structure.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
##########################################################################
# Author: Griffin Della Grotte (gdellagr@andrew.cmu.edu)
#
# This module is the first layer of abstraction I have implemented on top
# of PyGame. It creates a 'program' structure where by different 'modes'
# can be called upon, taking control of the window and inputs. MapCMU
# utilizes this system for its different screens.
##########################################################################
import threading
import os, sys, time
import pygame
import color_constants
from mapcmu_data import *
if not pygame.font: print ('Warning, fonts disabled')
if not pygame.mixer: print ('Warning, sound disabled')
class MainPygame(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
pygame.init()
pygame.display.set_caption("MapCMU")
self.lock = threading.RLock()
self.alive = True
self.modes = {}
self.modes['default'] = BootingMode()
self.currentMode = self.modes['default']
def __sleepFPS(self, fps, start, end):
delta = end - start
target = 1.0/fps
time.sleep(target-delta if target-delta > 0 else 0)
def terminate(self):
self.alive = False
def run(self):
# Note the locked setup for this class. As it is being accessed by
# multiple threads, we have to make sure there are no access collisions
self.lock.acquire()
try:
self.screen = pygame.display.set_mode(self.currentMode.screenDims)
finally:
self.lock.release()
self.__mainloop()
def __mainloop(self):
while self.alive:
if (isinstance(self.currentMode, PygameMode)):
start = time.time()
self.lock.acquire()
try:
#info = pygame.display.Info()
#if (info.current_h != self.dimQueue[1] or
# info.current_w != self.dimQueue[0]):
# self.screen = pygame.display.set_mode(self.dimQueue,
# pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE)
self.currentMode.drawView(self.screen)
self.currentMode.handleEvents(pygame.event.get())
pygame.display.flip()
finally:
self.lock.release()
end = time.time()
self.__sleepFPS(self.currentMode.fps, start,end)
else:
time.sleep(.05)
def addMode(self, pygameMode, name):
self.modes[name] = pygameMode
def useMode(self, name):
self.lock.acquire()
try:
self.currentMode = self.modes[name]
self.dimQueue = self.currentMode.screenDims
self.screen = pygame.display.set_mode(self.currentMode.screenDims)
finally:
self.lock.release()
def getMode(self, name):
return self.modes[name]
def getPygame(self):
return pygame
class PygameMode():
def __init__(self, screenDims=(640,400), fps=60,
bkcolor=(100,100,100), changeModeFn=None):
threading.Thread.__init__(self)
self.fps = fps
self.bkcolor = bkcolor
self.screenDims = screenDims
self.changeModeFn = changeModeFn
self.initView()
def initView(self):
self.surfaces = []
background = pygame.Surface(self.screenDims)
background.fill(self.bkcolor)
self.surfaces.append(background)
def drawView(self, screen):
for surface in self.surfaces:
#print("Mode: %s Surface: %s" % (self.currentMode, surfaceKey))
if (isinstance(surface, pygame.Surface)):
surface.convert()
screen.blit(surface, (0,0))
elif (isinstance(surface, SurfacePlus)):
surface.surf.convert()
screen.blit(surface.surf, (0,0)) #(surface.offset[0],surface.offset[1]))
def mousePressed(self, event):
# Override this!
pass
def keyPressed(self, event):
# Override this! (and call super because you should be able to exit
# from any mode)
ctrl = pygame.key.get_mods() & pygame.KMOD_CTRL
if (event.key == pygame.K_c and ctrl):
quit()
def terminate(self):
quit()
def handleEvents(self, events):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
self.mousePressed(event)
if event.type == pygame.KEYDOWN:
self.keyPressed(event)
if event.type == pygame.QUIT: # Red X window button
quit()
def addSurface(self, surface):
self.surfaces.append(surface)
def removeSurface(self, surface):
self.surfaces.remove(surface)
def getSurface(self, i):
return self.surfaces[i]
class PygameObject(object):
# Objects that can be added to a SurfacePlus and thus be drawn and
# take user input
def __init__(self, pos=(0,0), mToV=lambda *x:x[0], vToM=lambda *x:x[0]):
self.mToV = mToV
self.vToM = vToM
self.objects = []
self.pos = pos
self.initData()
pass
def initData(self):
pass
'''Override'''
def draw(self, surface, dims, offset=(0,0)):
for obj in self.objects:
obj.draw(surface, dims, offset)
'''Override'''
def mousePressed(self, surface, x, y):
for obj in self.objects:
obj.mousePressed(surface, x, y)
class SurfacePlus(object):
# Container for surface things, such as an actual surface and
# PygameObjects
def __init__(self):
self.surf = None
self.name = ""
self.objects = []
def drawObjects(self, offset=(0,0)):
for obj in self.objects:
obj.draw(self, self.surf.get_size(), offset)
def mouseObjects(self, event, offset):
for obj in self.objects:
obj.mousePressed(self, event.pos[0],
event.pos[1])
class BootingMode(PygameMode):
# Startup splashscreen mode, just draw an image
def __init__(self, screenDims=(1000,800), bkcolor=(200,200,200)):
PygameMode.__init__(self,screenDims=screenDims,bkcolor=bkcolor)
self.mainSurf = SurfacePlus()
self.mainSurf.surf = pygame.Surface(self.screenDims)
self.surfaces.append(self.mainSurf)
self.map2image = pygame.image.load("splash.jpg")
self.map2Rect = self.map2image.get_rect()
def drawView(self, screen):
self.mainSurf.surf.blit(self.map2image,
pygame.Rect(0, 0,
self.map2Rect.w, self.map2Rect.h))
super().drawView(screen)