我从CS老师那里得到了一些有趣的工作,准备A级课程,因为当我完成11年级时,我们没有任何“正式”工作要做,任务是一个“策划”类游戏。 >
玩家猜测顺序中的颜色以及正确的颜色
这是我第一次使用Tkinter
,我找到了destory()函数,但是如果不再次手动按“运行”,就无法重新启动程序。是否有一种方法可以删除所有小部件,然后重新显示它们。就像是重置按钮。
这是我的代码:
#the main window win = Tk() #the main frame MainFrame = Frame(win) #the frame which contains the buttons ButtonFrame = Frame(win) #will contain the game MainCanvas = Canvas(MainFrame, height=550) #the bottom most frame, contains the new game button BottomFrame = Frame(win) def closeWin(): win.destroy() def updateCoordinates(coodinates,NumberOfPresses): for i in range (4): print(i) if coodinates[i] == coodinates[0]: print(coodinates) coodinates[i] = coodinates[i] + 60 print(coodinates) elif coodinates[i] == coodinates[1] and NumberOfPresses >= 5: coodinates[i] = coodinates[i] - 60 print(coodinates) elif coodinates[i] == coodinates[2]: coodinates[i] = coodinates[i] + 60 print(coodinates) elif coodinates[i] == coodinates[3] and NumberOfPresses >= 5: coodinates[i] = coodinates[i] - 60 print(coodinates) return coodinates #this is run is the red button is pressed def redGuess(coodinates,NumberOfPresses): redGuess = MainCanvas.create_oval(coodinates, fill="red") NumberOfPresses = NumberOfPresses + 1 updateCoordinates(coodinates,NumberOfPresses) return NumberOfPresses, coodinates def blueGuess(coodinates,NumberOfPresses): blueGuess = MainCanvas.create_oval(coodinates, fill="blue") NumberOfPresses = NumberOfPresses + 1 updateCoordinates(coodinates,NumberOfPresses) return NumberOfPresses, coodinates def MakeAnswer(): #the list of the colours colours = list("rbygwo") #randomizes them random.shuffle(colours) #creates a new list of the randomised letters order = ''.join(colours) result = '' for i in (order): #loops through each letter and adds a space bettween them result = result + i + ' ' #calls the new list answer AND puts each letter into a sepearte index in a list answer = result.split() #removes one colour from the answer answer.pop(5) return answer #runs the main program def NewGame(): NumberOfPresses = 0 coodinates = [15,500,65,550] answer = MakeAnswer() redButton = Button(ButtonFrame,height=3, width=7 ,bg= "red", command =lambda: redGuess(coodinates,NumberOfPresses)) blueButton = Button(ButtonFrame,height=3, width=7 ,bg= "blue", command =lambda: blueGuess(coodinates, NumberOfPresses)) yellowButton = Button(ButtonFrame,height=3, width=7 ,bg= "yellow") greenButton = Button(ButtonFrame,height=3, width=7 ,bg= "green") whiteButton = Button(ButtonFrame,height=3, width=7 ,bg= "white") orangeButton = Button(ButtonFrame,height=3, width=7 ,bg= "orange") #displays all the widgets in the window MainFrame.pack() ButtonFrame.pack() MainCanvas.pack() BottomFrame.pack(side=BOTTOM) redButton.pack(side=LEFT, fill=X) blueButton.pack(side=LEFT, fill=X) yellowButton.pack(side=LEFT, fill=X) greenButton.pack(side=LEFT, fill=X) whiteButton.pack(side=LEFT, fill=X) orangeButton.pack(side=LEFT, fill=X) reset_button = Button(BottomFrame, text="Start A New Game", command = closeWin) reset_button.pack(side=BOTTOM, fill=X) #displayes the correct sequence print(answer) return #starting coodinates of the first guess #format= [x1, y1, x2, y2] #Start of the game NewGame() #creates the buttons to select the different colours #runs the game continuously win.mainloop()
同样也是为了让您知道按计数器的按钮无法正常工作,但是在重置窗口之前,我无法修复它。
我从CS老师那里得到了一些有趣的工作,准备A级课程,因为当我完成11年级时,我们没有任何“正式”工作要做,任务是一个“策划”类型的游戏。玩家猜测...
您可以使用.withdraw()
功能。