我的代码运行不正常,所以我希望有人可以看看它并告诉我出了什么问题?它应该是一个记忆游戏,但我在让用户输入进入列表时遇到了问题。任何帮助表示赞赏。
# Tile.py
from graphics import*
class Tile:
def __init__(self, win, tl, br, color):
til1 = Tile('Games', p1(top left), p2(bottom right), "red" """
x1, y1 = tl.getX(), tl.getY()
x2, y2 = br.getX(), br.getY()
self.xmin = x1
self.xmax = x2
self.ymin = y1
self.ymax = y2
p1 = Point(self.xmin, self.ymin)
p2 = Point(self.xmax, self.ymax)
self.rect = Rectangle(p1, p2)
self.rect.setFill(color)
self.rect.draw(win)
self.deactivate()
def setFill(self, color):
'Lets the user change the color of the tile'
self.rect = Rectangle(p1, p2)
self.rect.setFill(color)
def clicked(self,p):
"Returns true if tile active and p is inside"
return (self.active and
self.p1.getX() <= p.getX() <= self.p2.getX() and
self.p1.getY() <= p.getY() <= self.p2.getY())
def activate(self):
"Sets this button to 'active'."
#self.rect.setWidth(2)
self.active = True
def deactivate(self):
"Sets this button to 'inactive'."
#self.rect.setWidth(1)
self.active = False
from graphics import*
from random import*
from math import*
from Tile import*
def inCircle(pt1, circ):
dx = pt1.getX() - circ.getCenter().getX()
dy = pt1.getY() - circ.getCenter().getY()
dist = sqrt(dx * dx + dy * dy)
return dist <= circ.getRadius()
def main():
win = GraphWin('Games', 640, 480)
center = Point(320, 240)
p1 = Point(0, 0)
til1 = Tile(win, p1, center, "red3")
p2 = Point(320, 0)
p3 = Point(640, 240)
til2 = Tile(win, p2, p3, "blue3")
p4 = Point(0, 240)
p5 = Point(320, 480)
til3 = Tile(win, p4, p5, "green3")
p6 = Point(320, 240)
p7 = Point(640, 480)
til4 = Tile(win,p6, p7, "yellow3")
circ = Circle(center, 75)
circ.setFill("grey3")
circ.draw(win)
start = Text(Point(320, 240), "PLAY!")
start.setSize(24)
start.setFace("helvetica")
start.setFill("white")
start.setStyle("bold")
start.draw(win)
while True:
mouse = win.getMouse()
if inCircle(mouse,circ):
start.undraw()
while True:
memA = []
memB = []
randSel = randint(1, 4)
memA.append(randSel)
if randSel == 1:
til1.rect.setFill("linen") #flashes the tile
time.sleep(0.3)
til1.rect.setFill("red3")
win.getMouse()
elif randSel == 2:
til2.rect.setFill("linen") #flashes the tile
time.sleep(0.3)
til2.rect.setFill("blue3")
win.getMouse()
elif randSel == 3:
til3.rect.setFill("linen") #flashes the tile
time.sleep(0.3)
til3.rect.setFill("green3")
win.getMouse()
elif randSel == 4:
til4.rect.setFill("linen") #flashes the tile
time.sleep(0.3)
til4.rect.setFill("yellow3")
win.getMouse()
"""while True:
if til1.active is True:
memB.append(1)
elif til2.active is True:
memB.append(2)
elif til3.active is True:
memB.append(3)
elif til4.active is True:
memB.append(4)
if memA == memB:
continue
else if memA != memB:
end = Text(Point(320, 240), "Game Over!")
end.setSize(22)
end.setFace("helvetica")
end.setFill("white")
end.setStyle("bold")
end.draw(win)"""
main()