├── assets └── images │ ├── barrels │ ├── barrel.png │ ├── barrel2.png │ └── barrel3.png │ ├── dk │ ├── dk1.png │ ├── dk2.png │ └── dk3.png │ ├── fire.png │ ├── fireball.png │ ├── fireball2.png │ ├── hammer.png │ ├── mario │ ├── climbing1.png │ ├── climbing2.png │ ├── hammer_jump.png │ ├── hammer_overhead.png │ ├── hammer_stand.png │ ├── jumping.png │ ├── running.png │ └── standing.png │ └── peach │ ├── peach1.png │ └── peach2.png ├── dk.jpg ├── dk2.png └── main.py /assets/images/barrels/barrel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/barrels/barrel.png -------------------------------------------------------------------------------- /assets/images/barrels/barrel2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/barrels/barrel2.png -------------------------------------------------------------------------------- /assets/images/barrels/barrel3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/barrels/barrel3.png -------------------------------------------------------------------------------- /assets/images/dk/dk1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/dk/dk1.png -------------------------------------------------------------------------------- /assets/images/dk/dk2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/dk/dk2.png -------------------------------------------------------------------------------- /assets/images/dk/dk3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/dk/dk3.png -------------------------------------------------------------------------------- /assets/images/fire.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/fire.png -------------------------------------------------------------------------------- /assets/images/fireball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/fireball.png -------------------------------------------------------------------------------- /assets/images/fireball2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/fireball2.png -------------------------------------------------------------------------------- /assets/images/hammer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/hammer.png -------------------------------------------------------------------------------- /assets/images/mario/climbing1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/mario/climbing1.png -------------------------------------------------------------------------------- /assets/images/mario/climbing2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/mario/climbing2.png -------------------------------------------------------------------------------- /assets/images/mario/hammer_jump.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/mario/hammer_jump.png -------------------------------------------------------------------------------- /assets/images/mario/hammer_overhead.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/mario/hammer_overhead.png -------------------------------------------------------------------------------- /assets/images/mario/hammer_stand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/mario/hammer_stand.png -------------------------------------------------------------------------------- /assets/images/mario/jumping.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/mario/jumping.png -------------------------------------------------------------------------------- /assets/images/mario/running.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/mario/running.png -------------------------------------------------------------------------------- /assets/images/mario/standing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/mario/standing.png -------------------------------------------------------------------------------- /assets/images/peach/peach1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/peach/peach1.png -------------------------------------------------------------------------------- /assets/images/peach/peach2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/assets/images/peach/peach2.png -------------------------------------------------------------------------------- /dk.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/dk.jpg -------------------------------------------------------------------------------- /dk2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/plemaster01/PythonDonkeyKong/3168555d483b4488c3796e7a10c7153736d9b670/dk2.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # DONKEY KONG REBUILD IN PYTHON WITH THE PYGAME MODULE! (Est.720 Lines of Code) 2 | import os 3 | import random 4 | 5 | import pygame 6 | 7 | os.environ['SDL_VIDEO_CENTERED'] = '1' # call before pygame.init() 8 | pygame.init() 9 | info = pygame.display.Info() 10 | screen_width, screen_height = info.current_w, info.current_h 11 | window_width, window_height = screen_width - 800, screen_height - 150 12 | 13 | timer = pygame.time.Clock() 14 | fps = 60 15 | 16 | pygame.display.set_caption('Classic Donkey Kong Rebuild!') 17 | # pygame.display.set_icon('image file') 18 | 19 | font = pygame.font.Font('freesansbold.ttf', 50) 20 | font2 = pygame.font.Font('freesansbold.ttf', 30) 21 | 22 | screen = pygame.display.set_mode([window_width, window_height]) 23 | section_width = window_width // 32 24 | section_height = window_height // 32 25 | slope = section_height // 8 26 | 27 | barrel_spawn_time = 360 28 | barrel_count = barrel_spawn_time / 2 29 | barrel_time = 360 30 | barrel_img = pygame.transform.scale(pygame.image.load('assets/images/barrels/barrel.png'), 31 | (section_width * 1.5, section_height * 2)) 32 | flames_img = pygame.transform.scale(pygame.image.load('assets/images/fire.png'), 33 | (section_width * 2, section_height)) 34 | barrel_side = pygame.transform.scale(pygame.image.load('assets/images/barrels/barrel2.png'), 35 | (section_width * 2, section_height * 2.5)) 36 | dk1 = pygame.transform.scale(pygame.image.load('assets/images/dk/dk1.png'), 37 | (section_width * 5, section_height * 5)) 38 | dk2 = pygame.transform.scale(pygame.image.load('assets/images/dk/dk2.png'), 39 | (section_width * 5, section_height * 5)) 40 | dk3 = pygame.transform.scale(pygame.image.load('assets/images/dk/dk3.png'), 41 | (section_width * 5, section_height * 5)) 42 | peach1 = pygame.transform.scale(pygame.image.load('assets/images/peach/peach1.png'), 43 | (2 * section_width, 3 * section_height)) 44 | peach2 = pygame.transform.scale(pygame.image.load('assets/images/peach/peach2.png'), 45 | (2 * section_width, 3 * section_height)) 46 | fireball = pygame.transform.scale(pygame.image.load('assets/images/fireball.png'), 47 | (1.5 * section_width, 2 * section_height)) 48 | fireball2 = pygame.transform.scale(pygame.image.load('assets/images/fireball2.png'), 49 | (1.5 * section_width, 2 * section_height)) 50 | hammer = pygame.transform.scale(pygame.image.load('assets/images/hammer.png'), 51 | (2 * section_width, 2 * section_height)) 52 | standing = pygame.transform.scale(pygame.image.load('assets/images/mario/standing.png'), 53 | (2 * section_width, 2.5 * section_height)) 54 | jumping = pygame.transform.scale(pygame.image.load('assets/images/mario/jumping.png'), 55 | (2 * section_width, 2.5 * section_height)) 56 | running = pygame.transform.scale(pygame.image.load('assets/images/mario/running.png'), 57 | (2 * section_width, 2.5 * section_height)) 58 | climbing1 = pygame.transform.scale(pygame.image.load('assets/images/mario/climbing1.png'), 59 | (2 * section_width, 2.5 * section_height)) 60 | climbing2 = pygame.transform.scale(pygame.image.load('assets/images/mario/climbing2.png'), 61 | (2 * section_width, 2.5 * section_height)) 62 | hammer_stand = pygame.transform.scale(pygame.image.load('assets/images/mario/hammer_stand.png'), 63 | (2.5 * section_width, 2.5 * section_height)) 64 | hammer_jump = pygame.transform.scale(pygame.image.load('assets/images/mario/hammer_jump.png'), 65 | (2.5 * section_width, 2.5 * section_height)) 66 | hammer_overhead = pygame.transform.scale(pygame.image.load('assets/images/mario/hammer_overhead.png'), 67 | (2.5 * section_width, 3.5 * section_height)) 68 | 69 | start_y = window_height - 2 * section_height 70 | row2_y = start_y - 4 * section_height 71 | row3_y = row2_y - 7 * slope - 3 * section_height 72 | row4_y = row3_y - 4 * section_height 73 | row5_y = row4_y - 7 * slope - 3 * section_height 74 | row6_y = row5_y - 4 * section_height 75 | row6_top = row6_y - 4 * slope 76 | row5_top = row5_y - 8 * slope 77 | row4_top = row4_y - 8 * slope 78 | row3_top = row3_y - 8 * slope 79 | row2_top = row2_y - 8 * slope 80 | row1_top = start_y - 5 * slope 81 | fireball_trigger = False 82 | active_level = 0 83 | counter = 0 84 | score = 0 85 | high_score = 0 86 | lives = 5 87 | bonus = 6000 88 | first_fireball_trigger = False 89 | victory = False 90 | reset_game = False 91 | levels = [{'bridges': [(1, start_y, 15), (16, start_y - slope, 3), 92 | (19, start_y - 2 * slope, 3), (22, start_y - 3 * slope, 3), 93 | (25, start_y - 4 * slope, 3), (28, start_y - 5 * slope, 3), 94 | (25, row2_y, 3), (22, row2_y - slope, 3), 95 | (19, row2_y - 2 * slope, 3), (16, row2_y - 3 * slope, 3), 96 | (13, row2_y - 4 * slope, 3), (10, row2_y - 5 * slope, 3), 97 | (7, row2_y - 6 * slope, 3), (4, row2_y - 7 * slope, 3), 98 | (2, row2_y - 8 * slope, 2), (4, row3_y, 3), 99 | (7, row3_y - slope, 3), (10, row3_y - 2 * slope, 3), 100 | (13, row3_y - 3 * slope, 3), (16, row3_y - 4 * slope, 3), 101 | (19, row3_y - 5 * slope, 3), (22, row3_y - 6 * slope, 3), 102 | (25, row3_y - 7 * slope, 3), (28, row3_y - 8 * slope, 2), 103 | (25, row4_y, 3), (22, row4_y - slope, 3), 104 | (19, row4_y - 2 * slope, 3), (16, row4_y - 3 * slope, 3), 105 | (13, row4_y - 4 * slope, 3), (10, row4_y - 5 * slope, 3), 106 | (7, row4_y - 6 * slope, 3), (4, row4_y - 7 * slope, 3), 107 | (2, row4_y - 8 * slope, 2), (4, row5_y, 3), 108 | (7, row5_y - slope, 3), (10, row5_y - 2 * slope, 3), 109 | (13, row5_y - 3 * slope, 3), (16, row5_y - 4 * slope, 3), 110 | (19, row5_y - 5 * slope, 3), (22, row5_y - 6 * slope, 3), 111 | (25, row5_y - 7 * slope, 3), (28, row5_y - 8 * slope, 2), 112 | (25, row6_y, 3), (22, row6_y - slope, 3), 113 | (19, row6_y - 2 * slope, 3), (16, row6_y - 3 * slope, 3), 114 | (2, row6_y - 4 * slope, 14), (13, row6_y - 4 * section_height, 6), 115 | (10, row6_y - 3 * section_height, 3)], 116 | 'ladders': [(12, row2_y + 6 * slope, 2), (12, row2_y + 26 * slope, 2), 117 | (25, row2_y + 11 * slope, 4), (6, row3_y + 11 * slope, 3), 118 | (14, row3_y + 8 * slope, 4), (10, row4_y + 6 * slope, 1), 119 | (10, row4_y + 24 * slope, 2), (16, row4_y + 6 * slope, 5), 120 | (25, row4_y + 9 * slope, 4), (6, row5_y + 11 * slope, 3), 121 | (11, row5_y + 8 * slope, 4), (23, row5_y + 4 * slope, 1), 122 | (23, row5_y + 24 * slope, 2), (25, row6_y + 9 * slope, 4), 123 | (13, row6_y + 5 * slope, 2), (13, row6_y + 25 * slope, 2), 124 | (18, row6_y - 27 * slope, 4), (12, row6_y - 17 * slope, 2), 125 | (10, row6_y - 17 * slope, 2), (12, -5, 13), (10, -5, 13)], 126 | 'hammers': [(4, row6_top + section_height), (4, row4_top+section_height)], 127 | 'target': (13, row6_y - 4 * section_height, 3)}] 128 | 129 | 130 | class Player(pygame.sprite.Sprite): 131 | def __init__(self, x_pos, y_pos): 132 | pygame.sprite.Sprite.__init__(self) 133 | self.y_change = 0 134 | self.x_speed = 3 135 | self.x_change = 0 136 | self.landed = False 137 | self.pos = 0 138 | self.dir = 1 139 | self.count = 0 140 | self.climbing = False 141 | self.image = standing 142 | self.hammer = False 143 | self.max_hammer = 450 144 | self.hammer_len = self.max_hammer 145 | self.hammer_pos = 1 146 | self.rect = self.image.get_rect() 147 | self.hitbox = self.rect 148 | self.hammer_box = self.rect 149 | self.rect.center = (x_pos, y_pos) 150 | self.over_barrel = False 151 | self.bottom = pygame.rect.Rect(self.rect.left, self.rect.bottom - 20, self.rect.width, 20) 152 | 153 | def update(self): 154 | self.landed = False 155 | for i in range(len(plats)): 156 | if self.bottom.colliderect(plats[i]): 157 | self.landed = True 158 | if not self.climbing: 159 | self.rect.centery = plats[i].top - self.rect.height / 2 + 1 160 | if not self.landed and not self.climbing: 161 | self.y_change += 0.25 162 | self.rect.move_ip(self.x_change * self.x_speed, self.y_change) 163 | self.bottom = pygame.rect.Rect(self.rect.left, self.rect.bottom - 20, self.rect.width, 20) 164 | if self.x_change != 0 or (self.climbing and self.y_change != 0): 165 | if self.count < 3: 166 | self.count += 1 167 | else: 168 | self.count = 0 169 | if self.pos == 0: 170 | self.pos += 1 171 | else: 172 | self.pos = 0 173 | else: 174 | self.pos = 0 175 | if self.hammer: 176 | self.hammer_pos = (self.hammer_len // 30) % 2 177 | self.hammer_len -= 1 178 | if self.hammer_len == 0: 179 | self.hammer = False 180 | self.hammer_len = self.max_hammer 181 | 182 | def draw(self): 183 | if not self.hammer: 184 | if not self.climbing and self.landed: 185 | if self.pos == 0: 186 | self.image = standing 187 | else: 188 | self.image = running 189 | if not self.landed and not self.climbing: 190 | self.image = jumping 191 | if self.climbing: 192 | if self.pos == 0: 193 | self.image = climbing1 194 | else: 195 | self.image = climbing2 196 | else: 197 | if self.hammer_pos == 0: 198 | self.image = hammer_jump 199 | else: 200 | self.image = hammer_overhead 201 | if self.dir == -1: 202 | self.image = pygame.transform.flip(self.image, True, False) 203 | else: 204 | self.image = self.image 205 | self.calc_hitbox() 206 | if self.hammer_pos == 1 and self.hammer: 207 | screen.blit(self.image, (self.rect.left, self.rect.top - section_height)) 208 | else: 209 | screen.blit(self.image, self.rect.topleft) 210 | 211 | def calc_hitbox(self): 212 | if not self.hammer: 213 | self.hitbox = pygame.rect.Rect((self.rect[0] + 15, self.rect[1] + 5), 214 | (self.rect[2] - 30, self.rect[3] - 10)) 215 | elif self.hammer_pos == 0: 216 | if self.dir == 1: 217 | self.hitbox = pygame.rect.Rect((self.rect[0], self.rect[1] + 5), 218 | (self.rect[2] - 30, self.rect[3] - 10)) 219 | self.hammer_box = pygame.rect.Rect((self.hitbox[0] + self.hitbox[2], self.rect[1] + 5), 220 | (self.hitbox[2], self.rect[3] - 10)) 221 | else: 222 | self.hitbox = pygame.rect.Rect((self.rect[0] + 40, self.rect[1] + 5), 223 | (self.rect[2] - 30, self.rect[3] - 10)) 224 | self.hammer_box = pygame.rect.Rect((self.hitbox[0] - self.hitbox[2], self.rect[1] + 5), 225 | (self.hitbox[2], self.rect[3] - 10)) 226 | else: 227 | self.hitbox = pygame.rect.Rect((self.rect[0] + 15, self.rect[1] + 5), 228 | (self.rect[2] - 30, self.rect[3] - 10)) 229 | self.hammer_box = pygame.rect.Rect((self.hitbox[0], self.hitbox[1] - section_height), 230 | (self.hitbox[2], section_height)) 231 | 232 | 233 | class Hammer(pygame.sprite.Sprite): 234 | def __init__(self, x_pos, y_pos): 235 | pygame.sprite.Sprite.__init__(self) 236 | self.image = hammer 237 | self.rect = self.image.get_rect() 238 | self.rect.top = y_pos 239 | self.rect.left = x_pos * section_width 240 | self.used = False 241 | 242 | def draw(self): 243 | if not self.used: 244 | screen.blit(self.image, (self.rect[0], self.rect[1])) 245 | if self.rect.colliderect(player.hitbox): 246 | self.kill() 247 | player.hammer = True 248 | player.hammer_len = player.max_hammer 249 | self.used = True 250 | 251 | 252 | class Barrel(pygame.sprite.Sprite): 253 | def __init__(self, x_pos, y_pos): 254 | pygame.sprite.Sprite.__init__(self) 255 | self.image = pygame.Surface((50, 50)) 256 | self.rect = self.image.get_rect() 257 | self.rect.center = (x_pos, y_pos) 258 | self.y_change = 0 259 | self.x_change = 1 260 | self.pos = 0 261 | self.count = 0 262 | self.oil_collision = False 263 | self.falling = False 264 | self.check_lad = False 265 | self.bottom = self.rect 266 | 267 | def update(self, fire_trig): 268 | if self.y_change < 8 and not self.falling: 269 | barrel.y_change += 2 270 | for i in range(len(plats)): 271 | if self.bottom.colliderect(plats[i]): 272 | self.y_change = 0 273 | self.falling = False 274 | if self.rect.colliderect(oil_drum): 275 | if not self.oil_collision: 276 | self.oil_collision = True 277 | if random.randint(0, 4) == 4: 278 | fire_trig = True 279 | if not self.falling: 280 | if row5_top >= self.rect.bottom or row3_top >= self.rect.bottom >= row4_top or row1_top > self.rect.bottom >= row2_top: 281 | self.x_change = 3 282 | else: 283 | self.x_change = -3 284 | else: 285 | self.x_change = 0 286 | self.rect.move_ip(self.x_change, self.y_change) 287 | if self.rect.top > screen_height: 288 | self.kill() 289 | if self.count < 15: 290 | self.count += 1 291 | else: 292 | self.count = 0 293 | if self.x_change > 0: 294 | if self.pos < 3: 295 | self.pos += 1 296 | else: 297 | self.pos = 0 298 | else: 299 | if self.pos > 0: 300 | self.pos -= 1 301 | else: 302 | self.pos = 3 303 | self.bottom = pygame.rect.Rect((self.rect[0], self.rect.bottom), (self.rect[2], 3)) 304 | return fire_trig 305 | 306 | def check_fall(self): 307 | already_collided = False 308 | below = pygame.rect.Rect((self.rect[0], self.rect[1] + section_height), (self.rect[2], section_height)) 309 | for lad in lads: 310 | if below.colliderect(lad) and not self.falling and not self.check_lad: 311 | self.check_lad = True 312 | already_collided = True 313 | if random.randint(0, 60) == 60: 314 | self.falling = True 315 | self.y_change = 4 316 | if not already_collided: 317 | self.check_lad = False 318 | 319 | def draw(self): 320 | screen.blit(pygame.transform.rotate(barrel_img, 90 * self.pos), self.rect.topleft) 321 | 322 | 323 | class Flame(pygame.sprite.Sprite): 324 | def __init__(self, x_pos, y_pos): 325 | pygame.sprite.Sprite.__init__(self) 326 | self.image = fireball 327 | self.rect = self.image.get_rect() 328 | self.rect.center = (x_pos, y_pos) 329 | self.pos = 1 330 | self.count = 0 331 | self.x_count = 0 332 | self.x_change = 2 333 | self.x_max = 4 334 | self.y_change = 0 335 | self.row = 1 336 | self.check_lad = False 337 | self.climbing = False 338 | 339 | def update(self): 340 | if self.y_change < 3 and not self.climbing: 341 | flame.y_change += 0.25 342 | for i in range(len(plats)): 343 | if self.rect.colliderect(plats[i]): 344 | flame.climbing = False 345 | flame.y_change = -4 346 | # if flame collides with players hitbox - trigger reset of the game (also do this to barrels) 347 | if self.count < 15: 348 | self.count += 1 349 | else: 350 | self.count = 0 351 | self.pos *= -1 352 | if self.x_count < self.x_max: 353 | self.x_count += 1 354 | else: # row 1,3 and 5 - go further right than left overall, otherwise flip it 355 | self.x_count = 0 356 | if self.x_change > 0: 357 | if self.row in [1, 3, 5]: 358 | self.x_max = random.randint(3, 6) 359 | else: 360 | self.x_max = random.randint(6, 10) 361 | else: 362 | if self.row in [1, 3, 5]: 363 | self.x_max = random.randint(6, 10) 364 | else: 365 | self.x_max = random.randint(3, 6) 366 | self.x_change *= -1 367 | if self.pos == 1: 368 | if self.x_change > 0: 369 | self.image = fireball 370 | else: 371 | self.image = pygame.transform.flip(fireball, True, False) 372 | else: 373 | if self.x_change > 0: 374 | self.image = fireball2 375 | else: 376 | self.image = pygame.transform.flip(fireball2, True, False) 377 | self.rect.move_ip(self.x_change, self.y_change) 378 | if self.rect.top > screen_height or self.rect.top < 0: 379 | self.kill() 380 | 381 | def check_climb(self): 382 | already_collided = False 383 | for lad in lads: 384 | if self.rect.colliderect(lad) and not self.climbing and not self.check_lad: 385 | self.check_lad = True 386 | already_collided = True 387 | if random.randint(0, 120) == 120: 388 | self.climbing = True 389 | self.y_change = - 4 390 | if not already_collided: 391 | self.check_lad = False 392 | if self.rect.bottom < row6_y: 393 | self.row = 6 394 | elif self.rect.bottom < row5_y: 395 | self.row = 5 396 | elif self.rect.bottom < row4_y: 397 | self.row = 4 398 | elif self.rect.bottom < row3_y: 399 | self.row = 3 400 | elif self.rect.bottom < row2_y: 401 | self.row = 2 402 | else: 403 | self.row = 1 404 | 405 | 406 | class Bridge: 407 | def __init__(self, x_pos, y_pos, length): 408 | self.x_pos = x_pos * section_width 409 | self.y_pos = y_pos 410 | self.length = length 411 | self.top = self.draw() 412 | 413 | def draw(self): 414 | line_width = 7 415 | platform_color = (225, 51, 129) 416 | for i in range(self.length): 417 | bot_coord = self.y_pos + section_height 418 | left_coord = self.x_pos + (section_width * i) 419 | mid_coord = left_coord + (section_width * 0.5) 420 | right_coord = left_coord + section_width 421 | top_coord = self.y_pos 422 | # draw 4 lines, top, bot, left diag, right diag 423 | pygame.draw.line(screen, platform_color, (left_coord, top_coord), 424 | (right_coord, top_coord), line_width) 425 | pygame.draw.line(screen, platform_color, (left_coord, bot_coord), 426 | (right_coord, bot_coord), line_width) 427 | pygame.draw.line(screen, platform_color, (left_coord, bot_coord), 428 | (mid_coord, top_coord), line_width) 429 | pygame.draw.line(screen, platform_color, (mid_coord, top_coord), 430 | (right_coord, bot_coord), line_width) 431 | # get the top platform 'surface' 432 | top_line = pygame.rect.Rect((self.x_pos, self.y_pos), (self.length * section_width, 2)) 433 | # pygame.draw.rect(screen, 'blue', top_line) 434 | return top_line 435 | 436 | 437 | class Ladder: 438 | def __init__(self, x_pos, y_pos, length): 439 | self.x_pos = x_pos * section_width 440 | self.y_pos = y_pos 441 | self.length = length 442 | self.body = self.draw() 443 | 444 | def draw(self): 445 | line_width = 3 446 | lad_color = 'light blue' 447 | lad_height = 0.6 448 | for i in range(self.length): 449 | top_coord = self.y_pos + lad_height * section_height * i 450 | bot_coord = top_coord + lad_height * section_height 451 | mid_coord = (lad_height / 2) * section_height + top_coord 452 | left_coord = self.x_pos 453 | right_coord = left_coord + section_width 454 | pygame.draw.line(screen, lad_color, (left_coord, top_coord), (left_coord, bot_coord), line_width) 455 | pygame.draw.line(screen, lad_color, (right_coord, top_coord), (right_coord, bot_coord), line_width) 456 | pygame.draw.line(screen, lad_color, (left_coord, mid_coord), (right_coord, mid_coord), line_width) 457 | body = pygame.rect.Rect((self.x_pos, self.y_pos - section_height), 458 | (section_width, (lad_height * self.length * section_height + section_height))) 459 | return body 460 | 461 | 462 | # function to draw platforms and ladders 463 | def draw_screen(): 464 | platforms = [] 465 | climbers = [] 466 | ladder_objs = [] 467 | bridge_objs = [] 468 | 469 | ladders = levels[active_level]['ladders'] 470 | bridges = levels[active_level]['bridges'] 471 | 472 | for ladder in ladders: 473 | ladder_objs.append(Ladder(*ladder)) 474 | if ladder[2] >= 3: 475 | climbers.append(ladder_objs[-1].body) 476 | for bridge in bridges: 477 | bridge_objs.append(Bridge(*bridge)) 478 | platforms.append(bridge_objs[-1].top) 479 | 480 | return platforms, climbers 481 | 482 | 483 | def draw_extras(): 484 | # put lives, levels, bonus text etc in here 485 | screen.blit(font.render(f'I•{score}', True, 'white'), (3*section_width, 2*section_height)) 486 | screen.blit(font.render(f'TOP•{high_score}', True, 'white'), (14 * section_width, 2 * section_height)) 487 | screen.blit(font.render(f'[ ][ ][ ]', True, 'white'), (20 * section_width, 4 * section_height)) 488 | screen.blit(font2.render(f' M BONUS L ', True, 'white'), (20 * section_width + 5, 4 * section_height)) 489 | screen.blit(font2.render(f' {lives} {bonus} {active_level + 1} ', True, 'white'), 490 | (20 * section_width + 5, 5 * section_height)) 491 | # draw peach 492 | if barrel_count < barrel_spawn_time / 2: 493 | screen.blit(peach1, (10 * section_width, row6_y - 6 * section_height)) 494 | else: 495 | screen.blit(peach2, (10 * section_width, row6_y - 6 * section_height)) 496 | # draw oil drum 497 | oil = draw_oil() 498 | # draw stationary barrels 499 | draw_barrels() 500 | # draw donkey kong 501 | draw_kong() 502 | return oil 503 | 504 | 505 | def draw_oil(): 506 | x_coord, y_coord = 4 * section_width, window_height - 4.5 * section_height 507 | oil = pygame.draw.rect(screen, 'blue', [x_coord, y_coord, 2 * section_width, 2.5 * section_height]) 508 | pygame.draw.rect(screen, 'blue', [x_coord - 0.1 * section_width, y_coord, 2.2 * section_width, .2 * section_height]) 509 | pygame.draw.rect(screen, 'blue', 510 | [x_coord - 0.1 * section_width, y_coord + 2.3 * section_height, 2.2 * section_width, 511 | .2 * section_height]) 512 | pygame.draw.rect(screen, 'light blue', 513 | [x_coord + 0.1 * section_width, y_coord + .2 * section_height, .2 * section_width, 514 | 2 * section_height]) 515 | pygame.draw.rect(screen, 'light blue', 516 | [x_coord, y_coord + 0.5 * section_height, 2 * section_width, .2 * section_height]) 517 | 518 | pygame.draw.rect(screen, 'light blue', 519 | [x_coord, y_coord + 1.7 * section_height, 2 * section_width, .2 * section_height]) 520 | screen.blit(font2.render('OIL', True, 'light blue'), (x_coord + .4 * section_width, y_coord + 0.7 * section_height)) 521 | for i in range(4): 522 | pygame.draw.circle(screen, 'red', 523 | (x_coord + 0.5 * section_width + i * 0.4 * section_width, y_coord + 2.1 * section_height), 3) 524 | # draw the flames on top 525 | if counter < 15 or 30 < counter < 45: 526 | screen.blit(flames_img, (x_coord, y_coord - section_height)) 527 | else: 528 | screen.blit(pygame.transform.flip(flames_img, True, False), (x_coord, y_coord - section_height)) 529 | return oil 530 | 531 | 532 | def draw_barrels(): 533 | screen.blit(pygame.transform.rotate(barrel_side, 90), (section_width * 1.2, 5.4 * section_height)) 534 | screen.blit(pygame.transform.rotate(barrel_side, 90), (section_width * 2.5, 5.4 * section_height)) 535 | screen.blit(pygame.transform.rotate(barrel_side, 90), (section_width * 2.5, 7.7 * section_height)) 536 | screen.blit(pygame.transform.rotate(barrel_side, 90), (section_width * 1.2, 7.7 * section_height)) 537 | 538 | 539 | def draw_kong(): 540 | phase_time = barrel_time // 4 541 | if barrel_spawn_time - barrel_count > 3 * phase_time: 542 | dk_img = dk2 543 | elif barrel_spawn_time - barrel_count > 2 * phase_time: 544 | dk_img = dk1 545 | elif barrel_spawn_time - barrel_count > phase_time: 546 | dk_img = dk3 547 | else: 548 | dk_img = pygame.transform.flip(dk1, True, False) 549 | screen.blit(barrel_img, (250, 250)) 550 | screen.blit(dk_img, (3.5 * section_width, row6_y - 5.5 * section_height)) 551 | 552 | 553 | def check_climb(): 554 | can_climb = False 555 | climb_down = False 556 | under = pygame.rect.Rect((player.rect[0], player.rect[1] + 2 * section_height), (player.rect[2], player.rect[3])) 557 | for lad in lads: 558 | if player.hitbox.colliderect(lad) and not can_climb: 559 | can_climb = True 560 | if under.colliderect(lad): 561 | climb_down = True 562 | if (not can_climb and (not climb_down or player.y_change < 0)) or \ 563 | (player.landed and can_climb and player.y_change > 0 and not climb_down): 564 | player.climbing = False 565 | return can_climb, climb_down 566 | 567 | 568 | def barrel_collide(reset): 569 | global score 570 | under = pygame.rect.Rect((player.rect[0], player.rect[1] + 2 * section_height), (player.rect[2], player.rect[3])) 571 | for brl in barrels: 572 | if brl.rect.colliderect(player.hitbox): 573 | reset = True 574 | elif not player.landed and not player.over_barrel and under.colliderect(brl): 575 | player.over_barrel = True 576 | score += 100 577 | if player.landed: 578 | player.over_barrel = False 579 | 580 | return reset 581 | 582 | 583 | def reset(): 584 | global player, barrels, flames, hammers, first_fireball_trigger, victory, lives, bonus 585 | global barrel_spawn_time, barrel_count 586 | pygame.time.delay(1000) 587 | for bar in barrels: 588 | bar.kill() 589 | for flam in flames: 590 | flam.kill() 591 | for hams in hammers: 592 | hams.kill() 593 | for hams in hammers_list: 594 | hammers.add(Hammer(*hams)) 595 | lives -= 1 596 | bonus = 6000 597 | player.kill() 598 | player = Player(250, window_height - 130) 599 | first_fireball_trigger = False 600 | barrel_spawn_time = 360 601 | barrel_count = barrel_spawn_time / 2 602 | victory = False 603 | 604 | 605 | def check_victory(): 606 | target = levels[active_level]['target'] 607 | target_rect = pygame.rect.Rect((target[0]*section_width, target[1]), (section_width*target[2], 1)) 608 | return player.bottom.colliderect(target_rect) 609 | 610 | 611 | barrels = pygame.sprite.Group() 612 | flames = pygame.sprite.Group() 613 | hammers = pygame.sprite.Group() 614 | hammers_list = levels[active_level]['hammers'] 615 | for ham in hammers_list: 616 | hammers.add(Hammer(*ham)) 617 | player = Player(250, window_height - 130) 618 | 619 | run = True 620 | while run: 621 | screen.fill('black') 622 | timer.tick(fps) 623 | if counter < 60: 624 | counter += 1 625 | else: 626 | counter = 0 627 | if bonus > 0: 628 | bonus -= 100 629 | 630 | # draw platforms and ladders on the screen in dedicated function 631 | plats, lads = draw_screen() 632 | oil_drum = draw_extras() 633 | climb, down = check_climb() 634 | victory = check_victory() 635 | if barrel_count < barrel_spawn_time: 636 | barrel_count += 1 637 | else: 638 | barrel_count = random.randint(0, 120) 639 | barrel_time = barrel_spawn_time - barrel_count 640 | barrel = Barrel(270, 270) 641 | barrels.add(barrel) 642 | if not first_fireball_trigger: 643 | flame = Flame(5*section_width, window_height - 4*section_height) 644 | flames.add(flame) 645 | first_fireball_trigger = True 646 | for barrel in barrels: 647 | barrel.draw() 648 | barrel.check_fall() 649 | fireball_trigger = barrel.update(fireball_trigger) 650 | if barrel.rect.colliderect(player.hammer_box) and player.hammer: 651 | barrel.kill() 652 | score += 500 653 | 654 | if fireball_trigger: 655 | flame = Flame(5 * section_width, window_height - 4 * section_height) 656 | flames.add(flame) 657 | fireball_trigger = False 658 | 659 | for flame in flames: 660 | flame.check_climb() 661 | if flame.rect.colliderect(player.hitbox): 662 | reset_game = True 663 | flames.draw(screen) 664 | flames.update() 665 | player.update() 666 | player.draw() 667 | for ham in hammers: 668 | ham.draw() 669 | 670 | reset_game = barrel_collide(reset_game) 671 | if reset_game: 672 | if lives > 0: 673 | reset() 674 | reset_game = False 675 | else: 676 | run = False 677 | 678 | for event in pygame.event.get(): 679 | if event.type == pygame.QUIT: 680 | run = False 681 | if event.type == pygame.KEYDOWN: 682 | if event.key == pygame.K_RIGHT and not player.climbing: 683 | player.x_change = 1 684 | player.dir = 1 685 | if event.key == pygame.K_LEFT and not player.climbing: 686 | player.x_change = -1 687 | player.dir = -1 688 | if event.key == pygame.K_SPACE and player.landed: 689 | player.landed = False 690 | player.y_change = -6 691 | if event.key == pygame.K_UP: 692 | if climb: 693 | player.y_change = -2 694 | player.x_change = 0 695 | player.climbing = True 696 | if event.key == pygame.K_DOWN: 697 | if down: 698 | player.y_change = 2 699 | player.x_change = 0 700 | player.climbing = True 701 | if event.type == pygame.KEYUP: 702 | if event.key == pygame.K_RIGHT: 703 | player.x_change = 0 704 | if event.key == pygame.K_LEFT: 705 | player.x_change = 0 706 | if event.key == pygame.K_UP: 707 | if climb: 708 | player.y_change = 0 709 | if player.climbing and player.landed: 710 | player.climbing = False 711 | if event.key == pygame.K_DOWN: 712 | if climb: 713 | player.y_change = 0 714 | if player.climbing and player.landed: 715 | player.climbing = False 716 | if victory: 717 | screen.blit(font.render('VICTORY!', True, 'white'), (window_width/2, window_height/2)) 718 | reset_game = True 719 | # active_level += 1 720 | lives += 1 721 | score += bonus 722 | if score > high_score: 723 | high_score = score 724 | score = 0 725 | player.climbing = False 726 | 727 | pygame.display.flip() 728 | pygame.quit() 729 | --------------------------------------------------------------------------------