我正在做一个开放世界的Python ASCII艺术游戏(我只得到了字符移动和Map完成),需要帮助清理我的代码

t3psigkw  于 2023-02-26  发布在  Python
关注(0)|答案(1)|浏览(132)

这是代码,我正在尝试做一个几乎没有导入的开放世界ascii游戏,str 1基本上是Map,但被剥离到一行中,以使它更容易复制和粘贴到代码中,忽略str 2,因为它只是cave 2的一个想法(假设是角色购买物品的地方,比如城镇)有一些代码被硬编码,比如小Map功能,也有许多问题,可能是显而易见的,但我真的是新的python,所以随时批评,留下建议或解决方案!

import time
            def Convert(string):
                list1 = []
                list1[:0] = string
                return list1
             #currently unfixed weird problem where list have empty spaces
            # Driver code
            str1 = """----X-----------------------------------------------------------------------------------------------------------------------------------X-------------------------XX---X-------------------------------------------------------------------------------------------------XXXXXX------XXXXXXXXX-----------XXXXXXXXXXXXXXXXXXX------------------------XXXXXXXXXXXXX-----------------------------------------------XXXXXXX         XXXXXXXXXXX   , . , .    XXXXXXXXXXXXXXX----------XXXX    , .     XX--------------------------------------------------X     ,,  , . ,           , .             , .      XXXXXXXXXXXX   , .          XXXXXXXXXXXX----------XXXXXXX-----------------------XX               , . ,           X X , .,    X       , . ,,,                 , .         XXXXXXXXXXX       XXXXXXX------------------XXXX    X              , . , .             , .,            , .     X   , . ,    X       X      X, . , ,    XX------------------------XXX       X      X           X      X       XXXXXXXXXXXXXXX       X       , . ,    ,     X          X    X-----------------------XX   , .,                X                XXX------X-----X--XX                               , . , .     XX----------------------X           XXX   , . ,           X        XX-----X--------XXX    , . ,     X        X    ,              XX--------------------XXX  X      XXXX-XXX         X    , . , . ,   . XXX--------XXX          ,          ,             ,  ,  ,   XXX-----------------XXX X X     XXXX----X--X                X           X X----XXX  , .     X      X      X     , .    X          XX-----------------XX         XXX--------XXX    X   , .    X        ,,,    XXXX             , .,    , ,     ,. X            , .  XX-----------------XX    ,.  XX-X-----X---XXXX           X           X               X             X              XXXXX            X--------------XXX           XXX--------XX      . ,                     , .   ,     ,   ,          XXXXXXXXXXXXXX-----XXXXX     XX-------------XX                 XX----XX        ,    X  , . , X        X     X            X ,.   XX-----X---------X--------XX  X-------------XX        XX   X       XXXX                                  ,         X     , X    XXX----------X----X---X---XXX    XX----X----XX                             XX X    X       X X , . ,  X        X        X          X----------X------X----XX,,  XXX-----X----X       X, .  X      X    X                X                .,..        X         X   XXX-----X---------------XX   XX-----X-------X       ,  .                , . ,       X       X    X      X     X , .         X   X  XX-----------X-------XXX , . X--------------XXX            X    , ., . , XX  X     , . , .     XXXXXX                  ,.          X-----X---X---X-----X       X----------------XXX  , , ,.  X XX                    , , . XXXXXX---XX    , . ,   , .    X           XXX------X-----XXXXX X   X XX-----X----X--------XXX   ,           XXX     X  X    XXXXXXX------XXXXX                      , .         XXXXXXXXXXX     , . ,    XX-------X---XX-------XX     X           X         XXX------XX----X--XXX    X   X   X  X      X      X           X   X              XXX-----X-------------XXX    , . , . ,          X--------X----X----XXX  X                             XXXXXXX       , . , . , . ,   XX---------------------X         , . ,      XXX-----XX-------XXX-XX      XX     X     X    X X    XXX------XXXXX      X   X X X    XX------XXX---------XXXX                 XX------------XX----XXX            XXXX              XXX-----------XXX     , .   , .   ,  XX----------XX-------XXXX  XX X     X  XX-----------XXX----XXX  , ., .  XXXX---XX  X   X       X--------XXX       X         X      X-------------X--------XXX    , . XXXX--------X--------XXX            X--------XX         X    XX-----XX  , .  X    . , X    ,.    X----------X-----X------XX           XX------------XXX         X     X------X---X               XXXXXX   X                   , . X-------------------------XXX     ,   , XX-------XXX  . , .     , , . XXX-----XXX XX   X   X    XXX    XXX   X   X      X    X  XX-XXX------------------------XXX   X       X-----X           X            XXXXX                                    , .             XX-----------------------------XX     , .  XXXXXXX  ,       X   .    , .       X    X            X, . ,    X    , .     XX    X  X--X------------XX----------------XXX  , .         ,. , .      ,. ,                  XXXX   XX                X      X       X     X-----------------------------------XX, . ,   X       , .         ,   . , .   ,    X----XXX-X       X X       , . . ,         ,    X--X---------------------------------XX      ,  .            ,   X              XXX---------XX   , . .   ,               , . ,    X-----------XXX-----------XX-----------XXXXX , . X   , .    X        XX   ,    X-------------X   , . . .     , . ,   , .  , +-+.   X---X--------X-----X-----X----X---X------XXX        , .                      XX--------------XXXX       X      X       X   |♅|     X------------------------------------------XXXXX          XX, .     , .      X------------------X                  , . , .|=|  .   XX---------------------------------------------XXXXXXXXXX     . ,       XXXXXXXXXXXXXXXXX------XXX    , .    X                  XX---------------------------------------------------------XXXXX     XXXXX-X---------------XX-XXX-XXXXXXXX          , .     XXXXXXXXXX------------------------------------------------------------XXXXX------------------------------XXXXXXXXXXXXXXXXXXXXXXXXX----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"""
            str2 = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                             XX       ❁         ❁           XX                             XX       XXXXXXXXXXXXXXX   ❁   XX   ❁   X     -♅-     X       XX       X      o      X       XX ❁     X             X  ❁    XX       X  o       o  X       XX       X  o       o  X     ❁ XX    ❁  X  o       o  X ❁     XX       XXXXXX---XXXXXX       XX____________|---|____________XX            |---|            XX____________|---|____________XX            |---|            XX____________|---|____________XX            |---|            XX____________|---|____________XX            |---|            XXXXXXXXXXXXXXX\-/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            UncollidableObject = ["X", "#", "+", "|", "-", "♅"]
            tpobject = ["="]
            inventory = ["test_slot"]
            #make sure the map's code is stripped to one line, or else it will print \n
            myworld = Convert(str1)
            temple = Convert(str2)
            nline = 130
            #number of characters in one line
            #uses this structure

            #  1   2   3   4

            #  5   6   7   8

            #  9  10  11  12

            # 13  14  15  16

            #analogy of how the program works
            #however there is a large flaw is that when the user goes OUT of the map, the location is less predictable
            #this is solved by creating barriers around the map that the user cannot escape from to ensure the stability of the game
            location = 5186
            cave = 1
            #could be anywhere (as long as not out of map)
            oldlocation = location
            count1 = 0
            count2 = 0
            minicount1 = 0
            minicount2 = 0
            y = ""
            oldcharactercount = 0
            worldunit = myworld[0]
            myworld.pop(location)
            myworld.insert(location, " ")
            oldcharacter = myworld[location]
            startscreen = """    _                ___       _.--.
                \`.|\..----...-'`   `-._.-'_.-'`
                /  ' `         ,       __.--'
                )/' _/     \   `-_,   /
                `-'" `"\_  ,_.-;_.-\_ ',     START SCREEN
                    _.-'_./   {_.'   ; /
                   {_.-``-'         {_/"""
            def loadworld(count1, count2, myworld, location):
                worldstring = ""
                worldunit = myworld[0]
                print("\n" * 15)
                for i in range(len(myworld)):
                    worldunit = myworld[count2]
                    count2 = count2 + 1
                    count1 = count1 + 1
                    worldstring = worldstring + worldunit
                    if count1 == nline:#(number of characters in one line)
                         count1 = 0
                         worldstring + "\n"
                         print(worldstring)
                         worldstring = ""

            def minimap(myworld, location, minicount1, minicount2):
                print("\n" * 45)
                print("\n" * 45)
                print(location)
                minimapl = []
                count3 = 0
                count4 = 0
                count3 = -524
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                count3 = -394
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                count3 = -264
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                count3 = -134
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                count3 = -4
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                count3 = 126
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                count3 = 256
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                count3 = 386
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                count3 = 516
                for i in range(9):
                    minimapl.insert(count4,  myworld[location + count3])
                    count3 = count3 + 1
                    count4 = count4 + 1
                miniunit = minimapl[0]
                ministring = ""
                for i in range(len(minimapl)):
                    miniunit = minimapl[minicount2]
                    minicount2 = minicount2 + 1
                    minicount1 = minicount1 + 1
                    ministring = ministring + " " + miniunit
                    if minicount1 == 9:#nline
                        minicount1 = 0
                        ministring + "\n"
                        print(ministring)
                        ministring = ""
                print("\n" * 10)
            print("\n" * 45)
            print("\n" * 45)
            print("The ASCII Open World Game (In Development)")
            input("Press [ENTER] to continue")
            print("\n" * 45)
            print("\n" * 45)
            print("Your Vision is Limited, but you can always use your map to locate yourself (this feature might be removed after testing)")
            input("Press [ENTER] to continue")
            print("\n" * 45)
            print("\n" * 45)
            print("IT IS RECOMMENDED TO USE TERMINAL TO PLAY THE GAME, CURRENT PROGRESS LIMITED TO ONLY CHARACTER MOVEMENT")
            input("Press [ENTER] to continue")
            print("\n" * 45)
            print("\n" * 45)
            while True:
                print("\n" * 45)
                print("\n" * 45)
                print(startscreen)
                startgame = input("Click [ENTER] to play")
                minimap(myworld, location, minicount1, minicount2)
                while cave == 1:
                    direction = input("Command or (i)nformation: ")
                    if direction == "w":
                        location = location - nline
                        if myworld[location] in UncollidableObject:   
                            location = location + nline
                            #loadworld(count1, count2, myworld, location)
                            minimap(myworld, location, minicount1, minicount2)
                        elif myworld[location] in tpobject:
                            cave = 2
                            
                        else:
                            myworld.pop(oldlocation)
                            myworld.insert(oldlocation, oldcharacter)
                            oldcharacter = myworld[location]
                            myworld.pop(location)
                            myworld.insert(location, "@")
                            #loadworld(count1, count2, myworld, location)
                            minimap(myworld, location, minicount1, minicount2)
                            oldlocation = location
                    elif direction == "a":
                        location = location - 1
                        if myworld[location] in UncollidableObject:   
                            location = location + 1
                            #loadworld(count1, count2, myworld, location)
                            minimap(myworld, location, minicount1, minicount2)
                        elif myworld[location] in tpobject:
                            cave = 2
                        else:
                            myworld.pop(oldlocation)
                            myworld.insert(oldlocation, oldcharacter)
                            oldcharacter = myworld[location]
                            myworld.pop(location)
                            myworld.insert(location, "@")
                            #loadworld(count1, count2, myworld, location)
                            minimap(myworld, location, minicount1, minicount2)
                            oldlocation = location
                    elif direction == "s":
                        location = location + nline
                        if myworld[location] in UncollidableObject:   
                            location = location - nline
                            #loadworld(count1, count2, myworld, location)
                            minimap(myworld, location, minicount1, minicount2)
                        elif myworld[location] in tpobject:
                            cave = 2
                        else:
                            myworld.pop(oldlocation)
                            myworld.insert(oldlocation, oldcharacter)
                            oldcharacter = myworld[location]
                            myworld.pop(location)
                            myworld.insert(location, "@")
                            #loadworld(count1, count2, myworld, location)
                            minimap(myworld, location, minicount1, minicount2)
                            oldlocation = location
                    elif direction == "d":
                        location = location + 1
                        if myworld[location] in UncollidableObject:   
                            location = location - 1
                            #loadworld(count1, count2, myworld, location)
                            minimap(myworld, location, minicount1, minicount2)
                        elif myworld[location] in tpobject:
                            cave = 2
                        else:
                            myworld.pop(oldlocation)
                            myworld.insert(oldlocation, oldcharacter)
                            oldcharacter = myworld[location]
                            myworld.pop(location)
                            myworld.insert(location, "@")
                            #loadworld(count1, count2, myworld, location)
                            minimap(myworld, location, minicount1, minicount2)
                            oldlocation = location
                    elif direction == "i":
                        print("Information Tab:")
                        print("(w)(a)(s)(d) for movements")
                        print("(m) for map")
                        print("(p) for pocket/inventory")
                    elif direction == "m":
                        loadworld(count1, count2, myworld, location)
                    elif direction == "p":
                        print("Inventory: ", inventory)
                    else:
                        minimap(myworld, location, minicount1, minicount2)
                if cave == 2:
                    #something more for user to explore!
91zkwejq

91zkwejq1#

在定义str1的开始附近,您应该使用单引号或双引号' or ",这样peole就不会认为您正在定义多行字符串

相关问题