Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBase.py
More file actions
Latest commit
357 lines (273 loc) · 10.6 KB
/
Copy pathBase.py
File metadata and controls
357 lines (273 loc) · 10.6 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
importpygame
# Global constants
# Colors
BLACK= (0, 0, 0)
WHITE= (255, 255, 255)
GREEN= (0, 255, 0)
RED= (255, 0, 0)
BLUE= (0, 0, 255)
PORTAL= (255, 84 ,205)
INVISIBLE= (250,250,250)
# Screen dimensions
SCREEN_WIDTH=1366
SCREEN_HEIGHT=768
classPlayer(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player
controls. """
# -- Methods
def__init__(self):
""" Constructor function """
# Call the parent's constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
width=40
height=60
self.image=pygame.image.load("stick.png").convert()
self.image.set_colorkey(WHITE)
# Set a referance to the image rect.
self.rect=self.image.get_rect()
# Set speed vector of player
self.change_x=0
self.change_y=0
# List of sprites we can bump against
self.level=None
defupdate(self):
""" Move the player. """
# Gravity
self.calc_grav()
# Move left/right
self.rect.x+=self.change_x
# See if we hit anything
block_hit_list=pygame.sprite.spritecollide(self, self.level.platform_list, False)
forblockinblock_hit_list:
# If we are moving right,
# set our right side to the left side of the item we hit
ifself.change_x>0:
self.rect.right=block.rect.left
elifself.change_x<0:
# Otherwise if we are moving left, do the opposite.
self.rect.left=block.rect.right
enemy_block_hit_list=pygame.sprite.spritecollide(self, self.level.enemy_list, False)
forblockinenemy_block_hit_list:
self.rect.y=40
self.rect.x=40
# Move up/down
self.rect.y+=self.change_y
# Check and see if we hit anything
block_hit_list=pygame.sprite.spritecollide(self, self.level.platform_list, False)
forblockinblock_hit_list:
# Reset our position based on the top/bottom of the object.
ifself.change_y>0:
self.rect.bottom=block.rect.top
elifself.change_y<0:
self.rect.top=block.rect.bottom
# Stop our vertical movement
self.change_y=0
defcalc_grav(self):
""" Calculate effect of gravity. """
ifself.change_y==0:
self.change_y=1
else:
self.change_y+=.35
# See if we are on the ground.
ifself.rect.y>=SCREEN_HEIGHT-self.rect.heightandself.change_y>=0:
self.change_y=0
self.rect.y=40
self.rect.x=40
defjump(self):
""" Called when user hits 'jump' button. """
# move down a bit and see if there is a platform below us.
# Move down 2 pixels because it doesn't work well if we only move down
# 1 when working with a platform moving down.
self.rect.y+=2
platform_hit_list=pygame.sprite.spritecollide(self, self.level.platform_list, False)
self.rect.y-=2
# If it is ok to jump, set our speed upwards
iflen(platform_hit_list) >0orself.rect.bottom>=SCREEN_HEIGHT:
self.change_y=-10
# Player-controlled movement:
defgo_left(self):
""" Called when the user hits the left arrow. """
self.change_x=-6
defgo_right(self):
""" Called when the user hits the right arrow. """
self.change_x=6
defstop(self):
""" Called when the user lets off the keyboard. """
self.change_x=0
classPlatform(pygame.sprite.Sprite):
""" Platform the user can jump on """
def__init__(self, width, height):
""" Platform constructor. Assumes constructed with user passing in
an array of 5 numbers like what's defined at the top of this
code. """
super().__init__()
self.image=pygame.Surface([width, height])
self.image.fill(BLACK)
self.rect=self.image.get_rect()
classEnemy(pygame.sprite.Sprite):
""" ENEMY """
def__init__(self, width, height):
super().__init__()
self.image=pygame.Surface([width, height])
self.image.fill(RED)
self.rect=self.image.get_rect()
classLevel(object):
""" This is a generic super-class used to define a level.
Create a child class for each level with level-specific
info. """
def__init__(self, player):
""" Constructor. Pass in a handle to player. Needed for when moving platforms
collide with the player. """
self.platform_list=pygame.sprite.Group()
self.enemy_list=pygame.sprite.Group()
self.player=player
# Background image
self.background=pygame.image.load("towerwall.png").convert()
# Update everything on this level
defupdate(self):
""" Update everything in this level."""
self.platform_list.update()
self.enemy_list.update()
defdraw(self, screen):
""" Draw everything on this level. """
# Draw the background
screen.blit(self.background, [0, 0])
# Draw all the sprite lists that we have
self.platform_list.draw(screen)
self.enemy_list.draw(screen)
# Create platforms for the level
classLevel_01(Level):
""" Definition for level 1. """
def__init__(self, player):
""" Create level 1. """
# Call the parent constructor
Level.__init__(self, player)
# Array with width, height, x, and y of platform
level= [[210, 10, 0, 100],
[210, 10, 200, 600],
[210, 10, 590, 500],
[210, 10, 800, 350],
[210, 10, 1165, 200],
]
forplatforminlevel:
block=Platform(platform[0], platform[1])
block.rect.x=platform[2]
block.rect.y=platform[3]
block.player=self.player
self.platform_list.add(block)
level= [[15, 15, 0, 0],
[15, 15, 200, 585],
]
forplatforminlevel:
block=Enemy(platform[0], platform[1])
block.rect.x=platform[2]
block.rect.y=platform[3]
block.player=self.player
self.enemy_list.add(block)
classLevel_02(Level):
""" Definition for level 1. """
def__init__(self, player):
""" Create level 1. """
# Call the parent constructor
Level.__init__(self, player)
# Array with width, height, x, and y of platform
level= [[210, 10, 0, 650],
]
# Go through the array above and add platforms
forplatforminlevel:
block=Platform(platform[0], platform[1])
block.rect.x=platform[2]
block.rect.y=platform[3]
block.player=self.player
self.platform_list.add(block)
# Go through the array above and add platforms
defmain():
""" Main Program """
pygame.init()
# Set the height and width of the screen
size= [SCREEN_WIDTH, SCREEN_HEIGHT]
screen=pygame.display.set_mode(size, pygame.FULLSCREEN)
pygame.display.set_caption("Platformer Jumper")
# Create the player
player=Player()
# Create all the levels
level_list= []
level_list.append( Level_01(player) )
# Set the current level
current_level_no=0
current_level=level_list[current_level_no]
active_sprite_list=pygame.sprite.Group()
player.level=current_level
player.rect.x=40
player.rect.y=40
active_sprite_list.add(player)
# Loop until the user clicks the close button.
done=False
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
font=pygame.font.Font(None, 25)
frame_count=0
frame_rate=60
start_time=90
# -------- Main Program Loop -----------
whilenotdone:
foreventinpygame.event.get():
ifevent.type==pygame.KEYDOWN:
ifevent.key==pygame.K_ESCAPE:
done=True
ifevent.key==pygame.K_a:
player.go_left()
ifevent.key==pygame.K_d:
player.go_right()
ifevent.key==pygame.K_w:
player.jump()
ifevent.type==pygame.KEYUP:
ifevent.key==pygame.K_aandplayer.change_x<0:
player.stop()
ifevent.key==pygame.K_dandplayer.change_x>0:
player.stop()
ifplayer.rect.x>=1340andplayer.rect.y<=200:
level_list= []
level_list.append( Level_02(player) )
# Set the current level
current_level_no=0
current_level=level_list[current_level_no]
player.level=current_level
player.rect.x=20
player.rect.y=610
# Update the player.
active_sprite_list.update()
# Update items in the level
current_level.update()
# If the player gets near the right side, shift the world left (-x)
ifplayer.rect.right>SCREEN_WIDTH:
player.rect.right=SCREEN_WIDTH
# If the player gets near the left side, shift the world right (+x)
ifplayer.rect.left<0:
player.rect.left=0
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
current_level.draw(screen)
active_sprite_list.draw(screen)
total_seconds=frame_count//frame_rate
# Divide by 60 to get total minutes
minutes=total_seconds//60
# Use modulus (remainder) to get seconds
seconds=total_seconds%60
# Use python string formatting to format in leading zeros
output_string="Time: {0:02}:{1:02}".format(minutes, seconds)
# Blit to the screen
text=font.render(output_string, True, BLACK)
screen.blit(text, [15, 740])
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
frame_count+=1
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
if__name__=="__main__":
main()