Python 为什么这段代码不能正常工作?
这让我发疯,我正在做一个电路模拟程序,每次我问一个问题,它就会关闭 我真的需要帮助,但我的问题在有人能帮我回答之前就解决了 无论如何,问题是: 事实上,我不知道问题是什么,这段代码有问题,我不知道是什么?这一切看起来都很好,我找不到任何错误,但它就是不工作 在这个程序中,有电线和电源,当我把电源放在电线旁边时,我想让它通电,所有连接的电线也通电,但这个程序显示出非常奇怪的行为,除了我认为它会做的事情之外,它还做了所有事情。我希望电线在有电源连接时点亮,在没有电源连接时禁用。当我放置电源时,它们会亮起来,但当我放置更多的电线时,它们都会被禁用,我似乎不知道为什么 深红色=通电黑色=未通电 这里是我在电源旁边放一根电线的时候: 但接下来我要补充的是: 代码如下:Python 为什么这段代码不能正常工作?,python,pygame,circuit,Python,Pygame,Circuit,这让我发疯,我正在做一个电路模拟程序,每次我问一个问题,它就会关闭 我真的需要帮助,但我的问题在有人能帮我回答之前就解决了 无论如何,问题是: 事实上,我不知道问题是什么,这段代码有问题,我不知道是什么?这一切看起来都很好,我找不到任何错误,但它就是不工作 在这个程序中,有电线和电源,当我把电源放在电线旁边时,我想让它通电,所有连接的电线也通电,但这个程序显示出非常奇怪的行为,除了我认为它会做的事情之外,它还做了所有事情。我希望电线在有电源连接时点亮,在没有电源连接时禁用。当我放置电源时,它们会
import pygame
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((640,480))
blocks=[]
class PowerSource(object):
def __init__(self,pos):
self.posx=pos[0]
self.posy=pos[1]
self.rect=pygame.Rect(self.posx,self.posy,32,32)
self.powered=True
def update(self):
pygame.draw.rect(screen, (255,0,0), self.rect, 0)
def repos(self):
pass
class Circuit(object):
def __init__(self,pos):
self.powered=False
self.posx=pos[0]
self.posy=pos[1]
self.rect=pygame.Rect(self.posx,self.posy,32,32)
self.topped=False
self.lefted=False
self.righted=False
self.bottomed=False
def update(self):
self.powered=False
if any(b.rect.collidepoint(self.rect.left,self.rect.top-5) for b in [b for b in blocks if b is not self]):
if b.powered==True:
self.powered=True
if any(b.rect.collidepoint(self.rect.left,self.rect.top+38) for b in [b for b in blocks if b is not self]):
if b.powered==True:
self.powered=True
if any(b.rect.collidepoint(self.rect.left-5,self.rect.top) for b in [b for b in blocks if b is not self]):
if b.powered==True:
self.powered=True
if any(b.rect.collidepoint(self.rect.right+5,self.rect.top) for b in [b for b in blocks if b is not self]):
if b.powered==True:
self.powered=True
if not self.powered:
pygame.draw.rect(screen, (0,0,0), self.rect, 0)
else:
pygame.draw.rect(screen, (200,0,0), self.rect, 0)
while True:
place=1
screen.fill((255,255,255))
mse=pygame.mouse.get_pos()
mse=((mse[0]/32)*32,(mse[1]/32)*32)
pressed=pygame.mouse.get_pressed()
if pressed==(1,0,0):
pressed='L'
elif pressed==(0,0,1):
pressed='R'
for b in blocks:
b.update()
pygame.draw.rect(screen, (255,0,0), (mse[0],mse[1],32,32), 2)
for e in pygame.event.get():
if e.type==QUIT:
exit()
key=pygame.key.get_pressed()
if key[K_SPACE]:
for b in blocks:
if b.rect.collidepoint(mse):
place=0
if place==1:
blocks.append(PowerSource(mse))
if pressed=='L':
for b in blocks:
if b.rect.collidepoint(mse):
place=0
if place==1:
blocks.append(Circuit(mse))
elif pressed=='R':
for b in blocks:
if b.rect.collidepoint(mse):
blocks.remove(b)
pygame.display.flip()
请,请帮帮我!我很难过。这里有几个问题。首先,眼前的问题 在更新中,您认为b在这一行来自哪里
if b.powered==True:
它不是来自以下任何一个部分:
if any(b.rect.collidepoint(self.rect.left,self.rect.top-5) for b in [b for b in blocks if b is not self]):
^ ^
它来自此列表的最后一次迭代:
[b for b in blocks if b is not self]
如果b.powered==真测试,则块列表中的最后一个非自块用于所有测试。生成器表达式的循环变量在生成器表达式之外不可用,而列表理解的循环变量仅在列表理解之外可用,这是由于出于性能原因而做出的设计决策,并且在Python 3中已撤消
不要尝试在any调用外部使用b,而是将测试放在内部:
if any(b.powered and b is not self and b.rect.collidepoint(self.rect.left,self.rect.top-5) for b in blocks):
或者,因为这是一个相当大的行,所以将其拆分为一个显式循环,而不是一个any调用。在进行此操作时,您可以通过列表将4个任意呼叫合并为一个过程:
for b in blocks:
if not b.powered:
continue
if b is self:
# You don't actually need this test.
continue
adjacent = False
if b.rect.collidepoint(self.rect.left,self.rect.top-5):
adjacent = True
if b.rect.collidepoint(...):
adjacent = True
# and the other two adjacency checks
...
if adjacent:
self.powered = True
break
现在,还有其他问题。通电逻辑仅检查相邻块。这意味着,如果将一个块放置在远离电源的位置,然后连接,则该块可能需要多次更新才能实现其接收电源。此外,如果一个块与电源断开或电源被移除,该块可能永远不会关闭,因为无论何时,它的所有邻居都通电。这将需要更改您的算法。我建议使用来自电源的指示灯来确定哪些模块通电。尽量具体。你得到的确切错误信息是什么?除了我认为它会做的事情之外,做任何事情都是极其缺乏信息的。我们没什么好谈的。你生气/不安的事实与此无关。你的问题将一直被关闭,直到你费心根据发布。发布一个非常简单的例子,清楚地说明了这个问题。。。同样,如果b不是自性的话,那么b在[b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b为b!非常感谢。太糟糕了,很多人已经否决了这个问题。@SamTubb:老实说,在你编辑它之前,这是一个非常糟糕的问题。如果你在原始版本中包含编辑的信息,你会得到更好的回答。好的,在我继续之前的最后一个问题,我不明白为什么你要测试If not b.rect.collidatepointself.rect.left,self.rect.top-5:测试不在上面的块实际上做什么?@SamTubb:我认为这是我犯的错误。修正…@SamTubb:我说修正后又编辑了一次,因为我搞砸了两次。第二次,我忘了删除一些not。