1
+ import pygame
2
+ from sys import exit
3
+
4
+
5
+ #Player running animations
6
+ def player_physics ():
7
+ global play_surf , player_running_index , player_jumping_index
8
+
9
+ if play_rect .bottom >= 200 :
10
+ play_surf = player_jumping
11
+ player_jumping_index += 0.5
12
+ if player_jumping_index >= len (player_jumping ):
13
+ player_jumping_index = 0
14
+ play_surf = player_jumping [int (player_jumping_index )]
15
+
16
+ player_running_index += 0.15
17
+ if player_running_index >= len (player_running ):
18
+ player_running_index = 0
19
+ play_surf = player_running [int (player_running_index )]
20
+
21
+
22
+ #Starts pygame and initializes all sub parts
23
+ pygame .init ()
24
+
25
+ background_music = pygame .mixer .Sound ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/music.wav' )
26
+ background_music .set_volume (0.2 )
27
+ background_music .play (- 1 )
28
+ jump_sound = pygame .mixer .Sound ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/mixkit-player-jumping-in-a-video-game-2043.wav' )
29
+ jump_sound .set_volume (0.1 )
30
+
31
+ #Display surface
32
+ #Taking the dimensions of the screen (x-axis = 384, and y-axis = 216) and storing them in a tuple
33
+ screen = pygame .display .set_mode ((384 , 216 ))
34
+
35
+
36
+ #Title that you'll see in the top left portion
37
+ pygame .display .set_caption ("S2, Ep18, (8:31), Off" )
38
+
39
+
40
+ #Framerate
41
+ clock = pygame .time .Clock ()
42
+
43
+
44
+ #Font (Type and size)
45
+ font = pygame .font .Font ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/Pixeltype (1).ttf' , 15 )
46
+
47
+
48
+ #Regular Background surface. '''.convert()''' what this is doing is - Instead of python processing the imaga pixal by pixal, it uses a format that grabs the
49
+ back_drop_surf = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/parallax background/plx-1.png' ).convert_alpha ()
50
+ tree_drop_surf_1 = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/parallax background/plx-2.png' ).convert_alpha ()
51
+ tree_drop_surf_2 = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/parallax background/plx-3.png' ).convert_alpha ()
52
+ tree_drop_surf_3 = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/parallax background/plx-4.png' ).convert_alpha ()
53
+ tree_drop_surf_4 = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/parallax background/plx-5.png' ).convert_alpha ()
54
+
55
+
56
+ #Regular ground surface. The '''fill('grey')''' is taking the surface and filling it with a pre-defined-color
57
+ grnd_drop_surf = pygame .Surface ((384 , 216 ))
58
+ grnd_drop_surf .fill ('grey' )
59
+ grnd_drop_surf_2 = pygame .Surface ((384 , 216 ))
60
+ grnd_drop_surf_2 .fill ('gray55' )
61
+
62
+
63
+ #Regular coin surface. '''midbottom''' lets you choose where you would like to place your 'rect' at 9 different points
64
+ coin_surf = pygame .image .load ("C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/parallax background/coin_01a.png" ).convert_alpha ()
65
+ coin_rect = coin_surf .get_rect (midbottom = (300 , 100 ))
66
+
67
+
68
+ #Regular player surface + physics
69
+ play_run_1 = pygame .image .load ("C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-run-01.png" ).convert_alpha ()
70
+ play_run_2 = pygame .image .load ("C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-run-02.png" ).convert_alpha ()
71
+ play_run_3 = pygame .image .load ("C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-run-03.png" ).convert_alpha ()
72
+ play_run_4 = pygame .image .load ("C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-run-04.png" ).convert_alpha ()
73
+ play_run_5 = pygame .image .load ("C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-run-05.png" ).convert_alpha ()
74
+ player_running = [play_run_1 , play_run_2 , play_run_3 , play_run_4 , play_run_5 ]
75
+ player_running_index = 0
76
+ play_surf = player_running [player_running_index ]
77
+
78
+ play_jump_1 = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-jump-00.png' ).convert_alpha ()
79
+ play_jump_2 = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-jump-01.png' ).convert_alpha ()
80
+ play_jump_3 = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-jump-02.png' ).convert_alpha ()
81
+ play_jump_4 = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-jump-03.png' ).convert_alpha ()
82
+ player_jumping = [play_jump_1 , play_jump_2 , play_jump_3 , play_jump_4 ]
83
+ player_jumping_index = 0
84
+ play_rect = play_surf .get_rect (midbottom = (90 , 200 ))
85
+
86
+ #Regular enemy surface + physics
87
+ en_surf = pygame .image .load ("C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/Troll_Walk_1.png" ).convert_alpha ()
88
+ en_surf = pygame .transform .flip (en_surf , True , False )
89
+ en_surf = pygame .transform .scale (en_surf , (20 , 30 ))
90
+ en_rect = en_surf .get_rect (midbottom = (300 , 200 ))
91
+
92
+
93
+ #Text (text, anti aliasing, pre-defined-color - (IF wanted, instead of 'black' I could put (0,0,0) in place of it))
94
+ title_surf = font .render ('Mini-capstone' , False , 'black' )
95
+ title_rect = title_surf .get_rect (midtop = (384 / 2 , 10 ))
96
+ score_surf = font .render ('SCORE' , False , 'gold' )
97
+ score_rect = score_surf .get_rect (topleft = (10 , 10 ))
98
+
99
+ menu_surf = font .render ('Press the spacebar to start the game' , False , 'white' )
100
+ menu_rect = menu_surf .get_rect (midtop = (384 / 2 , 10 ))
101
+
102
+
103
+ #Title
104
+ en_title_surf = pygame .image .load ("C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/Troll_Walk_1.png" ).convert_alpha ()
105
+ en_title_surf = pygame .transform .scale (en_title_surf , (200 , 200 ))
106
+ en_title_rect = en_title_surf .get_rect (midbottom = (20 , 216 ))
107
+ title_image = pygame .image .load ('C:/Users/mudbo/pdx_code/programming_101/unit_2/Background layers/adventurer-run-04.png' ).convert ()
108
+ title_image = pygame .transform .scale (title_image , (200 , 200 ))
109
+ title_image_rect = title_image .get_rect (midbottom = (384 / 2 , 216 ))
110
+
111
+
112
+ #Gravity is going to be used for my jumping
113
+ gravity = 0
114
+
115
+ #Just for animation when not dead
116
+ game_running = False
117
+
118
+
119
+ #This allows the window to stay on until False
120
+ while True :
121
+
122
+
123
+ #Now it's time to check for events (If you move your mouse, click an arrow key, space, etc...)
124
+ for event in pygame .event .get ():
125
+ if event .type == pygame .QUIT :
126
+ pygame .quit ()
127
+ exit ()
128
+
129
+ #SPACEBAR for jumping
130
+ if game_running :
131
+ if event .type == pygame .KEYDOWN :
132
+ if event .key == pygame .K_SPACE and play_rect .bottom == 200 :
133
+ gravity = - 15
134
+ jump_sound .play ()
135
+
136
+
137
+ #SPACEBAR for restarting game
138
+ else :
139
+ if event .type == pygame .KEYDOWN :
140
+ if event .key == pygame .K_SPACE :
141
+ game_running = True
142
+ en_rect .left = 300
143
+
144
+
145
+ if game_running :
146
+
147
+
148
+ #BACKGROUND
149
+ screen .blit (back_drop_surf ,(0 ,0 ))
150
+ screen .blit (tree_drop_surf_1 ,(0 ,0 ))
151
+
152
+
153
+ screen .blit (tree_drop_surf_2 ,(0 ,0 ))
154
+ screen .blit (coin_surf , coin_rect )
155
+ screen .blit (tree_drop_surf_3 ,(0 ,0 ))
156
+ screen .blit (tree_drop_surf_4 ,(0 ,0 ))
157
+
158
+
159
+ #GROUND
160
+ screen .blit (grnd_drop_surf ,(0 ,200 ))
161
+ screen .blit (grnd_drop_surf_2 ,(0 ,202 ))
162
+
163
+
164
+ #TEXT
165
+ screen .blit (title_surf , title_rect )
166
+ screen .blit (score_surf , score_rect )
167
+
168
+
169
+ #PLAYER ACTION
170
+ #How gravity works - if you +1 gravity on player pos(190) it adds 1 (1 + 190 = 191) on the next loop (1 + 1 + 191 = 193)
171
+ gravity += 0.8
172
+ play_rect .y += gravity
173
+ if play_rect .bottom >= 200 :
174
+ play_rect .bottom = 200
175
+ player_physics ()
176
+ screen .blit (play_surf ,play_rect )
177
+
178
+
179
+ #ENEMY ACTION
180
+ en_rect .x -= 3
181
+ screen .blit (en_surf , en_rect )
182
+ if en_rect .right <= 0 :
183
+ en_rect .left = 384
184
+
185
+
186
+ #END GAME
187
+ if en_rect .colliderect (play_rect ):
188
+ game_running = False
189
+
190
+
191
+ else :
192
+ screen .fill ('black' )
193
+ screen .blit (title_image , title_image_rect )
194
+ screen .blit (menu_surf , menu_rect )
195
+ screen .blit (en_title_surf , en_title_rect )
196
+
197
+
198
+ #Continues to update the surface while running through the loop
199
+ pygame .display .update ()
200
+
201
+ #Framerate
202
+ clock .tick (60 )
0 commit comments