【发布时间】:2013-11-05 11:33:15
【问题描述】:
我有一个 if 语句检查 .txt 文件的某一行是否为 == "true" ,但它似乎无法正常工作。这里:
configfile=open("FirstGameConfig.txt")
config_lines=configfile.readlines()
speed_of_object=float(config_lines[5])
acceleration_of_object=float(config_lines[7])
show_coordinates=str(config_lines[9]) #####
acceleration_mode=str(config_lines[11])
configfile.close()
这一切都在上面,show_coordinates 字符串似乎在下面表现不正常:
font=pygame.font.Font(None, 40)
if acceleration_mode=="true":
speedblit=font.render("Speed:", True, activeblitcolor)
screen.blit(speedblit, [0, 0])
rectyspeedtext=font.render(str(abs(rectyspeed)), True, activeblitcolor)
screen.blit(rectyspeedtext, [100, 0])
if show_coordinates=="true":
rectycoord=font.render(str(recty), True, activeblitcolor)
screen.blit(rectycoord, [360, 570])
rectxcoord=font.render(str(rectx), True, activeblitcolor)
screen.blit(rectxcoord, [217, 570])
coordblit=font.render("Coordinates: x= y=", True, activeblitcolor)
screen.blit(coordblit, [0, 570])
脚本检查加速模式是否开启。如果Acceleration_mode 的值为true,那么对象的速度将打印在屏幕的左上角。 activeblitcolor 已经定义好了,所以没问题。 if show_coordinates 语句下的内容将在屏幕左下角打印对象的坐标,假设我拥有的 .txt 文件中的值为“true”。
所以问题在于,即使在 .txt 文件中将 show_coordinates 设置为 true,也会跳过此语句。加速模式也在 .txt 文件中,并且可以完美运行。如果检查Acceleration_mode 是否为真的语句完美运行,为什么show_coordinates 语句不运行相同?如果我删除了 if 语句,但在脚本中保留了它下面的代码,那么坐标 DO 会打印在屏幕的左下角,就像 show_coordinates == "true" 时应该显示的那样。
我当然在 .txt 文件的正确行上有“true”。如果我添加“print(show_coordinates)”,那么“true”就是输出。脚本识别出 show_coordinates 的值为真,但 if 语句没有?任何帮助,将不胜感激!我是初学者。
【问题讨论】:
-
如果您发布 txt 文件会有所帮助
-
你能在终端上运行它并粘贴“print(show_coordinates)”的输出吗?既然你说输出是“真”,我怀疑正在发生转换错误
标签: python if-statement pygame