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

Function runGame

src/gamesbyexample/pygame_games/squirrel.py:85–312  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

83
84
85def runGame():
86 # set up variables for the start of a new game
87 invulnerableMode = False # if the player is invulnerable
88 invulnerableStartTime = 0 # time the player became invulnerable
89 gameOverMode = False # if the player has lost
90 gameOverStartTime = 0 # time the player lost
91 winMode = False # if the player has won
92
93 # create the surfaces to hold game text
94 gameOverSurf = BASICFONT.render('Game Over', True, WHITE)
95 gameOverRect = gameOverSurf.get_rect()
96 gameOverRect.center = (HALF_WINWIDTH, HALF_WINHEIGHT)
97
98 winSurf = BASICFONT.render('You have achieved OMEGA SQUIRREL!', True, WHITE)
99 winRect = winSurf.get_rect()
100 winRect.center = (HALF_WINWIDTH, HALF_WINHEIGHT)
101
102 winSurf2 = BASICFONT.render('(Press "r" to restart.)', True, WHITE)
103 winRect2 = winSurf2.get_rect()
104 winRect2.center = (HALF_WINWIDTH, HALF_WINHEIGHT + 30)
105
106 # camerax and cameray are the top left of where the camera view is
107 camerax = 0
108 cameray = 0
109
110 grassObjs = [] # stores all the grass objects in the game
111 squirrelObjs = [] # stores all the non-player squirrel objects
112 # stores the player object:
113 playerObj = {'surface': pygame.transform.scale(L_SQUIR_IMG, (STARTSIZE, STARTSIZE)),
114 'facing': LEFT,
115 'size': STARTSIZE,
116 'x': HALF_WINWIDTH,
117 'y': HALF_WINHEIGHT,
118 'bounce':0,
119 'health': MAXHEALTH}
120
121 moveLeft = False
122 moveRight = False
123 moveUp = False
124 moveDown = False
125
126 # start off with some random grass images on the screen
127 for i in range(10):
128 grassObjs.append(makeNewGrass(camerax, cameray))
129 grassObjs[i]['x'] = random.randint(0, WINWIDTH)
130 grassObjs[i]['y'] = random.randint(0, WINHEIGHT)
131
132 while True: # main game loop
133 # Check if we should turn off invulnerability
134 if invulnerableMode and time.time() - invulnerableStartTime > INVULNTIME:
135 invulnerableMode = False
136
137 # move all the squirrels
138 for sObj in squirrelObjs:
139 # move the squirrel, and adjust for their bounce
140 sObj['x'] += sObj['movex']
141 sObj['y'] += sObj['movey']
142 sObj['bounce'] += 1

Callers 1

mainFunction · 0.70

Calls 7

makeNewGrassFunction · 0.85
getRandomVelocityFunction · 0.85
isOutsideActiveAreaFunction · 0.85
makeNewSquirrelFunction · 0.85
getBounceAmountFunction · 0.85
drawHealthMeterFunction · 0.85
terminateFunction · 0.70

Tested by

no test coverage detected