0

I have basically created a "Maze" game in pygame (or at least tried to). The game has 3 levels with a game sprite and collectable stars that add to the "score" of the game. The player also has 3 lives, and once the player collides with a wall, they lose a life and the game resets their position for that level. Right now i'm focussing on projecting level 1. But theres just one problem. On the intro screen, I made an on_key_down function where "once the ENTER key is pressed, the intro screen shoud switch to the first level", but my code only displays the intro screen and when you press the ENTER key, nothing happens. Can someone help me with this?

Here is my code:

import pgzrun
from pgzhelper import *


WIDTH = 800
HEIGHT = 600

#Task 3: Create game sprite
player = Actor('player.png', (20,20))

score = 0
lives = 3
level = 1
level_complete = False
end_rect = Rect((700, 500), (50, 50))
game_state = "show_intro"

def reset_player():
    global player
    player.pos = (20,20)

def reset_player_begining():
    global player
    global lives
    global score
    global level_complete
    lives = 3
    score = 0
    level_complete = False
    player.pos = (20,20)


    
#Task 1: Create Introduction screen with “let's play” button and Instructions on how to play the game
intro_text = """
Welcome to the Maze Game!

Instructions:
Use the arrow keys to move the player sprite through the maze.
Avoid touching the walls, or you'll lose a life.
Collect all the stars to complete the level

Good luck, and have fun!

""" 
show_intro = True

def draw_intro():
    screen.clear()
    screen.draw.text(intro_text, (50,50), color='white', fontsize=30)
    screen.draw.text("Press Enter to start playing", (250,300), color='white', fontsize=30)

def on_key_down_intro(key):
    global game_state
    if key == keys.RETURN:
        game_state = "playing"
        reset_player_begining()


#Task 2: Create "Game Over" Screen
game_over_text = """
Game Over!

You have run out of lives.
Press Enter to restart the game.
"""

show_game_over = False

def draw_game_over():
    screen.clear()
    screen.draw.text(game_over_text, (50,50), color='white', fontsize=30)

def on_key_down_game_over(key):
    global game_state
    if key == keys.RETURN:
        lose_life()
        game_state = "show_intro"
        

#Create Win Screen and functions
win_text = """
Congragulations!!!!!

You have finished the last level and completed all the stars.
If you want to play again, simply press the "replay" button to go
back to the begining to play again.

Thanks for playing my maze game!!!!!!

"""

show_win = False

def draw_win():
    screen.clear()
    screen.draw.text(win_text, (50,50), color='white', fontsize=30)

def on_key_down_win(key):
    global game_state
    if key == keys.RETURN:
        game_state = "show_intro"


#Task 4: Create maze walls
class MazeWalls:
    maze_walls_level_1 = [
        Rect((80, 80), (80, 320)),
        Rect((80, 80), (480, 80)),
        Rect((480, 80), (80, 320)),
        Rect((80, 320), (480, 80)),
        Rect((240,160), (80, 160)),
        Rect((750,0), (50, 60)),
        Rect((100, 100), (600, 10)),
        Rect((690, 100), (10, 200)),
        Rect((0, 0), (800, 50)),
        Rect((0, 550), (800, 50)),
        Rect((0, 0), (50, 600)),
        Rect((750, 0), (50, 600)),
        end_rect
        ]
    
    maze_walls_level_2 = [
        Rect((80, 80), (80, 320)),
        Rect((80, 80), (480, 80)),
        Rect((480, 80), (80, 320)),
        Rect((80, 320), (480, 80)),
        Rect((240,160), (80, 160)),
        Rect((750,0), (50, 60)),
        Rect((100, 100), (600, 10)),
        Rect((690, 100), (10, 200)),
        Rect((0, 0), (800, 50)),
        Rect((0, 550), (800, 50)),
        Rect((0, 0), (50, 600)),
        Rect((750, 0), (50, 600)),
        Rect((100, 400), (200, 10)),
        Rect((500, 400), (200, 10)),
        Rect((300, 200), (200, 10)),
        end_rect
        ]

    maze_walls_level_3 = [
        Rect((80, 80), (80, 320)),
        Rect((80, 80), (480, 80)),
        Rect((480, 80), (80, 320)),
        Rect((80, 320), (480, 80)),
        Rect((240,160), (80, 160)),
        Rect((750,0), (50, 60)),
        Rect((100, 100), (600, 10)),
        Rect((690, 100), (10, 200)),
        Rect((0, 0), (800, 50)),
        Rect((0, 550), (800, 50)),
        Rect((0, 0), (50, 600)),
        Rect((750, 0), (50, 600)),
        Rect((100, 400), (200, 10)),
        Rect((500, 400), (200, 10)),
        Rect((300, 200), (200, 10)),
        Rect((100, 50), (200, 10)),
        Rect((500, 250), (200, 10)),
        Rect((300, 100), (200, 10)),
        end_rect
        ]

#Stars
class stars:
    stars_level_1 = [
        Actor('star.png',(240,240)),
        Actor('star.png',(400,400)),
        ]
    
def draw_stars(level):
    for wall in MazeWalls.maze_walls_level_1:
        wall.draw()
        for star in stars[levels]:
            star.draw()


        
def lose_life():
    global lives
    lives -= 1
    if lives == 0:
        show_game_over = True
        reset_player_begining()
        
def draw_game():
    global score
    global lives
    screen.clear()
    screen.draw.text("Score: " + str(score), (10,10), color='white')
    screen.draw.text("Lives: " + str(lives), (20,10), color='white')
    player.draw()
    for wall in MazeWalls.maze_walls_level_1:
        screen.draw.filled_rect(wall, (0, 0, 225))
        
def draw():
    global show_intro
    global show_game_over
    global show_win
    global game_state

    if show_intro:
        draw_intro()
        return

    elif show_game_over:
        draw_game_over()
        return
    
    elif show_win:
        draw_win()
        
    elif game_state == "playing":
        draw_game()
   
               
pgzrun.go()
  • For questions about [Pygame Zero](https://pygame-zero.readthedocs.io/en/stable/) you must use the tag [tag:pgzero] – Rabbid76 May 02 '23 at 20:29

0 Answers0