我正在为编程课程编写一个老虎机游戏。我可以让游戏从包含6的文件中获取5个随机图像,将它们排列在空白画布上并比较图片以查看是否存在3行。它会告诉你你是否获胜并要求你再次参加比赛。我需要循环游戏,以便在用户选择“y / n”后再次运行。到目前为止,这是我的代码。
from random import *
def main():
pictureList = setMediaPath("F:\Class Stuff\INF 120\Project X\gamepics")
s = 1 # requestInteger("Slot machine image numbering begins at:")
e = 6 # requestInteger("Slot maching image numbering ends at:")
ext = "jpg" #requestString("Enter the extension ofthe slot machine image files (without a dot):")
mypic = []
for num in range(s, e+1):
mypic = mypic + [(makePicture("smImage" + str(num) + "." + ext))]
#set up the "game board" in a blank window
w = getWidth(mypic[0])
h = getHeight(mypic[0])
ePic = makeEmptyPicture(w*5, h)
show(ePic)
# start the actual game
win = False
inRow = 1
last = -1
#loop through all five slots and set pic for each one a new, random picture. Form list of pictures.
#make a loop to run through the game until win = true.
for i in range(0,5):
imgIndex = randrange(0,len(mypic))
copyInto(mypic[imgIndex], ePic, i*w, 0)
repaint(ePic)
if imgIndex == last:
inRow = inRow + 1
else:
inRow = 1
last = imgIndex
if inRow >= 3:
win = true
if win == true:
showInformation("YOU WIN!!")
rePlay = requestString("Would you like to play again? (y/n)")
else:
showInformation("You Lose...")
requestString("Play again? (y/n)")
媒体路径设置为我的机器上带有图像的目录。这是我的谷歌驱动器与图像的链接。 https://drive.google.com/folderview?id=0B_ZDD1CZGQYlYjFQWEpjR0NzMGs&usp=sharing。如果您打算运行此功能,则需要更改代码中的媒体路径以与图片的位置相对应。
我感谢任何和所有帮助。谢谢!
答案 0 :(得分:0)
我不知道任何jython,但伪代码应该是
var continue = true
while(continue) {
[[play game, set continue based on user input]]
}