From c4450ff3df75ffa68693dc2fa34679576bd8fb76 Mon Sep 17 00:00:00 2001 From: henriqueandras Date: Sun, 21 Nov 2021 20:47:35 -0700 Subject: [PATCH 1/2] Change in next_level() --- game/main.py | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/game/main.py b/game/main.py index 70a17a0..bd384d5 100644 --- a/game/main.py +++ b/game/main.py @@ -18,7 +18,7 @@ SCREEN_HEIGHT = 800 FRAME_RATE = 60 -# Useful colors +# Useful colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) @@ -70,25 +70,20 @@ level = levels[0] # Start the next level -def next_level(level, levels, player): + + +def next_level(level, levels): """ Start the next level from the list of levels - - Args: - player: The player to reset """ # Get the next level by getting the index of the current level and adding one # Note there's a new bug: When the last level is finished, the next level doesn't exist new_level_index = levels.index(level) + 1 new_level = levels[new_level_index] - - # Start the next level - new_level.start(player) - - # Return the next level so that the current level can be set to the next level return new_level + # Create the player sprite and add it to the players sprite group player = Player(400, 500) players = pygame.sprite.Group() @@ -104,7 +99,7 @@ def next_level(level, levels, player): if event.type == pygame.QUIT: # When user clicks the 'x' on the window, close our game pygame.quit() sys.exit() - + # Keyboard events keys_pressed = pygame.key.get_pressed() if keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]: @@ -114,13 +109,15 @@ def next_level(level, levels, player): if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]: player.move(player.move_speed, 0) if keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]: - pass # Now that we have platforms, there's no reason to make the player move down. + # Now that we have platforms, there's no reason to make the player move down. + pass if keys_pressed[pygame.K_SPACE]: player.create_new_bullet(level) # Mouse events - mouse_pos = pygame.mouse.get_pos() # Get position of mouse as a tuple representing the + # Get position of mouse as a tuple representing the + mouse_pos = pygame.mouse.get_pos() # (x, y) coordinate mouse_buttons = pygame.mouse.get_pressed() @@ -132,11 +129,11 @@ def next_level(level, levels, player): """ UPDATE section - manipulate everything on the screen """ - + players.update() level.enemies.update() player.bullets.update() - + # Handle collisions with platforms hit_platforms = pygame.sprite.spritecollide(player, level.platforms, False) for platform in hit_platforms: @@ -145,7 +142,6 @@ def next_level(level, levels, player): if len(hit_platforms) == 0: player.can_jump = False - # Handle collisions with enemies hit_enemies = pygame.sprite.spritecollide(player, level.enemies, False) for enemy in hit_enemies: @@ -157,18 +153,19 @@ def next_level(level, levels, player): # When all the enemies are defeated in a level, start the next level if len(level.enemies) == 0: - level = next_level(level, levels, player) + level = next_level(level, levels) """ DRAW section - make everything show up on screen """ screen.fill(BLACK) # Fill the screen with one colour - + level.platforms.draw(screen) players.draw(screen) level.enemies.draw(screen) player.bullets.draw(screen) - pygame.display.flip() # Pygame uses a double-buffer, without this we see half-completed frames - clock.tick(FRAME_RATE) # Pause the clock to always maintain FRAME_RATE frames per second - \ No newline at end of file + # Pygame uses a double-buffer, without this we see half-completed frames + pygame.display.flip() + # Pause the clock to always maintain FRAME_RATE frames per second + clock.tick(FRAME_RATE) From ddfd0d1027dfcec134b2a78e6b7d41fa2a90d50a Mon Sep 17 00:00:00 2001 From: henriqueandras Date: Sun, 21 Nov 2021 21:33:29 -0700 Subject: [PATCH 2/2] Adding main.py to session7 folder --- session7/main.py | 171 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 session7/main.py diff --git a/session7/main.py b/session7/main.py new file mode 100644 index 0000000..bd384d5 --- /dev/null +++ b/session7/main.py @@ -0,0 +1,171 @@ +# Created by Schulich Ignite Flare and students of Schulich Ignite + +import sys +import os +import pygame +from platform import Platform +from player import Player +from enemy import Enemy +from level import Level + +""" +SETUP section - preparing everything before the main loop runs +""" +pygame.init() + +# Global constants +SCREEN_WIDTH = 1000 +SCREEN_HEIGHT = 800 +FRAME_RATE = 60 + +# Useful colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) + +# Creating the screen and the clock +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) +screen.set_alpha(0) # Make alpha bits transparent +clock = pygame.time.Clock() + +# List of all levels +# Each level has a list of platforms and a list of enemies +# Each level has an optional third parameter called player_spawn that sets where the player will first appear +levels = [ + # Level 0 + Level([ + Platform(300, 600, 350, 50), + Platform(100, 500, 200, 50), + Platform(650, 450, 250, 50), + Platform(700, 650, 200, 25) + ], [ + Enemy(750, 410) + ], (400, 500)), + # Level 1 + Level([ + Platform(250, 600, 400, 50), + Platform(100, 700, 200, 50), + Platform(650, 300, 250, 50), + Platform(700, 650, 200, 25), + Platform(500, 450, 100, 25) + ], [ + Enemy(750, 260), + Enemy(150, 660), + ], (400, 500)), + # Level 2 + Level([ + Platform(50, 750, 350, 25), + Platform(500, 700, 200, 50), + Platform(700, 600, 150, 25), + Platform(425, 400, 200, 25), + Platform(150, 350, 200, 50) + ], [ + Enemy(600, 660), + Enemy(750, 560), + Enemy(500, 360), + Enemy(200, 310) + ], (100, 650)) +] + +# Set the current level to be the first level in the game +level = levels[0] + +# Start the next level + + +def next_level(level, levels): + """ + Start the next level from the list of levels + """ + + # Get the next level by getting the index of the current level and adding one + # Note there's a new bug: When the last level is finished, the next level doesn't exist + new_level_index = levels.index(level) + 1 + new_level = levels[new_level_index] + return new_level + + +# Create the player sprite and add it to the players sprite group +player = Player(400, 500) +players = pygame.sprite.Group() +players.add(player) + +level.start(player) + +while True: + """ + EVENTS section - how the code reacts when users do things + """ + for event in pygame.event.get(): + if event.type == pygame.QUIT: # When user clicks the 'x' on the window, close our game + pygame.quit() + sys.exit() + + # Keyboard events + keys_pressed = pygame.key.get_pressed() + if keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]: + player.jump() + if keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]: + player.move(-player.move_speed, 0) + if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]: + player.move(player.move_speed, 0) + if keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]: + # Now that we have platforms, there's no reason to make the player move down. + pass + + if keys_pressed[pygame.K_SPACE]: + player.create_new_bullet(level) + + # Mouse events + # Get position of mouse as a tuple representing the + mouse_pos = pygame.mouse.get_pos() + # (x, y) coordinate + + mouse_buttons = pygame.mouse.get_pressed() + if mouse_buttons[0]: # If left mouse pressed + player.teleport(mouse_pos[0], mouse_pos[1]) + if mouse_buttons[2]: # If right mouse pressed + pass # Replace this line + + """ + UPDATE section - manipulate everything on the screen + """ + + players.update() + level.enemies.update() + player.bullets.update() + + # Handle collisions with platforms + hit_platforms = pygame.sprite.spritecollide(player, level.platforms, False) + for platform in hit_platforms: + player.on_platform_collide(platform) + + if len(hit_platforms) == 0: + player.can_jump = False + + # Handle collisions with enemies + hit_enemies = pygame.sprite.spritecollide(player, level.enemies, False) + for enemy in hit_enemies: + # Check if collision is from "above", with 15 pixels margin of error + if player.rect.y + player.rect.height < enemy.rect.y + 15: + enemy.kill() + else: + player.kill() + + # When all the enemies are defeated in a level, start the next level + if len(level.enemies) == 0: + level = next_level(level, levels) + + """ + DRAW section - make everything show up on screen + """ + screen.fill(BLACK) # Fill the screen with one colour + + level.platforms.draw(screen) + players.draw(screen) + level.enemies.draw(screen) + player.bullets.draw(screen) + + # Pygame uses a double-buffer, without this we see half-completed frames + pygame.display.flip() + # Pause the clock to always maintain FRAME_RATE frames per second + clock.tick(FRAME_RATE)