-4

这让我发疯了,我正在开发一个电路模拟程序,每次我问一个关于它的问题时它都会关闭。

我真的需要帮助,但在任何人帮助我回答之前,我的问题就已经解决了。

不管怎样,问题出在这里:其实,我不知道问题出在哪里,这段代码有问题,我也不知道是什么问题?一切看起来都很好,我找不到任何错误,但它只是不起作用。

在这个程序中,有电线和电源,当我将电源放在电线旁边时,我希望它通电,并且所有连接的电线也都通电,但是这个程序表现出非常奇怪的行为并且做所有事情除了我认为它会做的事情。我希望电线在有电源连接时亮起,并在没有电源时禁用。当我放置电源时它们会亮起,但是当我放置更多电线时,它们都会被禁用,我似乎无法弄清楚为什么。

(深红色=通电黑色=未通电)这是当我在电源旁边放置一根电线时:

在此处输入图像描述

但后来我添加了更多:

在此处输入图像描述

这是代码:

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()

拜托,请帮帮我!我非常失望。

4

1 回答 1

2

这里有几个问题。首先,眼前的问题。

update,你认为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]

块列表中的最后一个非自我块用于所有if b.powered == True测试。生成器表达式的循环变量在生成器表达式之外不可用,并且列表推导的循环变量仅在列表推导之外可用,这是由于出于性能原因而做出的设计决定,并且在 Python 3 中被撤消。

不要尝试在通话b之外使用,而是将测试放在里面:any

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 个any调用合并为一次遍历列表:

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

现在,其他问题。您的加电逻辑仅检查相邻块。这意味着如果将一个模块与电源分开放置然后连接,则该模块可能需要多次更新才能实现它正在接收电源。此外,如果一个块与电源断开连接或电源被移除,该块可能永远不会关闭,因为无论何时,它的所有邻居都已通电。这将需要更改您的算法。我建议使用来自电源的洪水填充来确定哪些块被供电。

于 2014-02-16T22:53:33.230 回答