Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中向内置类添加属性?(用于检测tic tac toe中的win)_Python_Loops_Class_Pygame_Attributes - Fatal编程技术网

在Python中向内置类添加属性?(用于检测tic tac toe中的win)

在Python中向内置类添加属性?(用于检测tic tac toe中的win),python,loops,class,pygame,attributes,Python,Loops,Class,Pygame,Attributes,我正在用Pygame用Python编写一个tic-tac-toe游戏。我需要在我的循环中检查是否有任何赢家连续或对角3。我使用Pygame中内置的for循环和矩形位置类绘制零和十字 例如,是否可以将我自己的属性设置为内置类pygame.rect sq1.isFull = True …即使这不是rect的内置方法 如果不是,如果我所有的方块和圆圈都是在for循环中生成的,我将如何检查赢家 代码如下所示: import pygame # initialize pygame pygame.init

我正在用Pygame用Python编写一个tic-tac-toe游戏。我需要在我的循环中检查是否有任何赢家连续或对角3。我使用Pygame中内置的for循环和矩形位置类绘制零和十字

例如,是否可以将我自己的属性设置为内置类pygame.rect

sq1.isFull = True
…即使这不是rect的内置方法

如果不是,如果我所有的方块和圆圈都是在for循环中生成的,我将如何检查赢家

代码如下所示:

import pygame

# initialize pygame
pygame.init()

# window setup
screen = pygame.display.set_mode((550, 550))
pygame.display.set_caption('Tic Tac Toe by Donnaven')

# squares
sq1 = pygame.draw.rect(screen, (255, 255, 255), (25, 25, 150, 150))
sq2 = pygame.draw.rect(screen, (255, 255, 255), (200, 25, 150, 150))
sq3 = pygame.draw.rect(screen, (255, 255, 255), (375, 25, 150, 150))

sq4 = pygame.draw.rect(screen, (255, 255, 255), (25, 200, 150, 150))
sq5 = pygame.draw.rect(screen, (255, 255, 255), (200, 200, 150, 150))
sq6 = pygame.draw.rect(screen, (255, 255, 255), (375, 200, 150, 150))

sq7 = pygame.draw.rect(screen, (255, 255, 255), (25, 375, 150, 150))
sq8 = pygame.draw.rect(screen, (255, 255, 255), (200, 375, 150, 150))
sq9 = pygame.draw.rect(screen, (255, 255, 255), (375, 375, 150, 150))

square_list = [sq1, sq2, sq3, sq4, sq5, sq6, sq7, sq8, sq9]

# game loop
pTurn = True  # Whose turn it is.
# True = Rectangle, False = Circle
running = True

while running:
    pygame.time.delay(100)

    # checking for events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONUP:
            mouse_pos = pygame.mouse.get_pos()

            for square in square_list:
                if square.collidepoint(mouse_pos):
                    square_x = square.x
                    square_y = square.y

                    # draw shape inside of square
                    if pTurn:  # rectangle player turn
                        pygame.draw.rect(screen, (255, 0, 0), (square_x + 25, square_y + 25, 100, 100))
                        pTurn = False
                    elif not pTurn:  # circle player turn
                        pygame.draw.circle(screen, (0, 255, 0), (square_x + 75, square_y + 75), 60)
                        pTurn = True

    # update frame (DONE LAST!)
    pygame.display.update()

如果要向类添加属性,请使用。使用pygame.Rect子类创建MyRect类:

类MyRectpygame.Rect: def uuu init uuu self,x,y,w,h,isFull=真: 超级。uuu初始uuu x,y,w,h self.isFull=isFull

但是,您也应该考虑创建一个聚合PyGoad的类的类。 MyTile类: def uuu init uuu self,x,y,w,h,isFull=真: self.rect=pygame.Rectx,y,w,h self.isFull=isFull 这将允许您使MyTile成为以下内容的子类:

MyTilepygame.sprite.sprite类: def uuu init uuu self,x,y,w,h,isFull=真: 超级__ self.rect=pygame.Rectx,y,w,h self.isFull=isFull self.image=pygame.Surfaceself.rect.size self.image.fill255,0,0只是一个例子
如果要向类添加属性,请使用。使用pygame.Rect子类创建MyRect类:

类MyRectpygame.Rect: def uuu init uuu self,x,y,w,h,isFull=真: 超级。uuu初始uuu x,y,w,h self.isFull=isFull

但是,您也应该考虑创建一个聚合PyGoad的类的类。 MyTile类: def uuu init uuu self,x,y,w,h,isFull=真: self.rect=pygame.Rectx,y,w,h self.isFull=isFull 这将允许您使MyTile成为以下内容的子类:

MyTilepygame.sprite.sprite类: def uuu init uuu self,x,y,w,h,isFull=真: 超级__ self.rect=pygame.Rectx,y,w,h self.isFull=isFull self.image=pygame.Surfaceself.rect.size self.image.fill255,0,0只是一个例子
不是直接的,但是您可以创建一个包装类,a-la组合

类别磁贴: 定义初始自我,正方形: self.square=square self.is_full=False 方形列表=[Tilesq1,…Tilesqn] 对于方形列表中的平铺: 正方形 如果square.collidatepointmouse_pos而非tile.is_已满: ... tile.is_full=真
或者看看@rabbi76关于继承的答案。给猫剥皮的方法很多。

不是直接剥皮,但是您可以创建一个包装类a-la composition

类别磁贴: 定义初始自我,正方形: self.square=square self.is_full=False 方形列表=[Tilesq1,…Tilesqn] 对于方形列表中的平铺: 正方形 如果square.collidatepointmouse_pos而非tile.is_已满: ... tile.is_full=真
或者看看@rabbi76关于继承的答案。给猫剥皮的方法有很多。

有时间我会试试这个,谢谢。但是,我经常看到“包装器”这个词。这是什么意思?这里我用它作为一个全包术语来表示一个类,用来代替包含的对象。离真实事物只有一个抽象层次。啊,谢谢你的澄清。我很快就会实施!一旦我写完,我一定要把它标为答案。谢谢你抽出时间!没问题!再看一看@rabbi76的答案,它展示了良好的风格,从长远来看可能会带来更干净的代码。我按照您的建议修改了代码,但现在它只显示if circles:下面是代码:我会在有时间的时候尝试一下,谢谢。但是,我经常看到“包装器”这个词。这是什么意思?这里我用它作为一个全包术语来表示一个类,用来代替包含的对象。离真实事物只有一个抽象层次。啊,谢谢你的澄清。我很快就会实施!一旦我写完,我一定要把它标为答案。谢谢你抽出时间!没问题!再看一看@rabbi76的答案,它展示了良好的风格,从长远来看可能会带来更干净的代码。我按照您的建议修改了代码,但现在它只显示if circles:下面是代码: