MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / runLevel

Function runLevel

src/gamesbyexample/pygame_games/starpusher.py:124–262  ·  view source on GitHub ↗
(levels, levelNum)

Source from the content-addressed store, hash-verified

122
123
124def runLevel(levels, levelNum):
125 global currentImage
126 levelObj = levels[levelNum]
127 mapObj = decorateMap(levelObj['mapObj'], levelObj['startState']['player'])
128 gameStateObj = copy.deepcopy(levelObj['startState'])
129 mapNeedsRedraw = True # set to True to call drawMap()
130 levelSurf = BASICFONT.render('Level %s of %s' % (levelNum + 1, len(levels)), 1, TEXTCOLOR)
131 levelRect = levelSurf.get_rect()
132 levelRect.bottomleft = (20, WINHEIGHT - 35)
133 mapWidth = len(mapObj) * TILEWIDTH
134 mapHeight = (len(mapObj[0]) - 1) * TILEFLOORHEIGHT + TILEHEIGHT
135 MAX_CAM_X_PAN = abs(HALF_WINHEIGHT - int(mapHeight / 2)) + TILEWIDTH
136 MAX_CAM_Y_PAN = abs(HALF_WINWIDTH - int(mapWidth / 2)) + TILEHEIGHT
137
138 levelIsComplete = False
139 # Track how much the camera has moved:
140 cameraOffsetX = 0
141 cameraOffsetY = 0
142 # Track if the keys to move the camera are being held down:
143 cameraUp = False
144 cameraDown = False
145 cameraLeft = False
146 cameraRight = False
147
148 while True: # main game loop
149 # Reset these variables:
150 playerMoveTo = None
151 keyPressed = False
152
153 for event in pygame.event.get(): # event handling loop
154 if event.type == QUIT:
155 # Player clicked the "X" at the corner of the window.
156 terminate()
157
158 elif event.type == KEYDOWN:
159 # Handle key presses
160 keyPressed = True
161 if event.key == K_LEFT:
162 playerMoveTo = LEFT
163 elif event.key == K_RIGHT:
164 playerMoveTo = RIGHT
165 elif event.key == K_UP:
166 playerMoveTo = UP
167 elif event.key == K_DOWN:
168 playerMoveTo = DOWN
169
170 # Set the camera move mode.
171 elif event.key == K_a:
172 cameraLeft = True
173 elif event.key == K_d:
174 cameraRight = True
175 elif event.key == K_w:
176 cameraUp = True
177 elif event.key == K_s:
178 cameraDown = True
179
180 elif event.key == K_n:
181 return 'next'

Callers 1

mainFunction · 0.85

Calls 5

decorateMapFunction · 0.85
isLevelFinishedFunction · 0.85
drawMapFunction · 0.85
terminateFunction · 0.70
makeMoveFunction · 0.70

Tested by

no test coverage detected