如何在玩家健康状况不佳时让我的游戏结束?

时间:2017-04-28 18:54:00

标签: python-3.x

我目前正在为一项作业制作一个简单的流氓式地下城游戏,我对编码很陌生,所以我一直遇到问题。当玩家的生命值达到0时,我希望我的游戏结束,所以我写了这个功能

def main_game():
global room_count
global player_health
welcome_text()

while player_health >= 1:
    room_enter(empty_room, ghost_room, monster_room, exit_room)
else:
    print("Game Over :(")
    print("Number of rooms entered: ", room_count)
    time.sleep(7)
    quit()

无论出于什么原因,当健康状况达到0时,游戏继续进行,我无法弄清楚为什么,它还远没有完成,但是下面是完整的游戏代码:

import time
import random
import sys


player_health = 1
room_count = 1

def scroll_text(s):
    for c in s:
        sys.stdout.write( "%s" % c )
        sys.stdout.flush()
        time.sleep(0.03)

def welcome_text():
    scroll_text("\nYou wake up and find yourself in a dungeon.")
    time.sleep(1.5)
    scroll_text("\nYou have no idea how long you have been trapped for.")
    time.sleep(1.5)
    scroll_text("\nSuddenly, one day, for no apparent reason, your cell door opens.")
    time.sleep(1.5)
    scroll_text("\nEven though weak from having no food, you scramble out as the door closes behind you.")
    time.sleep(1.5)
    scroll_text("\nYou see many paths and many rooms ahead of you.")
    print("\n ")
    print( "-"*20)

def room_enter(empty_room, ghost_room, monster_room, exit_room):
    global player_health
    global room_count
    security = True
    while security == True:
        direction = input("\nYou have the option to go Left (l), Forward (f), or Right (r), which direction would you like to go? ")
        if direction == "l" or direction == "f" or direction == "r":
            security == False
            room_no = random.randint(1,8)
            if room_no == 1 or room_no == 2 or room_no == 3:
                empty_room()
                room_count = room_count + 1
            if room_no == 4 or room_no == 5 or room_no == 6:
                ghost_room()
                room_count = room_count + 1
            if room_no == 7:
                monster_room()
                room_count = room_count + 1
            if room_no == 8:
                exit_room()
                room_count = room_count + 1
        else:
            print("\nInvalid Entry")

    return player_health

def empty_room():
    global player_health
    scroll_text("You enter a cold empty room, doesn't look like anyone has been here in years.")
    player_health = player_health - 1
    print("\n ")
    print("-"*20)
    print("| Your Health: ", player_health, "|")
    print("-"*20)
    print("Number of rooms entered: ", room_count)
    return player_health

def ghost_room():
    global player_health
    scroll_text("You enter a room and feel a ghostly presence around you")
    time.sleep(1)
    scroll_text("\nWithout warning, the ghostly mist surrounding you pools together and the shape of a human-like figure emerges.")
    time.sleep(1)
    scroll_text("\nI AM THE SPIRIT OF AN UNFORTUNATE EXPLORER KILLED IN THEIR PRIME, DOOMED TO SPEND AN ETERNITY TRAPPED IN THIS CHAMBER!")
    time.sleep(1)
    scroll_text("\nANSWER MY QUESTION MORTAL AND CONTINUE IN PEACE, GET IT WRONG HOWEVER AND YOU WILL BE BEATEN!")
    time.sleep(1)

    x = random.randint(1,500)
    y = random.randint(1,500)

    time.sleep(1)

    print("\nTELL ME WHAT", x, "PLUS", y, "IS!")

    okay = False
    while not okay:
        try:
            player_answer = int(input("\nWHAT IS YOUR ANSWER?! "))

            okay = True

            if player_answer == x+y:
                scroll_text("\nCONGRATULATIONS YOU GOT IT CORRECT! HAVE A BIT OF HEALTH!")
                player_health = player_health + 2
            else:
                scroll_text("\nUNFORTUNATELY FOR YOU THAT ANSWER IS WRONG! PREPARE FOR THE BEATING!")
                player_health = player_health - 1
            print("\n ")
            print("-"*20)
            print("| Your Health: ", player_health, "|")
            print("-"*20)
            print("Number of rooms entered: ", room_count)

        except ValueError:
            print("\nInvalid Entry")
    return player_health

def monster_room():
    global player_health
    scroll_text("\nYou hear grunting noises as you enter the room and your worst fears are confirmed when your eyes meet the gaze of a giant monster guarding the other doors.")
    time.sleep(1)
    scroll_text("\nWith no way to fight the creature, you manage to slip around it however as you are making your escape the beast swipes at you and lands a blow to your back.")
    player_health = player_health - 5
    print("\n ")
    print("-"*20)
    print("| Your Health: ", player_health, "|")
    print("-"*20)
    print("Number of rooms entered: ", room_count)
    return player_health

def exit_room():
    global player_health
    scroll_text("\nYou stumble into the room, almost passing out from the lack of food and energy.")
    time.sleep(1)
    scroll_text("\nIs that...")
    time.sleep(1)
    scroll_text("\n... You can't believe your eyes! It's the exit from the dungeon at last!")
    time.sleep(1) 
    print("\nWINNER WINNER CHICKEN DINNER")
    print("\n ")
    print("-"*21)
    print("| Final Health: ", player_health, "|")
    print("-"*21)
    print("Number of rooms entered: ", room_count)
    return player_health

def main_game():
    global room_count
    global player_health
    welcome_text()

    while player_health >= 1:
        room_enter(empty_room, ghost_room, monster_room, exit_room)
    else:
        print("Game Over :(")
        print("Number of rooms entered: ", room_count)
        time.sleep(7)
        quit()

main_game()

2 个答案:

答案 0 :(得分:0)

room_enter中,您有一个拼写错误 - security == False而不是security = False,导致room_enter中出现无限循环,因此永远不会检查main_game中的条件

答案 1 :(得分:0)

非常简单,

if direction == "l" or direction == "f" or direction == "r":
        security == False

你基本上没有将安全性设置为false。它应该是

if direction == "l" or direction == "f" or direction == "r":
        security = False

顺便说一句,如果你声明playerHealth是全局的,那么你不需要在每个函数的末尾都返回它,无论如何都要有当前值。

另外,我认为你不需要“room_enter”上的那些参数,因为它们只是要执行的函数。如果您发现错误声明未声明它们,只需将声明移到函数room_enter

上方

如果有什么不清楚,请告诉我!快乐的编码!