为什么这个代码不能正常工作?

2024-06-25 23:53:48 发布

您现在位置:Python中文网/ 问答频道 /正文

这让我抓狂,我在做一个电路模拟程序,每次我问一个问题,它就会关闭。你知道吗

我真的需要帮助,但我的问题在别人帮我回答之前就解决了。你知道吗

不管怎样,问题是: 实际上,我不知道问题出在哪里,这个代码有问题,我不知道是什么?一切看起来都很好,我找不到任何错误,但它就是不工作。你知道吗

在这个程序中,有电线和电源,当我把电源放在电线旁边时,我想让它通电,所有连接的电线也通电,但是这个程序显示出非常奇怪的行为,除了我认为它会做的以外,它做了所有的事。当有电源连接时,我想让电线亮起来,没有电源时,电线就断开。当我放置电源时,电线就会亮起来,但是当我放置更多电线时,电线就会断开,我似乎不知道为什么。你知道吗

(暗红色=通电黑色=未通电) 当我把一根电线放在电源旁边时:

enter image description here

但我补充了更多:

enter image description here

代码如下:

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

求求你,求求你帮帮我!我很难过。你知道吗


Tags: inposrectselftrueforifpygame
1条回答
网友
1楼 · 发布于 2024-06-25 23:53:48

这里有几个问题。第一,眼前的问题。你知道吗

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测试。生成器表达式的循环变量在生成器表达式之外不可用,而列表理解的循环变量仅在列表理解之外可用,这是由于出于性能原因而做出的设计决策,在Python3中未执行。你知道吗

不要试图在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调用。当您正在进行时,您可以将4any调用合并到列表上的一个过程中:

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

现在,其他的问题。加电逻辑只检查相邻的块。这意味着,如果一个块被放置在远离电源,然后连接,它可能需要许多更新的块,以实现它的接收功率。此外,如果一个块与电源断开或电源被移除,该块可能永远不会关闭,因为无论何时,它的所有邻居都通电。这将需要更改您的算法。我建议使用来自电源的flood fill来确定哪些块被供电。你知道吗

相关问题 更多 >