Home > Net >  Loop is breaking before Modifying player position in python
Loop is breaking before Modifying player position in python

Time:12-31

I have a small maze game in python and the end is defined by 2 or G and when you get to the end you win but I want to actually show the player that they went onto the G and it Turns into a D for done and then the loop breaks but the loop breaks before thus happens and I tried making it work with a var called at_goal but its still no working, can someone help?

Code:

import random
import time
maze = [[1, 1, 1, 1, 1, 1],
        [1, 0, 1, 1, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 0, 0, 0, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 1, 1, 0, 1, 1],
        [1, 2, 1, 0, 1, 1],
        [1, 0, 0, 0, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 1, 1, 1, 1, 1]]

player_x = 1
player_y = 1

at_goal = False

done = False
while done == False:
    print("\033[H\033[2J", end="")
    for y, row in enumerate(maze):
        for x, element in enumerate(row):
            if x == player_x and y == player_y:
                if at_goal:
                    print("D", end=" ")
                else:
                    print("P", end=" ")
            elif element == 1:
                print("#", end=" ")
            elif element == 2:
                print("G", end=" ")
            else:
                print(" ", end=" ")
        print()
    move = input("Enter a move (up, down, left, right): ")
    if move == "up":
        player_y -= 1 
    elif move == "down":
        player_y  = 1
    elif move == "left":
        player_x -= 1
    elif move == "right":
        player_x  = 1
    else:
        print("Invalid move")
        time.sleep(0.5)
        continue
    
    if maze[player_y][player_x] == 2:
        at_goal = True
    elif maze[player_y][player_x] == 1:
        print("You have hit a wall and lost the game. Try again.")
        player_x = 1
        player_y = 1
        at_goal = False
        time.sleep(0.5)
    if at_goal:
        print("You have reached the goal and won the game!")
        done = True

Thanks In advance.

CodePudding user response:

The problem in your code is that you're using at_goal to check if the player has reached or not but you're not using *at_goal to update the maze itself. Try this solution.

import random
import time

maze = [[1, 1, 1, 1, 1, 1],
        [1, 0, 1, 1, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 0, 0, 0, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 1, 1, 0, 1, 1],
        [1, 2, 1, 0, 1, 1],
        [1, 0, 0, 0, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 1, 1, 1, 1, 1]]

player_x = 1
player_y = 1

done = False
while done == False:
    print("\033[H\033[2J", end="")
    for y, row in enumerate(maze):
        for x, element in enumerate(row):
            if x == player_x and y == player_y:
                print("P", end=" ")
            elif element == 1:
                print("#", end=" ")
            elif element == 2:
                print("G", end=" ")
            elif element == 3:
                print("D", end=" ")
            else:
                print(" ", end=" ")
        print()
    move = input("Enter a move (up, down, left, right): ")
    if move == "up":
        player_y -= 1 
    elif move == "down":
        player_y  = 1
    elif move == "left":
        player_x -= 1
    elif move == "right":
        player_x  = 1
    else:
        print("Invalid move")
        time.sleep(0.5)
        continue
    
    if maze[player_y][player_x] == 2:
        maze[player_y][player_x] = 3  # update the maze to reflect that the player has reached the goal
        print("You have reached the goal and won the game!")
        done = True
    elif maze[player_y][player_x] == 1:
        print("You have hit a wall and lost the game. Try again.")
        player_x = 1
        player_y = 1
        time.sleep(0.5)

CodePudding user response:

the solution for this problem is very simple. Your while loop stops before the last print that you need. To solve that you can move the code to show the table to a function, and then call the function at the end of the game. The final logic looks like this:

import random
import time
maze = [[1, 1, 1, 1, 1, 1],
        [1, 0, 1, 1, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 0, 0, 0, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 1, 1, 0, 1, 1],
        [1, 2, 1, 0, 1, 1],
        [1, 0, 0, 0, 1, 1],
        [1, 0, 1, 0, 1, 1],
        [1, 1, 1, 1, 1, 1]]

player_x = 1
player_y = 1

at_goal = False

def print_table():
    print("\033[H\033[2J", end="")
    for y, row in enumerate(maze):
        for x, element in enumerate(row):
            if x == player_x and y == player_y:
                if at_goal:
                    print("D", end=" ")
                else:
                    print("P", end=" ")
            elif element == 1:
                print("#", end=" ")
            elif element == 2:
                print("G", end=" ")
            else:
                print(" ", end=" ")
        print()

done = False
while done == False:
    
    print_table()
    move = input("Enter a move (up, down, left, right): ")
    if move == "up":
        player_y -= 1 
    elif move == "down":
        player_y  = 1
    elif move == "left":
        player_x -= 1
    elif move == "right":
        player_x  = 1
    else:
        print("Invalid move")
        time.sleep(0.5)
        continue
    
    if maze[player_y][player_x] == 2:
        at_goal = True
    elif maze[player_y][player_x] == 1:
        print("You have hit a wall and lost the game. Try again.")
        player_x = 1
        player_y = 1
        at_goal = False
        time.sleep(0.5)
    if at_goal:
        print_table()
        print("You have reached the goal and won the game!")
        done = True
  • Related