2017-04-21 83 views
0

我的代碼工作不正常,所以我希望有人可以看看它,告訴我什麼是錯的?這應該是一個記憶遊戲,但我有用戶輸入進入列表的問題。任何幫助表示讚賞。Python內存輸入

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

順便說一句,對不起任何壞的編碼。我只是想讓它工作得很好。 – Shihab

+0

請閱讀[如何創建最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)和[如何提問](https://stackoverflow.com/help/how-to-問)。謝謝! – UnsignedByte

回答

0

我已經對下面的遊戲進行了重新設計,以使其基本工作。我不認爲它完成,只是功能。最重要的變化並不是使瓦片變成單個變量,而是使用瓦片數組。他們在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()