This is driving me crazy, I am working on a circuit simulation program, and every time I ask a question about it it gets closed.
I seriously need help, but my questions get closed before anyone can help me answer them.
Anyways, here is the problem: Actually, I don't know what the problem is, there is something wrong with this code, and I have no idea what it is? It all looks fine, I can't find any bugs, but it just isn't working.
In this program, there are wires and power-sources, when I place a power-source beside a wire, I want it to become powered, and all connected wires to also be powered, but this program is showing very strange behavior and doing everything besides what I'd thought it would do. I wanted the wire to light up when there is a power source connected to it, and disable when there isn't. They light up when I place the power source, but when I place more wire, they all get disabled, and I can't seem to figure out why.
(dark red=powered black=not powered) Here it is when I place one wire beside the power source:
But then I add more and:
Here is the code:
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()
Please, please help me! I am very upset.