我刚刚在python和Iam试图找出这个如果使用图形在python上的tic tac toe游戏的声明...我需要一些帮助,谢谢你
# this programe wil present Tic Tac Toe game X , O
# in this code i will use import tkinter to show a message box
from graphics import *
import tkinter.messagebox
from button import Button
def main():
win = GraphWin("Tic Tac", 500,500)
win.setBackground("dark red")
# win.setCoords(Point(0,0),Point(3,3))
# text
T = Text(Point(228,420.20),'Welcome to Tic Tac Toc game !')
T.setFill("red")
T.setStyle('bold')
T.draw(win)
input_box = Entry(Point(250,450),10)
input_box.draw(win)
txt = Text(Point(250,400),"")
txt.draw(win)
txt.setText(input_box.getText())
# get mouse click to start the game and change to the next window
Ck = win.getMouse()
Ck.draw(win)
# changing the intro and ask the user to put moe information
T.setText('Enter your name please , then click anywhere to start!.')
# 2 Mouse click
Ck2 = win.getMouse()
Ck2.draw(win)
buttons=[]
buttonWidth=100
buttonHeight=100
y=50
for i in range(3):
x=100
for j in range(3):
b = Button(Point(x,y), Point(x+buttonWidth,y+buttonHeight),"")
buttons.append(b)
b.draw(win)
x += buttonWidth
y+=buttonHeight
while True:
p = win.getMouse()
for b in buttons:
if b.clicked(p):
b.setText("X")
# 0 1 2
# 3 4 5
# 6 7 8
#check for X winner
if (buttons[0] != "" and buttons[0].getText() == buttons[1].getText() == buttons[2].getText() or
buttons[0] != "" and buttons[0].getText() == buttons[3].getText() == buttons[6].getText()or
buttons[0] != "" and buttons[0].getText() == buttons[4].getText() == buttons[8].getText()or
buttons[0] != "" and buttons[1].getText() == buttons[4].getText() == buttons[7].getText()or
buttons[0] != "" and buttons[2].getText() == buttons[5].getText() == buttons[8].getText()or
buttons[0] != "" and buttons[3].getText() == buttons[4].getText() == buttons[5].getText()or
buttons[0] != "" and buttons[6].getText() == buttons[7].getText() == buttons[8].getText()or
buttons[0] != "" and buttons[2].getText() == buttons[4].getText() == buttons[6].getText()):
tkinter.messagebox.showinfo('WE GOT A WINNER !', ' player X Won the game')
# check for O winner
b.setText('O')
if buttons[0] != "" and buttons[0].getText() == buttons[1].getText() == buttons[2].getText():
break
elif buttons[0] != "" and buttons[0].getText() == buttons[3].getText() == buttons[6].getText():
break
elif buttons[0] != "" and buttons[0].getText() == buttons[4].getText() == buttons[8].getText():
break
elif buttons[0] != "" and buttons[1].getText() == buttons[4].getText() == buttons[7].getText():
break
elif buttons[0] != "" and buttons[2].getText() == buttons[5].getText() == buttons[8].getText():
break
elif buttons[0] != "" and buttons[3].getText() == buttons[4].getText() == buttons[5].getText():
break
elif buttons[0] != "" and buttons[6].getText() == buttons[7].getText() == buttons[8].getText():
break
elif buttons[0] != "" and buttons[2].getText() == buttons[4].getText() == buttons[6].getText():
break
tkinter.messagebox.showinfo('WE GOT A WINNER !', ' player O Won the game')
main()的
这是我想要的其他代码:
from graphics import *
class Button:
'''This is a button class.'''
def __init__(self, p1, p2, text):
self.box = Rectangle(p1, p2)
self.label = Text(Point((p1.getX()+p2.getX())/2,(p1.getY()+p2.getY())/2), text)
self.p1 = Point(min(p1.getX(),p2.getX()),min(p1.getY(),p2.getY()))
self.p2 = Point(max(p1.getX(),p2.getX()),max(p1.getY(),p2.getY()))
self.drawn = False
def draw(self, win):
''' draw this button to the window, and returns None.'''
if self.drawn == False:
self.box.draw(win)
self.label.draw(win)
self.drawn = True
def undraw(self):
if self.drawn:
self.box.undraw()
self.label.undraw()
self.drawn = False
def clicked(self, p):
'''Returns True if p is inside the button. False otherwise.'''
if self.p1.getX()<p.getX()<self.p2.getX():
if self.p1.getY()<p.getY()<self.p2.getY():
return True
return False
def isClicked(self, p):
return self.clicked(p)
def getText(self):
"""returns the label text."""
return self.label.getText()
def setText(self, text):
self.label.setText(text)