You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyScript在HTML中运行Python游戏报错求助

PyScript运行自制选择型冒险游戏报错排查

我尝试使用PyScript在HTML中运行一款自制的选择型冒险Python游戏,该游戏在本地终端中可正常运行,但在浏览器中运行时出现报错,输入任意内容后仍会触发错误。目前游戏尚未完成,恳请协助排查报错问题。


HTML代码

<html>
    <head>
        <title>
            Anthony's Python Video Game
        </title>
        <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    </head>
    <body> 
            <py-script src = "VideoGame.py">
            </py-script>
            
    </body>
</html>

启动命令

python -m http.server 5050

注:原命令中的port为冗余参数,正确命令如上

Python游戏代码

print('This is my very first video game, this is a choose your own adventure game.')
print('The objective of this game is to get to the end of the longstreet road without dying.')
print('There will be decisions on the path that will be quite litteraly life or death, please choose coutiosly to survive.')
print('click i to check inventory or q to quit at any time')



#START

begin = input('Would you like to begin ')

def NO():
    print ('Please follow the rules or there will be consequences ://///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////')
    quit()

def STOP():
    print ("If you see anything out of the ordinary please do not stop and continue on the pathway ://///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////")
    quit()

def WENT_BACK_2():
    print('You instantly hear a loud bang and feel a pain in your forehead right before you feel everything go empty and drop to the ground dead.')
    print('...')
    NO()

def WENT_BACK_1():
    print ('You start back home, and relize that it feels like you have been walkning for a while, you look down at your watch and relize it has been 30 minutes and you feel like you are getting nowhere, you start to sprint back and you black out.')
    print ('...')
    print ('You wake up with a tattoo on your right arm, it reads. "Cannot go this way please dont'+ "'t" + ' disobey again, thank you ://///////////////////"')
    choose_to_go_back = 1

def inventory():
    if coins >=1:
        print ('You have', str(coins), 'coins')
    elif torch >=1:
        print ('You have', str(torch), 'torch')
    elif knife >=1:
        print ('You have', str(knife), 'knife')
    else:
        print('You have nothing in your inventory')

choose_to_go_back = 0
coins = 0
torch = 0
knife = 0
''''

elif line == 'q'.lower():
    quit()
elif line == 'i'.lower():
    inventory()
    continue
    
'''

''''

elif f == 'b'.lower() and choose_to_go_back == 0:
    WENT_BACK_1
    break
elif f == 'b'.lower() and choose_to_go_back == 1:
    WENT_BACK_2

'''


if begin == ('yes').lower() or begin == ('y').lower():
#SIGN
    while True:
        sign = input('You run into a sign that read rules before starting your quest, would you like to read ')
        line = (sign)
        if sign == 'yes'.lower() or sign == 'y'.lower():
            print('Hello, the fist rule is to read the rules ,if you disobey any of these rules there will be consequences, you are about to start down a journey with not many designated stops, please do not attempt to go backwards,')
            print('if you see anything out of the ordinary please do not stop and continue on the pathway, and DO NOT go into the woods, start on this journey on you own risk. We are not liable to any damages done to you or to your body. Thank you and good luck.')
            break
        elif line == 'q'.lower():
            quit()
        elif line == 'i'.lower():
            inventory()
            continue
        else:
            print('First rule states, "the fist rule is to read the rules ,if you disobey any of these rules there will be consequences"')
            print('://///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////')
            quit()
#
#
    
    print ('...')
    print ('You walk onto the street and everyting feels fine')

#FIRST DESISION TO GO BACK 
    while True:
        walk = input ('Would you like to continue(c) or walk back to your house(b) ')
        line = (walk)
        if walk == ('c').lower():
            break
        elif line == 'q'.lower():
            quit()
        elif line == 'i'.lower():
            inventory()
            continue
        elif walk == ('b').lower():
            WENT_BACK_1()
            while True:
                a = input('Try runing back again (b) or continue (c) ')
                line = (a)
                if a == ('b').lower():
                    WENT_BACK_2()
                elif line == 'q'.lower():
                    quit()
                elif line == 'i'.lower():
                    inventory()
                    continue
                elif a == 'c'.lower():
                    break
                else:
                    NO()
            break
        else: 
            NO()
    
#

#
    print('You walk along the road for a good 15 miuntest, you then see a split in the road.')
#

#SPLIT IN THE ROAD READ SIGN   
    while True:
        sign2 = input('Left has a sign and right does not, would you like to read the sign ')
        line = (sign2)
        if sign2 == 'y'.lower() or sign2 == 'yes'.lower():
            print ('DO NOT GO THIS WAY')
            break
        elif line == 'q'.lower():
            quit()
        elif line == 'i'.lower():
            inventory()
            continue
        else:
            break
#

#WENT RIGHT
    while True:    
        b = input('Would you like to go right(r) or left(l) ')
        line = (b)
        if b == 'r'.lower():
            print('You continue going the way that is intended')
            print('You go down the path for about ten miuntes and come across a man standing and staring blankly into space')
            while True:
                g  = input('Would you like to talk with the man or(t) or continue down the path(c) ')
                line = (g)
                if g == 't'.lower():
                    print('The man starts to speak')
                    print('"It is too late, it'+ "'" + 's a trap, go back, go back, go back, go back, go back, go ba-k-kkc-kc"')
                    print('Foam starts to acumulate in the mans mouth and he drops to the floor dead.')
                    while True:
                        f = input('Would you like to seach his body (s) go back (b) or coninue(c) ')
                        line = (f)
                        if f == 's'.lower():
                            print ('You found 5 boldtown coins')
                            coins = 5
                            print('And you continue on the path')
                            break
                        elif line == 'q'.lower():
                            quit()
                        elif line == 'i'.lower():
                            inventory()
                            continue    
                        elif f == 'b'.lower() and choose_to_go_back == 0:
                            WENT_BACK_1
                            break
                        elif f == 'b'.lower() and choose_to_go_back == 1:
                            WENT_BACK_2
                        elif f == 'c':
                            break
                    break
                elif line == 'q'.lower():
                    quit()
                elif line == 'i'.lower():
                    inventory()
                    continue
                elif g == 'c'.lower():
                    break
            break

#

#WENT LEFT    
        elif line == 'q'.lower():
            quit()
        elif line == 'i'.lower():
            inventory()
            continue     
        elif b == 'l'.lower():
            print('You continue left, good luck')
            print('...')
            print('After a few hours of walknig you decide it is time that you should stop')
            print('You decide to stop and rest. You set up a little camp ')
            print('...')
            print('You fall asleep')
            print('...')
            while True:
                d = input('You here a noise late into the night, do you decide to wake up ')
                line = (d)
                if d == 'y'.lower() or d == 'yes'.lower():
                    while True:
                        print('You notice a pair of eyes in the trees around you')
                        e = input ('Would you like to check it out(c) or walk away and forget about it(w) ')
                        line = (e)
                        if e == 'c'.lower():
                            STOP()
                        elif e == 'w'.lower():
                            break
                        elif line == 'q'.lower():
                            quit()
                        elif line == 'i'.lower():
                            inventory()
                            continue
                        else:
                            NO()
                    break
                elif line == 'q'.lower():
                    quit()
                elif line == 'i'.lower():
                    inventory()
                    continue
                else:
                    NO()
            break
        else:
            NO()
                
        
#    
    print('...')
    print('The path you decided to go eventually connects back at the end ')
    print('After another hour of walking on what seems like the endless path of nothing but trees, you come upon a village with a sign that labels it to be boldtown village')
    print('...')
    
    
#TOWN    
    while True:
        h = input('Would you like to visit the town, they may have resouses ')
        line = (h)
        if h ==  'y'.lower() or h == 'yes'.lower():
            print ('You see the village people and the village people see you, they all wave hi at the same time stoping what they are doing as if you are the fist person that you have ever seen before')
            print ('You need sleep, go to the hotel after you are done with your activity')
            while True:
                i = input('Would you like to go to the shop(s) or skip and go to the hotel(h) ')
                line = (i)
                if i == 's'.lower() and coins >= 1:
                    while True:
                        inventory()
                        j = input('You can by a torch for 2 coins(t) and/or a knife for 3 coins(k) click(e) to exit ')
                        line = (j)
                        if j == 't'.lower() and coins >= 2:
                            print('You got a torch')
                            torch = 1
                            coins -= 2
                            continue
                        elif j == 't'.lower() and coins <= 1:
                            print("You don't have enough coins")
                            continue
                        elif j == 'k'.lower() and coins >= 3:
                            print('You got a knife')
                            knife = 1
                            knife -= 3
                            continue
                        elif j == 'k'.lower() and coins <= 2:
                            print("You don't have enough coins")
                            continue
                        elif j == 'e'.lower():
                            break
                        elif line == 'q'.lower():
                            quit()
                        elif line == 'i'.lower():
                            inventory()
                            continue
                        else:
                            NO()
                    continue
                elif i == 's'.lower() and coins <= 1:
                    print('You do not have enough coins')
                    continue
                elif line == 'q'.lower():
                            quit()
                elif line == 'i'.lower():
                    inventory()
                    continue
                elif i == 'h'.lower():
                    print('You went to the hotel to get some sleep')
                    print('You talk to the hotel manager and he tells you that you will be able to have a free night at the hotel')
                    print("You accept the free night at the hotel. He tells you you need to go outside and your room is in the home next to this biulding")
                    print('He insists that he walks you over to the room')
                    while True:
                        k = input('Would you like him to come with you ')
                        line = k
                        if k == 'y'.lower() or k == 'yes'.lower():
                            print('He lights a candle and says. You never want to be in the dark around here. Something might come for you')
                            print ('He walks you to your room and you go to sleep')
                            break
                        elif line == 'q'.lower():
                            quit()
                        elif line == 'i'.lower():
                            inventory()
                            continue
                        else:
                            while True:
                                if torch >= 1:
                                    j = input('Would you like to light your torch ')
                                    line = (j)
                                    if j == 'y'.lower() or i == 'yes'.lower():
                                        torch -=1
                                        print ('You walk to your room and go to sleep')
                                        break
                                    elif line == 'q'.lower():
                                        quit()
                                    elif line == 'i'.lower():
                                        inventory()
                                        continue
                                    else:
                                        print('You walk to your room in the dark, you can not see anything around you. You here a swoosh in the dark and drop to the ground dead')
                                        NO()
                                else:
                                    print('You walk to your room in the dark, you can not see anything around you. You here a swoosh in the dark and drop to the ground dead')
                                    NO()
                            break
                    break
            break        
        elif line == 'q'.lower():
            quit()
        elif line == 'i'.lower():
            inventory()
            continue
        else:
            print('You walk for 15 miunets and relize the path looks the same')
            print('...')
            print('You come upon a village with a sign that labels it to be boldtown village')
            continue
         
# Day 2 in village

else:
    print('Maybe you will play next time, might have fun ://///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////')
    quit()

内容的提问来源于stack exchange,提问作者NotButt3r.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 09:55:24