python文字游戏循环3次_如何停止迭代for循环以便玩家可以在Python迷宫游戏中移动?...
我使用的是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中的所有问题。怎么解决这个问题?
python文字游戏循环3次_如何停止迭代for循环以便玩家可以在Python迷宫游戏中移动?...相关推荐
- python做erp系统教程_“python2.7教程廖雪峰“刚开始学openERP Python,如何快速入门?...
为什么廖雪峰的JS教程不如Python 教程 娃娃哈 廖雪峰python教程在哪 这是他的网址:www.liaoxuefeng.com 这是python专有python2.7,python3,可供选择 ...
- python print end报错_下面代码的输出结果是 for i in Python: print(i,end= )_学小易找答案...
[单选题]关于Python字符串,以下选项中描述错误的是 [单选题]以下程序的输出结果是: lcat =["狮子","猎豹","虎猫",&q ...
- 32岁了学python来的及吗_为什么每个人都应该在2020年学习Python?
如今每个人都在谈论 Python,包括那些曾经对 Python 嗤之以鼻的人.本文作者 Javinpaul 原是一名 Java 狂热粉,他以前还曾号召大家学习 Java 而不是 Python.如今他的 ...
- python好用的软件_【分享|10款超好用的辅助Python的软件,初学者请查收!】- 环球网校...
[摘要]在这个科学技术高速发展的时代,越来越多的人都开始选择学习编程软件,那么首先被大家选择的编程软件就是python,也用在各行各业之中,并被大家所熟知,所以也有越来越多的python学习者关注Py ...
- python实现洗牌算法_如何高效而完美地洗牌?用Python做很简单
Python不用学,看看你就懂:拿来就能用,用用你就会 无需安装编程软件,把代码拷贝到在线编辑器即可运行 考虑一下扑克牌,如何用电脑编程做到高效而完美地洗牌呢? 要求是代码少.效率高,洗牌的结果要同时 ...
- python 函数进度条怎么_刷新你对进度条的认识,用python写出不一样的进度条
1 简介 在日常工作中,我们运行程序经常会用到「循环迭代」,假如这个执行时间很短,那倒也无所谓.但是有一些过程耗时蛮长的,给其加上「进度条」(progress bar),可以帮我们监控代码执行进度,以 ...
- for循环如果先--_如果再写for循环,我就锤自己!
几种遍历方法中for执行最快,它没有任何额外的函数调用栈和上下文.但在实际开发中我们要结合语义话.可读性和程序性能,去选择究竟使用哪种方案.下面来看for, foreach, map, for...i ...
- python统计图作息规律统计分析_借鉴柳比契夫时间统计法,用Python做了个时间管理工具TMTask...
很久以前,我读过格吕宁一本很神奇的书--<奇特的一生>,俄国生物学家柳比契夫的传奇故事和他的时间统计法. 先来体会一下这位老人家有多牛. 柳比歇夫生前发表了七十来部学术著作.其中有分散分析 ...
- python外国人也用吗_再也不怕和老外交流了!我用python实现一个微信聊天翻译助手!...
前言 在前面的一篇文章如何用python"优雅的"调用有道翻译中咱们清楚的写过如何一层一层的解开有道翻译的面纱,并且笔者说过那只是脑洞的开始.现在笔者又回来了.当你遇到一些外国小哥 ...
最新文章
- 如何利用python的newspaper包快速爬取网页数据
- 开源自动机器学习(AutoML)框架盘点
- 第六章:密码破解系统修复
- 内网使用ohmyzsh
- js中null和undefined
- 关于Mac设置alias别名访问服务器
- SAP OData的CSRF校验开关
- [BOOST] BOOST::Format
- python实现数据恢复软件_pyinstaller还原python代码过程图解
- vscode用鼠标滚轮_前端开发神器 VSCode 使用总结
- 【Unity】(转)游戏辅(外)助(挂)开发
- PPT高级教程及技巧
- python snownlp了解_分享python snownlp的实例教程
- cc2530协调器向终端发信息
- php ping 检测电脑在线,怎么ping网速(教你如何用ping命令来检测电脑的网速)
- czy的后宫——矩阵快速幂优化DP
- C++万年历课程设计
- HIT-ICS Hello‘s P2P
- android 代码 drawable,Android Drawable完全解析(一):Drawable源码分析(中)
- sql语句的批量添加