井字游戏检查在我的代码末尾无法正常工作

时间:2020-09-02 12:34:20

标签: python python-3.x pygame tic-tac-toe

我正在尝试制作井字游戏,并且在代码的最后一部分中,假设检查一下circleList中的数字,但是,唯一按预期工作的检查是1,4,7。因为当我按下3、6、7、8、9方框时,该行立即显示出来,我不知道为什么

import pygame

pygame.init()
wn = pygame.display.set_mode((640, 840))
wn.fill((8, 140, 120))
pygame.display.set_caption("Tic-Tac-Toe")
First = pygame.draw.rect(wn, (20, 189, 172), (10, 210, 200,200))
Second = pygame.draw.rect(wn, (20, 189, 172), (220, 210, 200,200))
Third = pygame.draw.rect(wn, (20, 189, 172), (430, 210, 200,200))
Fourth = pygame.draw.rect(wn, (20, 189, 172), (10, 420, 200,200))
Fifth = pygame.draw.rect(wn, (20, 189, 172), (220, 420, 200,200))
Sixth = pygame.draw.rect(wn, (20, 189, 172), (430, 420, 200,200))
Seventh = pygame.draw.rect(wn, (20, 189, 172), (10,630, 200,200))
Eight = pygame.draw.rect(wn, (20, 189, 172), (220, 630, 200,200))
Ninth = pygame.draw.rect(wn, (20, 189, 172), (430, 630, 200,200))
Circle = False

Flag = True
First_Empty = True
Second_Empty = True
Third_Empty = True
Fourth_Empty = True
Fifth_Empty = True
Sixth_Empty = True
Seventh_Empty = True
Eight_Empty = True
Ninth_Empty = True

circleList = []
xList = []


while Flag:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Flag = False

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

            if First.collidepoint(pos) and First_Empty:
                if Circle:
                    pygame.draw.circle(wn,(8, 140, 120), (110, 310), 90, 20)
                    circleList.append(1)
                    Circle = False
                    First_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (40,230), (180,380),20)
                    pygame.draw.line(wn, (8, 140, 120), (180, 230), (40, 380), 20)
                    xList.append(1)
                    Circle = True
                    First_Empty = False

            if Second.collidepoint(pos) and Second_Empty:
                if Circle:
                    pygame.draw.circle(wn,(8, 140, 120), (320, 310), 90, 20)
                    circleList.append(2)
                    Circle = False
                    Second_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (250,230), (380,380),20)
                    pygame.draw.line(wn, (8, 140, 120), (380, 230), (250, 380), 20)
                    xList.append(2)
                    Circle = True
                    Second_Empty = False

            if Third.collidepoint(pos) and Third_Empty:
                if Circle:
                    pygame.draw.circle(wn,(8, 140, 120), (530, 310), 90, 20)
                    circleList.append(3)
                    Circle = False
                    Third_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (450,230), (600, 380), 20)
                    pygame.draw.line(wn, (8, 140, 120), (600, 230), (450, 380), 20)
                    xList.append(3)
                    Circle = True
                    Third_Empty = False

            if Fourth.collidepoint(pos) and Fourth_Empty:
                if Circle:
                    pygame.draw.circle(wn,(8, 140, 120), (110, 520), 90, 20)
                    Circle = False
                    circleList.append(4)
                    Fourth_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (40,440), (180,580),20)
                    pygame.draw.line(wn, (8, 140, 120), (180, 440), (40, 580), 20)
                    xList.append(4)
                    Circle = True
                    Fourth_Empty = False

            if Fifth.collidepoint(pos) and Fifth_Empty:
                if Circle:
                    pygame.draw.circle(wn,(8, 140, 120), (320, 520), 90, 20)
                    Circle = False
                    circleList.append(5)
                    Fifth_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (250,440), (380,590),20)
                    pygame.draw.line(wn, (8, 140, 120), (380, 440), (250, 590), 20)
                    xList.append(5)
                    Circle = True
                    Fifth_Empty = False

            if Sixth.collidepoint(pos) and Sixth_Empty:
                if Circle:
                    pygame.draw.circle(wn, (8, 140, 120), (530, 520), 90, 20)
                    Circle = False
                    circleList.append(6)
                    Sixth_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (450, 440), (600, 590), 20)
                    pygame.draw.line(wn, (8, 140, 120), (600, 440), (450, 590), 20)
                    xList.append(6)
                    Circle = True
                    Sixth_Empty = False

            if Seventh.collidepoint(pos) and Seventh_Empty:
                if Circle:
                    pygame.draw.circle(wn,(8, 140, 120), (110, 730), 90, 20)
                    Circle = False
                    circleList.append(7)
                    Seventh_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (40,650), (180,800),20)
                    pygame.draw.line(wn, (8, 140, 120), (180, 650), (40, 800), 20)
                    xList.append(7)
                    Circle = True
                    Seventh_Empty = False

            if Eight.collidepoint(pos) and Eight_Empty:
                if Circle:
                    pygame.draw.circle(wn,(8, 140, 120), (320, 730), 90, 20)
                    Circle = False
                    circleList.append(8)
                    Eight_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (250,650), (380,800),20)
                    pygame.draw.line(wn, (8, 140, 120), (380, 650), (250, 800), 20)
                    xList.append(8)
                    Circle = True
                    Eight_Empty = False

            if Ninth.collidepoint(pos) and Ninth_Empty:
                if Circle:
                    pygame.draw.circle(wn, (8, 140, 120), (530, 730), 90, 20)
                    Circle = False
                    circleList.append(9)
                    Ninth_Empty = False
                else:
                    pygame.draw.line(wn, (8, 140, 120), (450, 650), (600, 800), 20)
                    pygame.draw.line(wn, (8, 140, 120), (600, 650), (450, 800), 20)
                    xList.append(9)
                    Circle = True
                    Ninth_Empty = False

            if 1 and 2 and 3 in circleList:
                pygame.draw.line(wn, (208, 240, 192), (10, 305), (630, 305), 20)
            elif 1 and 5 and 9 in circleList:
                pygame.draw.line(wn, (208,240,192), (10, 305), (630, 305), 20)
            elif 1 and 4 and 7 in circleList:
                pygame.draw.line(wn, (208,240,192), (10, 305), (630, 305), 20)

            elif 2 and 5 and 8 in circleList:
                pygame.draw.line(wn, (208, 240, 192), (10, 305), (630, 305), 20)
            elif 3 and 5 and 7 in circleList:
                pygame.draw.line(wn, (208,240,192), (10, 305), (630, 305), 20)
            elif 3 and 6 and 9 in circleList:
                pygame.draw.line(wn, (208,240,192), (10, 305), (630, 305), 20)
            elif 4 and 5 and 6 in circleList:
                pygame.draw.line(wn, (208,240,192), (10, 305), (630, 305), 20)
            elif 7 and 8 and 9 in circleList:
                pygame.draw.line(wn, (208,240,192), (10, 305), (630, 305), 20)




    pygame.display.update()

pygame.quit()

我知道行打印方向错误,因此我想解决这个问题后确实可以使用某人的帮助

1 个答案:

答案 0 :(得分:1)

在全部if 1 and 2 and 3 in circleList:的行中,您应该尝试将其替换为if 1 in circleList and 2 in circleList and 3 in circleList:,因为说1 and 2 and 3 in circleList实际上只是意味着-如果1是一个值,如果两个是一个值如果circleList中有3,则继续。我希望您了解纠正的意思是您需要检查是否在circleList中是否为1,是否在circleList中为2,是否在3中。它也适用于其后的elifs

相关问题