精华内容
下载资源
问答
  • 1: print("游戏结束") break else: map_data[x][y],map_data[x][y+1] = map_data[x][y+1], map_data[x][y] # 进行交换操作 print_map() # 当用户输入s时执行向下走进行交换(列不变行变 行下标加1) elif order == ...

    # 画地图

    map_data = [

    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

    [1, 0, 1, 1, 1, 1, 1, 1, 1, 1],

    [1, 2, 1, 0, 0, 0, 0, 0, 0, 1],

    [1, 0, 1, 0, 1, 0, 1, 1, 0, 1],

    [1, 0, 1, 0, 1, 0, 1, 1, 0, 1],

    [1, 0, 0, 0, 1, 0, 0, 1, 0, 1],

    [1, 1, 1, 0, 1, 1, 1, 1, 0, 1],

    [1, 1, 1, 0, 1, 1, 1, 1, 0, 0],

    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

    ]

    # 定义人所在的位置(初始化)

    x = 2

    y = 1

    endx = 7

    endy = 9

    # 用字符串重新给地图赋值

    def print_map():

    for nums in map_data:

    for num in nums:

    if num == 1:

    print(" #",end=" ")

    elif(num == 0):

    print(" ",end=" ")

    else:

    print(" $",end=" ")

    print("")

    # 所用到的核心知识

    # print("交换前的地图")

    # print_map()

    # map_data[2][1], map_data[2+1][1] = map_data[2+1][1], map_data[2][1]

    # print("交换后的地图")

    # print_map()

    # 先画地图

    print_map()

    while True:

    # 指令的输入

    order = input("请输入指令(a: 左,s: 下, d: 右, w: 上):")

    # 对用户输入的指令进行判断

    # 当用户输入a时执行向左走进行交换(列变行不变 列下标减1)

    if order == "a":

    y = y-1

    # 碰到墙,游戏结束

    if map_data[x][y] == 1:

    print("游戏结束")

    break

    else:

    map_data[x][y],map_data[x][y+1] = map_data[x][y+1], map_data[x][y] # 进行交换操作

    print_map()

    # 当用户输入s时执行向下走进行交换(列不变行变 行下标加1)

    elif order == "s":

    x = x + 1

    if map_data[x][y] == 1:

    print("游戏结束")

    break

    else:

    map_data[x][y], map_data[x-1][y] = map_data[x-1][y], map_data[x][y] # 进行交换操作

    print_map()

    # 当用户输入d时执行向右走进行交换(列变行不变 列下标加1)

    elif order == "d":

    y = y + 1

    if map_data[x][y] == 1:

    print("游戏结束")

    break

    else:

    map_data[x][y], map_data[x][y - 1] = map_data[x][y - 1], map_data[x][y] # 进行交换操作

    print_map()

    if map_data[x][y] == map_data[endx][endy]:

    print("恭喜你过关了")

    break

    # 当用户输入w时执行向上走进行交换(列不变行变 行下标减1)

    elif order == "w":

    x = x - 1

    if map_data[x][y] == 1:

    print("游戏结束")

    break

    else:

    map_data[x][y], map_data[x + 1][y] = map_data[x + 1][y], map_data[x][y] # 进行交换操作

    print_map()

    # 当用户输入非规则内的指令时的错误提示,并重新输入

    else:

    print("您输入指令有误,请重新按指令规则输入!")

    continue

    #

    截图:

    1302991-20180404212346847-1502890690.png

    展开全文
  • 我有一个迷宫游戏,也是一个语言词汇游戏。除了一件事之外,一切都像是应该的。当玩家滚过“黄金”并弹出问题时,for循环会不断迭代我的词典中针对该语言的所有问题。我希望它只询问一个,允许玩家移动,并在玩家...

    我使用的是Python 3,而且我非常非常缺乏经验,所以请善待。我有一个迷宫游戏,也是一个语言词汇游戏。除了一件事之外,一切都像是应该的。当玩家滚过“黄金”并弹出问题时,for循环会不断迭代我的词典中针对该语言的所有问题。我希望它只询问一个,允许玩家移动,并在玩家滚动另一个“黄金”以获得下一个问题时询问另一个玩家。for我引用的循环在这里:# Create function to get questions and answers.

    def getQuestions(language):

    for qa in dic[language]:

    q = qa['question']

    a = qa['answer']

    ans = simpledialog.askstring("Input", q, parent=root)

    if ans == a:

    print("Great job!")

    else:

    print("The correct answer was...", a)

    screen.listen()

    我不知道该怎么办这个问题。我在下面发布了我的游戏代码。请让我知道如何解决这个问题,或者是否有更好的方法来解决这个问题。from turtle import RawTurtle, TurtleScreen

    import tkinter

    from tkinter import PhotoImage, simpledialog

    import random

    largefont = ("Verdana", 12)

    # Define function to create Spanish level.

    def span():

    pen.setup(level_1)

    player.setup(level_1)

    setup(level_1)

    # Define function to create French level.

    def fren():

    pen.setup(level_2)

    player.setup(level_2)

    setup(level_2)

    # Define function to create Japanese level.

    def jpn():

    pen.setup(level_3)

    player.setup(level_3)

    setup(level_3)

    # Define function to create Turkish level.

    def turk():

    pen.setup(level_4)

    player.setup(level_4)

    setup(level_4)

    # Create dictionary.

    dic = {'spanish': [

    {'question': 'What is morado/a?', 'answer': 'purple'},

    {'question': 'What is blanco/a?', 'answer': 'white'},

    {'question': 'What is el durazno?', 'answer': 'peach'},

    {'question': 'What is la granada?', 'answer': 'pomegranate'},

    {'question': 'What is la galleta?', 'answer': 'cookie'},

    {'question': 'What is el bistec?', 'answer': 'steak'},

    {'question': 'What is el relámpago?', 'answer': 'lightning'},

    {'question': 'What is el amanecer?', 'answer': 'sunrise'}

    ],

    'french': [

    {'question': 'What is le arc en ciel?', 'answer': 'rainbow'},

    {'question': 'What is la glace?', 'answer': 'ice'},

    {'question': 'What is la cascade?', 'answer': 'waterfall'},

    {'question': 'What is le marais?', 'answer': 'swamp'},

    {'question': 'What is le feu?', 'answer': 'fire'},

    {'question': 'What is la cuillère?', 'answer': 'spoon'},

    {'question': 'What is la bouche?', 'answer': 'mouth'},

    {'question': 'What is le dos?', 'answer': 'back'},

    {'question': 'What is le visage?', 'answer': 'face'},

    {'question': 'What is le chat?', 'answer': 'cat'}

    ],

    'japanese': [

    {'question': 'What is ペンギン?', 'answer': 'penguin'},

    {'question': 'What is 甲虫?', 'answer': 'beetle'},

    {'question': 'What is 蝶々?', 'answer': 'butterfly'},

    {'question': 'What is お兄さん?', 'answer': 'older brother'},

    {'question': 'What is ふうふ?', 'answer': 'married couple'},

    {'question': 'What is 今日は?', 'answer': 'hello'},

    {'question': 'What is 元気です。?', 'answer': 'I am good.'},

    {'question': 'What is わくわくしています。?', 'answer': 'I am excited.'},

    {'question': 'What is 恥ずかしいです。?', 'answer': 'I am embarrassed.'},

    {'question': 'What is 誰??', 'answer': 'Who?'},

    {'question': 'What is 何??', 'answer': 'What?'}

    ],

    'turkish': [

    {'question': 'What is nasıl??', 'answer': 'How?'},

    {'question': 'What is Banyo nerede??', 'answer': 'Where is the bathroom?'},

    {'question': 'What is kitaplık?', 'answer': 'library'},

    {'question': 'What is piyasa?', 'answer': 'market'},

    {'question': 'What is plaj?', 'answer': 'beach'},

    {'question': 'What is fırıncılık?', 'answer': 'bakery'},

    {'question': 'What is kule?', 'answer': 'tower'},

    {'question': 'What is lunapark?', 'answer': 'amusement park'},

    {'question': 'What is taraf?', 'answer': 'party'},

    {'question': 'What is kitapçı?', 'answer': 'bookstore'},

    {'question': 'What is gökdelen?', 'answer': 'skyscraper'},

    {'question': 'What is uçak?', 'answer': 'airplane'},

    {'question': 'What is taksicilik?', 'answer': 'taxi'}

    ]}

    # Create window and canvas using tkinter.

    root = tkinter.Tk()

    root.title("Language Labyrinth")

    canvas = tkinter.Canvas(root, width=600, height=600)

    canvas.pack()

    screen = TurtleScreen(canvas)

    screen.bgcolor('black')

    # Define a function to set flag background for Spanish level

    def spanishFlag():

    screen.bgpic("spainflag.png")

    # Define a function to set flag background for French level

    def frenchFlag():

    screen.bgpic("franceflaggrunge.png")

    # Define a function to set flag background for Japanese level

    def japaneseFlag():

    screen.bgpic("japanflagoffwhite.png")

    # Define a function to set flag background for Turkish level

    def turkishFlag():

    screen.bgpic("turkishflagdiagonal.png")

    # Define a function to combine the level and background setup functions for Spanish level

    def combinedSpanishCommands():

    span()

    spanishFlag()

    # Define a function to combine the level and background setup functions for French level

    def combinedFrenchCommands():

    fren()

    frenchFlag()

    # Define a function to combine the level and background setup functions for Japanese level

    def combinedJapaneseCommands():

    jpn()

    japaneseFlag()

    # Define a function to combine the level and background setup functions for Turkish level

    def combinedTurkishCommands():

    turk()

    turkishFlag()

    # Create function to get questions and answers.

    def getQuestions(language):

    for qa in dic[language]:

    q = qa['question']

    a = qa['answer']

    ans = simpledialog.askstring("Input", q, parent=root)

    if ans == a:

    print("Great job!")

    else:

    print("The correct answer was...", a)

    screen.listen()

    # Create function to get Spanish Questions.

    def getSpanishQuestions():

    getQuestions('spanish')

    # Create function to get French Questions.

    def getFrenchQuestions():

    getQuestions('french')

    # Create function to get Japanese Questions.

    def getJapaneseQuestions():

    getQuestions('japanese')

    # Create function to get Turkish Questions.

    def getTurkishQuestions():

    getQuestions('turkish')

    # Create class with separate window to choose level.

    class StartPage():

    def __init__(self):

    # Creation of second window.

    wn = tkinter.Tk()

    wn.title("Welcome!")

    # Creation of game title on start page.

    label = tkinter.Label(wn, text="Language Labyrinth", font=largefont)

    label.pack()

    # Create Spanish level button.

    button = tkinter.Button(wn, text="Spanish", command=combinedSpanishCommands)

    button.pack()

    # Create French level button.

    button2 = tkinter.Button(wn, text="French", command=combinedFrenchCommands)

    button2.pack()

    # Create Japanese level button.

    button3 = tkinter.Button(wn, text="Japanese", command=combinedJapaneseCommands)

    button3.pack()

    # Create Turkish level button.

    button4 = tkinter.Button(wn, text="Turkish", command=combinedTurkishCommands)

    button4.pack()

    # Create quit button for start page.

    qbutton = tkinter.Button(wn, text="Quit", command=wn.destroy)

    qbutton.pack()

    start = StartPage()

    # Create Pen class to draw the maze.

    class Pen(RawTurtle):

    def __init__(self):

    super().__init__(screen, shape='square')

    self.speed('fastest')

    self.color('white')

    self.penup()

    # Create setup so the maze will be drawn.

    def setup(self, level):

    for y in range(len(level)):

    screen_y = 288 - (y * 24)

    for x in range(len(level[y])):

    if level[y][x] == 'X':

    screen_x = (x * 24) - 288

    self.goto(screen_x, screen_y)

    self.stamp()

    walls.append((screen_x, screen_y))

    # Create player class to have a player.

    class Player(RawTurtle):

    def __init__(self):

    super().__init__(screen, shape='square')

    self.penup()

    self.speed('fastest')

    self.color('black')

    def bKey(self):

    global color

    print("b key pressed")

    self.color('blue')

    def rKey(self):

    global color

    print("r key pressed")

    self.color('red')

    def gKey(self):

    global color

    print("g key pressed")

    self.color('green')

    def pKey(self):

    global color

    print("p key pressed")

    self.color('purple')

    def yKey(self):

    global color

    print("y key pressed")

    self.color('goldenrod')

    def oKey(self):

    global color

    print("o key pressed")

    self.color('orange')

    # Create setup to create the player on the screen.

    def setup(self, level):

    for y in range(len(level)):

    for x in range(len(level[y])):

    if level[y][x] == 'P':

    screen_x = (x * 24) - 288

    screen_y = 288 - (y * 24)

    self.goto(screen_x, screen_y)

    return

    # Define a function that will allow player to move up.

    def move_up(self):

    # Calculate the spot to move to.

    movetoX = self.xcor()

    movetoY = self.ycor() + 24

    # Check if the space has a wall.

    if (movetoX, movetoY) not in walls:

    self.goto(movetoX, movetoY)

    gold_encounter()

    # Define a function that will allow player to move down.

    def move_down(self):

    # Calculate the spot to move to.

    movetoX = self.xcor()

    movetoY = self.ycor() - 24

    # Check if the space has a wall.

    if (movetoX, movetoY) not in walls:

    self.goto(movetoX, movetoY)

    gold_encounter()

    # Define a function that will allow player to move left.

    def move_left(self):

    # Calculate the spot to move to.

    movetoX = self.xcor() - 24

    movetoY = self.ycor()

    # Check if the space has a wall.

    if (movetoX, movetoY) not in walls:

    self.goto(movetoX, movetoY)

    gold_encounter()

    # Define a function that will allow player to move right.

    def move_right(self):

    # Calculate the spot to move to.

    movetoX = self.xcor() + 24

    movetoY = self.ycor()

    # Check if the space has a wall.

    if (movetoX, movetoY) not in walls:

    self.goto(movetoX, movetoY)

    gold_encounter()

    # Check if player touches the question.

    def collision(self, other):

    return self.distance(other) < 5

    # Create Question class to create the "gold" in the game.

    class Question(RawTurtle):

    def __init__(self, x, y):

    super().__init__(screen, shape='circle', visible=False)

    self.speed('fastest')

    self.color('hotpink')

    self.penup()

    self.goto(x, y)

    self.showturtle()

    # Define function that will remove gold when collided with.

    def destroy(self):

    self.hideturtle()

    # Define function to setup the "gold" in the game.

    def setup(level):

    for y in range(len(level)):

    for x in range(len(level[y])):

    char = level[y][x]

    screen_x = -288 + (x * 24)

    screen_y = 288 - (y * 24)

    if char == 'Q':

    questions.append(Question(screen_x, screen_y))

    # Define a function for the quit button.

    def quitPlaying():

    root.destroy()

    root.quit()

    # Game loop in regards to the gold.

    def gold_encounter():

    if levels[1] == True:

    print("It worked")

    return

    else:

    # Check for player collision with a question.

    # Iterate through the questions list.

    for question in questions:

    if player.collision(question):

    master = tkinter.Tk()

    b1 = tkinter.Button(master, text="Spanish Questions", command=getSpanishQuestions)

    b1.pack()

    b2 = tkinter.Button(master, text="French Questions", command=getFrenchQuestions)

    b2.pack()

    b3 = tkinter.Button(master, text="Japanese Questions", command=getJapaneseQuestions)

    b3.pack()

    b4 = tkinter.Button(master, text="Turkish Questions", command=getTurkishQuestions)

    b4.pack()

    # Destroy the question.

    question.destroy()

    # Remove question from questions list.

    questions.remove(question)

    screen.listen()

    # Create frame where button(s) will be.

    frame = tkinter.Frame(root)

    frame.pack()

    # Add questions list.

    questions = []

    # Wall coordinate list.

    walls = []

    # Create a levels list.

    levels = []

    # Define first level.

    level_1 = [

    "XXXXXXXXXXXXXXXXXXXXXXXXX",

    "XP XXXXXXX XXXXX",

    "X XXXXXXX XXXXXX XXXXX",

    "X XX XXXXXX XXXXX",

    "X XX XXX XX",

    "XXXXXX XX XXX Q XX",

    "XXXXXX XX XXXXXX XXXXX",

    "XXXXXX XX XXXX XXXXX",

    "X XXX Q XXXX XXXXX",

    "X XXX XXXXXXXXXXXXXXXXX",

    "X XXXXXXXXXXXXXXX",

    "X Q XXXXXXXX",

    "XXXXXXXXXXXX XXXXX X",

    "XXXXXXXXXXXXXXX XXXXX X",

    "XXX XXXXXXXXXX X",

    "XXX Q X",

    "XXX XXXXXXXXXXXXX",

    "XXXXXXXXXX XXXXXXXXXXXXX",

    "XXXXXXXXXX X",

    "XX XXXXX Q X",

    "XX XXXXXXXXXXXXX XXXXX",

    "XX XXXXXXXXXXXX XXXXX",

    "XX Q XXXX X",

    "XXXX X",

    "XXXXXXXXXXXXXXXXXXXXXXXXX"

    ]

    # Define second level.

    level_2 = [

    "XXXXXXXXXXXXXXXXXXXXXXXXX",

    "XP XX XX XXXX",

    "X XX XX XXXX",

    "X XXXXXXX XX XX X",

    "X XXXXXXX XX XX Q X",

    "X XX XX XX XXXXX X",

    "X XX XX XX XXXXX X",

    "XQ Q XX X X",

    "X XX X X",

    "X XXXXXXX XXXXX XXXX",

    "X XX XX X",

    "XXXXXX XX Q X",

    "XXXXXX XXXXXXXXXXXX X",

    "X Q XX XX X",

    "X XX XX X",

    "XXXXXXXXXX XX X",

    "XXXXXX X XX Q X",

    "XXXXXX X XX XX X",

    "X X X X",

    "X Q X X X",

    "XXXXXX XXXXXXXXXX X X",

    "XXXXXX XXXXXXXXXX X",

    "X X X",

    "X XQ XXXXXXXX",

    "XXXXXXXXXXXXXXXXXXXXXXXXX"

    ]

    # Define third level.

    level_3 = [

    "XXXXXXXXXXXXXXXXXXXXXXXXX",

    "X X XP X",

    "X Q X X X",

    "X XXXX XXXXXXXXXX X",

    "X XXXX XXXXXXXXXX X",

    "X Q XX X",

    "X XX X",

    "XXXXXXXXXXXX XXXX XXX",

    "X XXXX XXX",

    "X XXXX XXX",

    "XXXXX Q XXXXXXXXXXX XXX",

    "X Q X",

    "X X",

    "XXXXXXXXXX Q XXXXXXX X",

    "XXXXXXXXXX XXXXXXX X",

    "XXXX XXXXXXXXX X",

    "XXX XXXXXXXXX X",

    "XXXXXX XXXXXXXXXXXX X",

    "X X Q X",

    "X Q X XXXX",

    "XXXXXXX XXXXXX",

    "XXXXXXXXXXXX Q XXXXXXXXXX",

    "X X",

    "X Q XXX Q X",

    "XXXXXXXXXXXXXXXXXXXXXXXXX"

    ]

    # Define fourth level.

    level_4 = [

    "XXXXXXXXXXXXXXXXXXXXXXXXX",

    "XXXXXXXXXX P XXXXXXXXXX",

    "XXXXXXXXXX XXXXXXXXXX",

    "XXXXXXXXXXX Q XXXXXXXXXXX",

    "X X",

    "X XXX X",

    "XXXXX Q XXXXX Q XXXXX",

    "X XXX X",

    "XXXXXXXX X XXXXXXXX",

    "X Q X Q X",

    "X X X",

    "XXXXXXXXXX QXQ XXXXXXXXXX",

    "X X",

    "X X",

    "XXXXXXXXXXXXXXXXXXXXX X",

    "XXXXXXXXXXXXXXXXXXXXX X",

    "XXXXXX XXXXXXXXX X",

    "XXXXXX XXXXXXX Q XXXXX",

    "XQ X",

    "X X",

    "XXXXXXXXXXX XXXXXXX X",

    "XXXXXXXX XXXXXXX X",

    "XXXXXX XXXXXXXXXX X",

    "XXXXX Q XXXXXXXXXXX Q X",

    "XXXXXXXXXXXXXXXXXXXXXXXXX"

    ]

    # Add the level(s) to the levels list.

    levels.append(level_1)

    levels.append(level_2)

    levels.append(level_3)

    levels.append(level_4)

    # Class instances.

    pen = Pen()

    player = Player()

    # Creation of quit button.

    quitButton = tkinter.Button(frame, text='Quit', command=quitPlaying)

    quitButton.pack()

    # Button commands for player movement

    screen.onkeypress(player.move_up, 'Up')

    screen.onkeypress(player.move_down, 'Down')

    screen.onkeypress(player.move_left, 'Left')

    screen.onkeypress(player.move_right, 'Right')

    # Button commands for customizable player colors

    screen.onkeypress(player.bKey, "b")

    screen.onkeypress(player.rKey, "r")

    screen.onkeypress(player.gKey, "g")

    screen.onkeypress(player.pKey, "p")

    screen.onkeypress(player.yKey, "y")

    screen.onkeypress(player.oKey, "o")

    screen.listen()

    # Call main game loop.

    screen.mainloop()

    所以基本上我希望在关卡中每个“黄金”只能提出一个问题。但是,它目前遍历了dict中的所有问题。怎么解决这个问题?

    展开全文
  • # 碰到墙,游戏结束 if map_data[x][y] == 1: print("游戏结束") break else: map_data[x][y],map_data[x][y+1] = map_data[x][y+1], map_data[x][y] # 进行交换操作 print_map() # 当用户输入s时执行向下走...

     

     

    #  画地图
    map_data = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 2, 1, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
    [1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
    [1, 0, 0, 0, 1, 0, 0, 1, 0, 1],
    [1, 1, 1, 0, 1, 1, 1, 1, 0, 1],
    [1, 1, 1, 0, 1, 1, 1, 1, 0, 0],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ]
    # 定义人所在的位置(初始化)
    x = 2
    y = 1
    endx = 7
    endy = 9
    # 用字符串重新给地图赋值
    def print_map():
    for nums in map_data:
    for num in nums:
    if num == 1:
    print(" #",end=" ")
    elif(num == 0):
    print(" ",end=" ")
    else:
    print(" $",end=" ")
    print("")

    # 所用到的核心知识

    # print("交换前的地图")
    # print_map()
    # map_data[2][1], map_data[2+1][1] = map_data[2+1][1], map_data[2][1]
    # print("交换后的地图")
    # print_map()

    # 先画地图
    print_map()
    while True:
    # 指令的输入
    order = input("请输入指令(a: 左,s: 下, d: 右, w: 上):")
    # 对用户输入的指令进行判断
    # 当用户输入a时执行向左走进行交换(列变行不变 列下标减1)
    if order == "a":
    y = y-1
    # 碰到墙,游戏结束
    if map_data[x][y] == 1:
    print("游戏结束")
    break
    else:
    map_data[x][y],map_data[x][y+1] = map_data[x][y+1], map_data[x][y] # 进行交换操作
    print_map()

    # 当用户输入s时执行向下走进行交换(列不变行变 行下标加1)
    elif order == "s":
    x = x + 1
    if map_data[x][y] == 1:
    print("游戏结束")
    break
    else:
    map_data[x][y], map_data[x-1][y] = map_data[x-1][y], map_data[x][y] # 进行交换操作
    print_map()

    # 当用户输入d时执行向右走进行交换(列变行不变 列下标加1)
    elif order == "d":
    y = y + 1
    if map_data[x][y] == 1:
    print("游戏结束")
    break
    else:
    map_data[x][y], map_data[x][y - 1] = map_data[x][y - 1], map_data[x][y] # 进行交换操作
    print_map()
    if map_data[x][y] == map_data[endx][endy]:
    print("恭喜你过关了")
    break

    # 当用户输入w时执行向上走进行交换(列不变行变 行下标减1)
    elif order == "w":
    x = x - 1
    if map_data[x][y] == 1:
    print("游戏结束")
    break
    else:
    map_data[x][y], map_data[x + 1][y] = map_data[x + 1][y], map_data[x][y] # 进行交换操作
    print_map()

    # 当用户输入非规则内的指令时的错误提示,并重新输入
    else:
    print("您输入指令有误,请重新按指令规则输入!")
    continue
    #
    截图:

     



    转载于:https://www.cnblogs.com/yunlongaimeng/p/8719335.html

    展开全文
  • python迷宫游戏

    2014-08-08 19:15:20
    课余制作的PYTHON迷宫游戏,提一些建议,谢谢
  • Python实现迷宫游戏

    千次阅读 多人点赞 2020-07-07 15:27:35
    本次实验设计了一款迷宫游戏,采用用Python开发技术实现。以往经典的的游戏中有魂斗罗,拳皇,超级玛丽,贪吃蛇,俄罗斯方块等;发展到现在,玩游戏已经成为生活的一部分了,尤其是现在的很多游戏都已经网络社交化...

    摘要

    本次实验设计了一款迷宫小游戏,采用用Python开发技术实现。以往经典的的游戏中有魂斗罗,拳皇,超级玛丽,贪吃蛇,俄罗斯方块等;发展到现在,玩游戏已经成为生活的一部分了,尤其是现在的很多游戏都已经网络社交化了,游戏种类也更加丰富了。Python在设计上坚持了清晰划一的风格,这使得Python成为一门易读、易维护,并且被大量用户所欢迎的、用途广泛的语言。因此,利用Python语言制作一个简单的迷宫小游戏,将是本次论文讨论的内容。该迷宫小游戏使用几个模块绘制呈现,并实现可以自由操作的功能。
    This experiment designed a small maze game, using Python development technology to achieve.Past classic games have contra, Boxing Emperor, Super Mary, snake, Tetris and so on,Up to now, playing games has become a part of life, especially now many games have been social network, games have become more diversified. Python’s adherence to a clear and uniform style of design has made it an easy-to-read, maintainable, and versatile language that is popular with a large number of users. Therefore, using Python language to make a simple maze game will be the content of this paper.The maze game USES several modules to draw and render, and to achieve the function of free operation.

    1.引言

    1.1研究的背景及意义

    Python是一种跨平台的计算机程序设计语言。 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
    当Python作为游戏脚本内嵌在游戏中,这样做即可以利用游戏引擎的高性能,又可以受益于脚本化开发的优点。即游戏剧本、数据、玩法逻辑这类需要灵活修改和调整的部分可以写在脚本中,只需要修改脚本内容就可以调整游戏内容,不需要重新编译游戏。简单的小游戏可以直接用Python一类的脚本语言开发,善用一些库应该也可以达到不错的性能。

    1.2研究的内容

    为了了解Python的模块脚本的使用,本次实验使用Python语言实现的一款迷宫小游戏,从而去更深度认识Python的功能。本次实验分三个模块绘制,首先用Maze类绘制出迷宫地图,再用Player类实现玩家的方向动作,再由Controller类实现操作器让玩家方便操作,最后在主函数中运行实现迷宫小游戏。

    2.系统结构

    2.1系统的结构

    本次项目由主函数、maze模块、player模块和controller模块组成。如下图所示。
    图2-1

    2.2基本思路

    先创建maze模块绘制迷宫地图;再定义玩家移动前后的位置方向;再设计操作器实现玩家自由操作;最后由主函数将各个模块集合。其流程图如下图所示。
    图2-2

    3.实现代码

    在迷宫问题中,首先解决的就是迷宫的地图,我们从实现的效果以及主函数中,不难得知在主函数中的maze_list里,1表示迷宫的墙,0表示迷宫内的通道,如下图所示:
    图3-1

    3.1Maze类

    而实际实现迷宫的绘制在Maze类中,以下为maze模块中的代码及解析。在Maze类中,先调用Turtle父类初始化方法,方便后面调用,再绘制出迷宫内的一格墙,最后由一格墙循环打印出整个迷宫。

    from turtle import Turtle
    import turtle
    
    #设置游戏的窗口大小和背景颜色
    turtle.screensize(800,600, "pink")
    
    class Maze(Turtle):
      size = 20    #迷宫内一格墙的长宽
      
      def __init__(self, maze_list):
        # 需要先调用父类的初始化方法才能在初始化方法中调用父类的方法
        Turtle.__init__(self)
        self.maze_list = maze_list
        # 为了加快绘图速度隐藏海龟,速度设为最快
        self.hideturtle()
        self.speed(0)
        self.draw_walls()
    
    #绘制迷宫内一格墙的过程
      def draw_wall(self):
        self.pendown()
        self.begin_fill()
        #绘制墙的颜色
        self.fillcolor('red')
        #首先画一个距离为20的横线,再向右旋转90度,循环4次形成方形
        for i in range(4):
          self.forward(self.size)
          self.right(90)
        self.end_fill()
        self.penup()
    
    #绘制整个迷宫的墙
      def draw_walls(self):
        self.penup()
        # 从 (-130, 130) 开始
        self.goto(-130, 130)
        #打印墙,横纵循环13次(整个迷宫的长和宽由13格墙组成)
        for row in range(13):
          for col in range(13):
            #主函数中的maze_list里面的1则打印出一格墙
            if self.maze_list[row][col] == 1:
              self.draw_wall()
            # 右移一列
            self.goto(self.size * (col + 1) - 130, 130 - self.size * row)
          # 下移一行
          self.goto(-130, 130 - self.size * (row + 1))
    
    

    3.2Player类

    迷宫内自然需要一个玩家(海龟)来走出迷宫,所以此处用Player类对玩家player进行定义,以下为player模块中的代码及解析。其中通过对玩家的初始位置、终点位置、以及移动到相应的位置的定义,实现玩家在迷宫内的通道移动,即移动时的位置和玩家自身方向的改变。

    from turtle import Turtle
    import turtle
    
    class Player(Turtle):
      def __init__(self, maze_list, start_m, start_n, end_m, end_n):
        # 父类初始化
        Turtle.__init__(self)
        #初始的横纵坐标
        self.m = start_m
        self.n = start_n
        #终点的横纵坐标
        self.end_m = end_m
        self.end_n = end_n
        #迷宫地图
        self.maze_list = maze_list
        self.hideturtle()
        self.speed(0)
        self.penup()
        # 玩家移到对应的位置
        self.goto(self.n * 20 - 120, 120 - self.m * 20)
        # 生成玩家
        self.shape('turtle')
        self.color('yellow')
        #玩家初始方向
        self.setheading(270)
        self.showturtle()
        
    #当玩家到达终点时,显示'you win!'
      def reach_exit(self, m, n):
        if m == self.end_m and n == self.end_n:
          # 走出迷宫,显示'you win!'
          text = turtle.Turtle()
          text.hideturtle()
          text.penup()
          text.goto(-125, -10)
          text.color('blue')
          text.write('you win!', font = ('SimHei', 48, 'bold'))
    
      #定义玩家可移动的位置,即只允许在迷宫内的通道里移动
      def canmove(self, m, n):
        #遇到0允许移动
        return self.maze_list[m][n] == 0
    
      #玩家移动时位置发生的变化
      def move(self, m, n):
        self.m = m
        self.n = n
        self.goto(self.n * 20 - 120, 120 - self.m * 20)
        self.reach_exit(m, n)
    
      #向上移动
      def go_up(self):
        if self.canmove(self.m - 1, self.n):
          self.setheading(90)
          self.move(self.m - 1, self.n)
    
      #向下移动
      def go_down(self):
        if self.canmove(self.m + 1, self.n):
          self.setheading(270)
          self.move(self.m + 1, self.n)
    
      #向左移动
      def go_left(self):
        if self.canmove(self.m, self.n - 1):
          self.setheading(180)
          self.move(self.m, self.n - 1)
    
      #向右移动
      def go_right(self):
        if self.canmove(self.m, self.n + 1):
          self.setheading(0)
          self.move(self.m, self.n + 1)
    

    3.3Controller类

    当拥有玩家出现时,便需要让玩家拥有操作器,从而操作玩家离开迷宫,所以此处使用controller模块,其功能是为了方便玩家的控制,以下为controller模块中的代码及解析。在Controller类中,先绘制出控制器上、下、左、右的图示,再绑定鼠标的点击事件,最后用abs函数进行比较去判断鼠标点击时的位置方向控制。

    from turtle import Turtle
    import turtle
    
    class Controller(Turtle):
      def __init__(self, go_up, go_down, go_left, go_right):
        # 父类初始化
        Turtle.__init__(self)
        # 初始值设置
        self.go_up = go_up
        self.go_down = go_down
        self.go_left = go_left
        self.go_right = go_right
        # 绘制控制器
        self.hideturtle()
        self.speed(0)
        self.draw_btn('上', -15, 165)
        self.draw_btn('下', -15, -135)
        self.draw_btn('左', -165, 15)
        self.draw_btn('右', 135, 15)
        # 绑定点击事件
        screen = self.getscreen()
        screen.onclick(self.handlescreenclick)
    
      #此处与绘制迷宫内一格墙的方法雷同,不做解释
      def draw_btn(self, name, x, y):
        self.penup()
        self.goto(x, y)
        self.begin_fill()
        self.fillcolor('#ffffff')
        for i in range(4):
          self.forward(30)
          self.right(90)
        self.end_fill()
        self.color('#000000')
        self.goto(x + 7, y - 20)
        self.write(name, font = ('SimHei', 12, 'bold'))
    
      #当点击事件发生时利用abs函数进行比较判断
      def handlescreenclick(self, x, y):    
        if y > 0 and abs(x) < y:
          self.go_up()
        if y < 0 and abs(x) < -y:
          self.go_down()
        if x < 0 and abs(y) < -x:
          self.go_left()
        if x > 0 and abs(y) < x:
          self.go_right()
    

    3.4主函数

    最后再由主函数将各个模块集合。

    from maze import Maze
    from player import Player
    from controller import Controller
    
    maze_list = [
      [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1],
      [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
      [1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
      [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
      [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
      [1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1],
      [1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
      [1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1],
      [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1],
      [1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1],
      [1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]
    ]
    
    Maze(maze_list)
    #0,5表示玩家起始的位置;12,7表示终点的位置
    player = Player(maze_list, 0, 5, 12, 7)     
    Controller(player.go_up, player.go_down, player.go_left, player.go_right)
    

    4.实验

    实验结果如图。
    图4-1
    通过点击上、下、左、右控制玩家的移动,如图。
    图4-2
    当玩家走到终点时,将显示出’you win!’,如图。
    图4-3

    5.总结和展望

    基于Python的迷宫小游戏是采用python语言及其模块开发完成的。本项目实现了游戏流程中的所有功能,其游戏操作容易上手,界面设计颜色感官简洁明了,其内容功能有可拓展性,方便为来程序的优化设计。
    这次项目设计开发为今后的学习和工作产生了积极的意义。由于还是初学者,在项目设计中还有欠缺和考虑不周的地方,游戏还有待进一步研究和改善,比如迷宫的地图目前没有设计随机生成等不足。

    参考文献

    [1]https://blog.csdn.net/jark_/article/details/77532105
    [2]https://blog.csdn.net/qq_29681777/article/details/83719680
    [3]https://www.jianshu.com/p/b5b1391420f1

    展开全文
  • Python小游戏源码-逃出迷宫游戏源码
  • Python一维迷宫游戏

    2021-03-28 11:15:04
    Python一维迷宫游戏
  • Python二维迷宫游戏

    2021-03-28 11:13:19
    Python二维迷宫游戏
  • Python编写迷宫游戏

    千次阅读 2020-12-25 10:00:42
    文章目录1、 项目概述1.1 项目目标和...迷宫游戏是非常经典的游戏,在该项目要求随机生成一个迷宫,并求解迷宫 1.2 项目的主要功能 随机生成迷宫并求解 2、 项目设计 2.1项目总体框架 通过 Prim 算法随机创建迷宫
  • 一看就懂的,Python迷宫游戏教程

    千次阅读 多人点赞 2019-10-13 17:30:02
    Python写一个迷宫游戏(含代码)前言开发工具参考资料(谢谢各位大牛的资料,没有你们的无私贡献我根本写不出来)Python第三方模块库安装开发第一步插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建...
  • Python 制作迷宫游戏(二)——游戏窗口 上一节我们使用prime做了迷宫的底层数组,它的形式是一个二维数组. 这一节我们着手开始制作游戏窗口 使用模块 pygame 在这项迷宫游戏的制作当中需要运用到的模块为pygame,小...
  • 作者 | 刘早起来源 | 早起Python大家好,儿童节就要来了,虽然秃头程序员没有头发,但是童心还是一直都在的,今天就分享一个私藏的GitHub项目——free-python-games,一行代码就能进入使用Python开发的小游戏快乐...
  • 本文实例讲述了Python基于分水岭算法解决走迷宫游戏。分享给大家供大家参考,具体如下:#Solving maze with morphological transformation"""usage:Solving maze with morphological transformationneeded module:cv...
  • 原标题:Python入门|迷宫游戏制作 Python功能强大,广泛用于系统任务处理和Web编程 上一期的工作坊中,新一任的会长向大家介绍了创客的发展以及本学期的活动安排。本学期考虑不同专业的限制,从硬件和软件两方面...
  • Python 制作迷宫游戏(一)——地图

    千次阅读 2020-06-19 21:34:08
    Python 制作迷宫游戏(一)——地图 序 作为一个迷宫类的游戏,其最重要的是什么?当然是它的地图啦♪(^∇^*) 那么我们又该如何制作一张迷宫地图呢⊙(・◇・)? 很显然,我们不可能一张张自己画吧 网络上常见的迷宫...
  • 迷宫游戏python实现

    千次阅读 多人点赞 2020-02-01 12:39:11
    首先我使用python3中的tkinter实现了一个简陋版的迷宫游戏,截图如下图所示,为了降低游戏的难度,在寻找路径的过程中你可以通过点击图中的空白点,生成从起点到你点击位置的暂时路径。迷宫的难度主要由以下几个参数...
  • 这是一个用Python制作的迷宫游戏,需要基于Python海龟画图模块精灵模块支持,安装方法为 pip install sprites。 如果安装很慢,请联系李兴球,微信scratch8, 以下是代码, """ 小虫子的冒险.py 这是一个迷宫...
  • 相信大家都玩过迷宫游戏,对于简单的迷宫,我们可以一眼就看出通路,但是对于复杂的迷宫,可能要仔细寻找好久,甚至耗费数天,然后可能还要分别从入口和出口两头寻找才能找的到通路,甚至也可能找不到通路。...
  • 主要介绍了Python基于分水岭算法解决走迷宫游戏,结合具体实例形式分析了分水岭算法解决迷宫问题的相关步骤与操作技巧,需要的朋友可以参考下
  • Python 制作一个迷宫游戏.pdf

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,665
精华内容 666
关键字:

python迷宫游戏

python 订阅