在tic-tac-toe中使用Pygame和Python的else-if语句突然出现故障?
我们正在制作tic-tac-toe,我们使用了在tic-tac-toe中使用Pygame和Python的else-if语句突然出现故障?,python,pygame,tic-tac-toe,Python,Pygame,Tic Tac Toe,我们正在制作tic-tac-toe,我们使用了pygame。我们只是随意地改变坐标,但出于某种原因,在为第五个正方形编码后,它停止了工作?有人知道怎么回事吗?请参阅下面的代码。我们添加了几行代码,用于根据moue被点击的区域来确定文本“X”或“O”应该放在哪里。但是,由于某种原因(是什么?)最后一个elif语句永远不会工作,因此x或o永远不会出现。谢谢你花时间帮忙 import pygame pygame.init() width = 600 height = 600 Board = py
pygame
。我们只是随意地改变坐标,但出于某种原因,在为第五个正方形编码后,它停止了工作?有人知道怎么回事吗?请参阅下面的代码。我们添加了几行代码,用于根据moue被点击的区域来确定文本“X”或“O”应该放在哪里。但是,由于某种原因(是什么?)最后一个elif
语句永远不会工作,因此x或o永远不会出现。谢谢你花时间帮忙
import pygame
pygame.init()
width = 600
height = 600
Board = pygame.display.set_mode((width, height))
pygame.display.update()
running = True
pygame.draw.lines(Board, (255, 255, 153), False, [(0,200), (300,200), (600,200)], 1)
pygame.draw.lines(Board, (255, 255, 153), False, [(0,400), (300,400), (600,400)], 1)
pygame.draw.lines(Board,(255,255,153), False, [(200, 0), (200,300), (200,600)],1)
pygame.draw.lines(Board,(255,255,153), False, [(400, 0), (400,300), (400,600)],1)
pygame.display.update()
def text_objects(text, font):
textSurface = font.render(text, True, (255,255,153))
return textSurface, textSurface.get_rect()
def message_display(text, position):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = position
Board.blit(TextSurf, TextRect)
pygame.display.update()
def toX(tuple):
x = tuple[0]
return x
def toY(tuple):
y = tuple[1]
return y
def makeX(pos):
if toX(pos)<=200 and toY(pos)<=200:
position = (100, 100)
message_display(letter, position)
elif toX(pos)<=400 and toX(pos)>200:
if toY(pos) <= 200:
position = (300,100)
message_display(letter, position)
elif toX(pos)<=600 and toX(pos)>400:
if toY(pos) <= 200:
position = (500,100)
message_display(letter, position)
elif toX(pos)<=200:
if toY(pos) <= 400 and toY(pos) > 200:
position = (100,300)
message_display(letter, position)
x = 0
while (running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if x == 0:
letter = "X"
x = 1
else:
letter = "O"
x = 0
makeX(pos)
导入pygame
pygame.init()
宽度=600
高度=600
Board=pygame.display.set_模式((宽度、高度))
pygame.display.update()
运行=真
pygame.draw.lines(棋盘,(255,255,153),假,[(0200),(300200),(600200)],1)
pygame.draw.lines(棋盘,(255,255,153),假,[(0400),(300400),(600400)],1)
pygame.draw.lines(Board,(255255153),False,[(200,0),(200300),(200600)],1)
pygame.draw.lines(Board,(255255153),False,[(400,0),(400300),(400600)],1)
pygame.display.update()
def text_对象(文本、字体):
textSurface=font.render(文本,真,(255255153))
返回textSurface,textSurface.get_rect()
def信息_显示(文本、位置):
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text\u对象(text,largeText)
TextRect.center=位置
Board.blit(TextSurf,TextRect)
pygame.display.update()
def toX(元组):
x=元组[0]
返回x
def玩具(元组):
y=元组[1]
返回y
def makeX(位置):
如果toX(pos)我已经更新了你的makeX
函数,分离出x和y坐标的确定:
def makeX(pos):
if toX(pos)<=200:
pos_x = 100
elif toX(pos) <= 400:
pos_x = 300
else:
pos_x = 500
if toY(pos) <= 200:
pos_y = 100
elif toY(pos) <= 400:
pos_y = 300
else:
pos_y = 500
message_display(letter, (pos_x, pos_y))
def makeX(位置):
如果toX(pos)Akisaat,正如您可能意识到的,这个论坛不是为您的编程帮助请求而设置的。堆栈溢出更多是由经验丰富的程序员提出的技术问题。有一些网站适合你的学习水平。这个网站不是
话虽如此,就这一次,让我们看看您的代码,看看哪里出了问题
对于大多数正方形,顶部的elif测试捕捉X,但内部的Y测试仅适用于单个正方形,而Y的所有其他值都被忽略。结果,一旦elif测试捕捉到一个X值,并且找不到它可以使用的Y值,控制就会从if elif链中退出,并且这些方块永远不会被处理
例如,该行捕获中间列中的所有点击…
elif toX(pos)200:
..但是嵌套的“if”测试只捕获顶行所有其他方块丢失
当控件从if elif链中退出时。您需要先测试一行,然后测试该行中包含的所有三列。您只测试任何给定行中的一个正方形
但我建议采用另一种方法,将所有行和列测试放在一行上。这样,您就可以拥有独立的代码块,每个代码块对应一个唯一的方块,而不需要在任何单个代码块中进行太多嵌套
例如:
elif toX(pos)200和toY(pos)<200:#如果单击第1列第0行
然后在广场上做点什么
如果您使用注释命名方块,并使用print()命令生成报告,以便验证代码是否按计划运行,那么遵循程序逻辑将更容易。一旦代码得到验证,您就可以返回并删除这些行。在您的代码中尝试类似的内容
# As an aid, let's identify the squares for our logic block below
# | |
# Square [0,0] | Square [1,0] | Square [2,0]
# | |
# --------------------------------------
# | |
# Square [0,1] | Square [1,1] | Square [2,1]
# | |
# --------------------------------------
# | |
# Square [0,2] | Square [1,2] | Square [2,2]
# | |
def makeX(pos):
# Square [0,0]
if toX(pos)<=200 and toY(pos)<=200: # If the far left column and the top row is Clicked
print(" Square [0,0] Clicked")
position = (100, 100)
message_display(letter, position)
# Square [1,0]
elif toX(pos)<=400 and toX(pos)>200: # If the middle column is clicked
print(" Column 1 Clicked")
if toY(pos) <= 200: # And then if the top Row is clicked
print(" Row 0 Clicked")
position = (300,100)
message_display(letter, position)
# Square [2,0]
elif toX(pos)<=600 and toX(pos)>400 and toY9pos) <= 200: # If the far right Column
print(" Column 2 Clicked")
if toY(pos) <= 200: # And then if the top row is clicked
print(" Row 0 Clicked")
position = (500,100)
message_display(letter, position)
# Square [0,1]
elif toX(pos)<=200: # If the far left column is clicked
print(" Column 0 Clicked")
if toY(pos) <= 400 and toY(pos) > 200: # And then if the middle Row is Clicked
print(" Row 1 Clicked")
position = (100,300)
message_display(letter, position)
#作为一种帮助,让我们为下面的逻辑块确定正方形
# | |
#正方形[0,0]|正方形[1,0]|正方形[2,0]
# | |
# --------------------------------------
# | |
#正方形[0,1]|正方形[1,1]|正方形[2,1]
# | |
# --------------------------------------
# | |
#正方形[0,2]|正方形[1,2]|正方形[2,2]
# | |
def makeX(位置):
#平方[0,0]
如果toX(pos)“它永远不会工作”不是问题规范。请特别重复介绍之旅。您没有提供任何跟踪来支持您的子句“不起作用”。出现问题时,x
和y
的值是多少?请注意,您只覆盖了4个或可能的9个方块,没有处理其他情况,也没有完全单击棋盘。这就是为什么你看不到来自你的程序的任何帮助。