如何解决井字游戏检查在我的代码末尾无法正常工作
我正在尝试制作井字游戏,并且在代码的最后一部分中,假设检查一下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,(220,200))
Third = pygame.draw.rect(wn,(430,200))
Fourth = pygame.draw.rect(wn,420,200))
Fifth = pygame.draw.rect(wn,200))
Sixth = pygame.draw.rect(wn,200))
Seventh = pygame.draw.rect(wn,630,200))
Eight = pygame.draw.rect(wn,200))
Ninth = pygame.draw.rect(wn,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,120),(110,310),90,20)
circleList.append(1)
Circle = False
First_Empty = False
else:
pygame.draw.line(wn,(40,230),(180,380),20)
pygame.draw.line(wn,20)
xList.append(1)
Circle = True
First_Empty = False
if Second.collidepoint(pos) and Second_Empty:
if Circle:
pygame.draw.circle(wn,(320,20)
circleList.append(2)
Circle = False
Second_Empty = False
else:
pygame.draw.line(wn,(250,(380,20)
xList.append(2)
Circle = True
Second_Empty = False
if Third.collidepoint(pos) and Third_Empty:
if Circle:
pygame.draw.circle(wn,(530,20)
circleList.append(3)
Circle = False
Third_Empty = False
else:
pygame.draw.line(wn,(450,(600,20)
xList.append(3)
Circle = True
Third_Empty = False
if Fourth.collidepoint(pos) and Fourth_Empty:
if Circle:
pygame.draw.circle(wn,520),20)
Circle = False
circleList.append(4)
Fourth_Empty = False
else:
pygame.draw.line(wn,440),580),20)
xList.append(4)
Circle = True
Fourth_Empty = False
if Fifth.collidepoint(pos) and Fifth_Empty:
if Circle:
pygame.draw.circle(wn,20)
Circle = False
circleList.append(5)
Fifth_Empty = False
else:
pygame.draw.line(wn,590),20)
xList.append(5)
Circle = True
Fifth_Empty = False
if Sixth.collidepoint(pos) and Sixth_Empty:
if Circle:
pygame.draw.circle(wn,20)
Circle = False
circleList.append(6)
Sixth_Empty = False
else:
pygame.draw.line(wn,20)
xList.append(6)
Circle = True
Sixth_Empty = False
if Seventh.collidepoint(pos) and Seventh_Empty:
if Circle:
pygame.draw.circle(wn,730),20)
Circle = False
circleList.append(7)
Seventh_Empty = False
else:
pygame.draw.line(wn,650),800),20)
xList.append(7)
Circle = True
Seventh_Empty = False
if Eight.collidepoint(pos) and Eight_Empty:
if Circle:
pygame.draw.circle(wn,20)
Circle = False
circleList.append(8)
Eight_Empty = False
else:
pygame.draw.line(wn,20)
xList.append(8)
Circle = True
Eight_Empty = False
if Ninth.collidepoint(pos) and Ninth_Empty:
if Circle:
pygame.draw.circle(wn,20)
Circle = False
circleList.append(9)
Ninth_Empty = False
else:
pygame.draw.line(wn,20)
xList.append(9)
Circle = True
Ninth_Empty = False
if 1 and 2 and 3 in circleList:
pygame.draw.line(wn,(208,240,192),305),(630,20)
elif 1 and 5 and 9 in circleList:
pygame.draw.line(wn,20)
elif 1 and 4 and 7 in circleList:
pygame.draw.line(wn,20)
elif 2 and 5 and 8 in circleList:
pygame.draw.line(wn,20)
elif 3 and 5 and 7 in circleList:
pygame.draw.line(wn,20)
elif 3 and 6 and 9 in circleList:
pygame.draw.line(wn,20)
elif 4 and 5 and 6 in circleList:
pygame.draw.line(wn,20)
elif 7 and 8 and 9 in circleList:
pygame.draw.line(wn,20)
pygame.display.update()
pygame.quit()
我知道行打印方向错误,因此我想解决这个问题后确实可以使用某人的帮助
解决方法
在全部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
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。