Python 内存输入

Python Memory Input

我的代码运行不正常,所以我希望有人可以查看它并告诉我哪里出了问题?它应该是一个记忆游戏,但我在让用户输入进入列表时遇到了问题。感谢任何帮助。

# 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()

我已经在下方重新设计了您的游戏,使其基本正常运行。我不认为它已经完成,只是功能性的。最显着的变化不是让瓷砖成为独立变量,而是让瓷砖成为一个数组。他们在tiles数组中的位置就是我们在memory数组中记住的位置。

我已尽可能简化代码以使其成为一个更好的示例——您可能想要添加回您的一些功能:

from math import sqrt
from random import randint
from graphics import *

class Tile:

    """
    A Tile is a colored rectangular in a window.
    It can be clicked on to return True.
    """

    def __init__(self, win, tl, br, color):
        """
        Creates a rectangular tile, eg:
            til1 = Tile('Games', p1(top left), p2(bottom right), 'red')
        """

        self.color = color

        xmin, ymin = tl.getX(), tl.getY()
        xmax, ymax = br.getX(), br.getY()

        self.p1 = Point(xmin, ymin)
        self.p2 = Point(xmax, ymax)

        self.rect = Rectangle(self.p1, self.p2)

        self.rect.setFill(self.color)
        self.rect.draw(win)

    def clicked(self, point):
        """ Returns true if p is inside """
        return self.p1.getX() <= point.getX() <= self.p2.getX() and \
                self.p1.getY() <= point.getY() <= self.p2.getY()

    def flash(self):
        self.rect.setFill('linen')  # flashes the tile
        time.sleep(0.3)
        self.rect.setFill(self.color)

def inCircle(circle, point):
    dx = point.getX() - circle.getCenter().getX()
    dy = point.getY() - circle.getCenter().getY()

    distance = sqrt(dx * dx + dy * dy)

    return distance <= circle.getRadius()

def main():
    win = GraphWin('Games', 640, 480)

    tiles = []

    center = Point(320, 240)
    p1 = Point(0, 0)
    tiles.append(Tile(win, p1, center, 'red3'))

    p2 = Point(320, 0)
    p3 = Point(640, 240)
    tiles.append(Tile(win, p2, p3, 'blue3'))

    p4 = Point(0, 240)
    p5 = Point(320, 480)
    tiles.append(Tile(win, p4, p5, 'green3'))

    p6 = Point(320, 240)
    p7 = Point(640, 480)
    tiles.append(Tile(win, p6, p7, 'yellow3'))

    circle = Circle(center, 75)
    circle.setFill('grey3')
    circle.draw(win)

    text = Text(Point(320, 240), 'PLAY!')
    text.setSize(24)
    text.setFill('white')
    text.setFace('helvetica')
    text.setStyle('bold')
    text.draw(win)

    while True:
        mouse = win.getMouse()

        if not inCircle(circle, mouse):
            continue

        memory = []
        score = 0
        text.setText(score)

        while True:
            randSel = randint(0, len(tiles) - 1)
            memory.append(randSel)

            tiles[randSel].flash()

            for count, selection in enumerate(memory, 1):
                mouse = win.getMouse()

                if tiles[selection].clicked(mouse):
                    if count > score:
                        score = count
                        text.setText(score)
                else:
                    text.setText('Game Over!')
                    time.sleep(1.0)
                    exit()

main()