Python Platformer code

Here's all the code for the platform game, so if you're unable to see the bit you need on the projector, you can look here instead. You'll notice that you can't copy and paste it. That's a good thing, because if you do, you:

a) won't learn anything, and
b) will likely break your code.

But, if you're struggling to keep up, and because it's the last week, give me a shout and I might just show you how to cheat ; )


More helpfully, Chris has put a guide to each week's lessons and objectives here, along with all the code as it should be at the end of each week.


*** Cheat mode activated ***


import pgzrun from platformer import* # platformer constants TILE_SIZE = 18 ROWS = 30 COLS = 20 # pygame constants WIDTH = TILE_SIZE * ROWS HEIGHT = TILE_SIZE * COLS TITLE = "FOXY LADY" # build world platforms = build("platformer_platforms.csv", TILE_SIZE) _keys = build("platformer_keys.csv", TILE_SIZE) hearts = build("platformer_hearts.csv", TILE_SIZE) obstacles = build("platformer_obstacles.csv", TILE_SIZE) backgrounds = build("platformer_backgrounds.csv", TILE_SIZE) # define sprites color_key = (0,0,0) fox_stand = Sprite("fox.png",(0,32,32,32),14,color_key,30) fox_walk = Sprite("fox.png",(0,64,32,32),8,color_key,5) # define player actor #player = Actor("p_right") player = SpriteActor(fox_stand) player.bottomleft = (0, HEIGHT - TILE_SIZE) # define player specific variables player.velocity_x = 3 player.velocity_y = 0 player.health = 5 player.jumping = False player.alive = True # define global variables gravity = 0.6 jump_velocity = -10 game_state = "Menu" def draw_game(): # clear screen and draw sky background screen.clear() screen.fill("skyblue") # draw all backgrounds for background in backgrounds: background.draw() # draw all platforms for platform in platforms: platform.draw() # draw all keys for _key in _keys: _key.draw() # draw all hearts for heart in hearts: heart.draw() # draw all obstacles for obstacle in obstacles: obstacle.draw() # draw the player if player.alive: player.draw() health_string = "Health: " + str(player.health) screen.draw.text(health_string,(3*WIDTH/4,0),fontname="atop",fontsize=20,color="darkred") def draw_menu(): # clear screen and draw sky background screen.clear() screen.fill("skyblue") screen.draw.text("Foxy Lady",center=(WIDTH/2,HEIGHT/2),fontname="atop",fontsize=60,color="darkred") screen.draw.text("Press S to Start",center=(WIDTH/2,64 + HEIGHT/2),fontname="atop",fontsize=20,color="darkred") def draw_over(): screen.clear() screen.fill("skyblue") screen.draw.text("Game Over !",center=(WIDTH/2,HEIGHT/2),fontname="atop",fontsize=60,color="darkred") screen.draw.text("Press S to Start",center=(WIDTH/2,64 + HEIGHT/2),fontname="atop",fontsize=20,color="darkred") def draw_win(): screen.clear() screen.fill("skyblue") screen.draw.text("You Win !",center=(WIDTH/2,HEIGHT/2),fontname="atop",fontsize=60,color="darkred") screen.draw.text("Press S to Start",center=(WIDTH/2,64 + HEIGHT/2),fontname="atop",fontsize=20,color="darkred") def draw(): global game_state if game_state == "Menu": draw_menu() elif game_state == "Game": draw_game() elif game_state == "Over": draw_over() elif game_state == "Win": draw_win() def update_game(): global game_state # handle left movement if keyboard.left and player.midleft[0] > 0: player.x -= player.velocity_x # player.image="p_left" player.sprite=fox_walk player.flip_x = True # # handle collision with the platform if player.collidelist(platforms) != -1: # get object that the player collided with object = platforms[player.collidelist(platforms)] #player.velocity_y = 0 player.x += player.velocity_x #player.y = object.y - object.height/2 - player.height/2 # handle right movement elif keyboard.right and player.midright[0] < WIDTH: player.x += player.velocity_x # player.image="p_right" player.sprite = fox_walk player.flip_x = False # handle collision with the platform if player.collidelist(platforms) != -1: # get object that the player collided with object = platforms[player.collidelist(platforms)] #player.velocity_y = 0 player.x -= player.velocity_x #player.y = object.y - object.height/2 - player.height/2 # handle gravity player.y += player.velocity_y player.velocity_y += gravity # handle collision with platforms if player.collidelist(platforms) != -1: # get object that the player collided with object = platforms[player.collidelist(platforms)] # moving down -> hit the ground if player.velocity_y >= 0: player.y = object.y - object.height/2 - player.height/2 player.jumping = False # moving up -> hit their head else: player.y = object.y + object.height/2 + player.height/2 player.velocity_y = 0 # handle collision with the obstacles if player.collidelist(obstacles) != -1: player.alive=False clock.unschedule(reduce_health) music.stop() sounds.hero_dies.play() game_state = "Over" # handle collision with the hearts if player.collidelist(hearts) != -1: # get heart that the player collided with object = hearts[player.collidelist(hearts)] hearts.remove(object) player.health += 1 sounds.health.play() # handle collision with the keys if player.collidelist(_keys) != -1: # get heart that the player collided with object = _keys[player.collidelist(_keys)] _keys.remove(object) if len(_keys) == 0: clock.unschedule(reduce_health) music.stop() sounds.magic.play() game_state = "Win" if player.health <= 0: music.stop() sounds.alarm.stop() sounds.hero_dies.play() game_state = "Over" elif player.health < 3: sounds.alarm.set_volume(0.2) sounds.alarm.play() def update_menu(): global game_state if keyboard.S: reset_game() game_state = "Game" def update_over(): global game_state if keyboard.S: reset_game() game_state = "Game" def update_win(): global game_state if keyboard.S: reset_game() game_state = "Game" def update(): global game_state if game_state == "Game": update_game() elif game_state == "Menu": update_menu() elif game_state == "Over": update_over() elif game_state == "Win": update_win() def on_key_down(key): global game_state if key == keys.SPACE and not player.jumping and game_state == "Game": player.velocity_y = jump_velocity player.jumping = True sounds.jump.set_volume(0.2) sounds.jump.play() def on_key_up(key): global game_state if key == keys.LEFT or key == keys.RIGHT and game_state == "Game": player.sprite = fox_stand def reduce_health(): player.health -= 1 def reset_game(): global _keys, hearts player.bottomleft = (0, HEIGHT - TILE_SIZE) player.velocity_x = 3 player.velocity_y = 0 player.health = 5 player.jumping = False player.alive = True _keys = build("platformer_keys.csv", TILE_SIZE) hearts = build("platformer_hearts.csv", TILE_SIZE) clock.unschedule(reduce_health) clock.schedule_interval(reduce_health, 3) music.play("sneaking_around") pgzrun.go()