def setBlock(boxx, boxy):  #screen coords
    worldX, worldY = boxx + CameraBlocksX.get(), boxy + CameraBlocksY.get()
    blockType = SelectedBlockType.get()
    if blockType == 1:
        if (worldX, worldY) not in mainDict:
            WSet.add((worldX, worldY))
            mainDict[worldX, worldY] = WireBlock(
                boxx,
                boxy)  #send in screen coords to block object constructors
            mapWirePaths()  #wirePathMapping def here
    elif blockType == 2:  #left input transistor
        if (worldX, worldY) not in mainDict:
            TSet.add((worldX, worldY))
            mainDict[worldX, worldY] = TransistorBlock(LEFT, boxx, boxy, \
                LEFTSIDEX, LEFTSIDEY, UPSIDEX, UPSIDEY, RIGHTSIDEX, RIGHTSIDEY, DOWNSIDEX, DOWNSIDEY)
            mapWirePaths()  #wirePathMapping def here
    elif blockType == 3:  #right input transistor
        if (worldX, worldY) not in mainDict:
            TSet.add((worldX, worldY))
            mainDict[worldX, worldY] = TransistorBlock(RIGHT, boxx, boxy, \
                RIGHTSIDEX, RIGHTSIDEY, DOWNSIDEX, DOWNSIDEY, LEFTSIDEX, LEFTSIDEY, UPSIDEX, UPSIDEY)
            mapWirePaths()  #wirePathMapping def here
    elif blockType == 4:  #up input transistor
        if (worldX, worldY) not in mainDict:
            TSet.add((worldX, worldY))
            mainDict[worldX, worldY] = TransistorBlock(UP, boxx, boxy, \
                UPSIDEX, UPSIDEY, RIGHTSIDEX, RIGHTSIDEY, DOWNSIDEX, DOWNSIDEY, LEFTSIDEX, LEFTSIDEY)
            mapWirePaths()  #wirePathMapping def here
    elif blockType == 5:  #down input transistor
        if (worldX, worldY) not in mainDict:
            TSet.add((worldX, worldY))
            mainDict[worldX, worldY] = TransistorBlock(DOWN, boxx, boxy, \
                DOWNSIDEX, DOWNSIDEY, LEFTSIDEX, LEFTSIDEY, UPSIDEX, UPSIDEY, RIGHTSIDEX, RIGHTSIDEY)
            mapWirePaths()  #wirePathMapping def here
示例#2
0
 def repositionOnScreen(self):  #screen coords
     if self.onScreen:
         self.left, self.top = leftTopCoordsOfBox(
             self.boxx - CameraBlocksX.get(),
             self.boxy - CameraBlocksY.get()
         )  #screen coords, world coords. world converted to screen
         self.blockLocSize = (self.left, self.top, BoxSize.get(),
                              BoxSize.get())  #screen coords
         self.drawBlock()
 def repositionOnScreen(self):  #screen coords
     if self.onScreen:
         self.left, self.top = leftTopCoordsOfBox(
             self.boxx - CameraBlocksX.get(),
             self.boxy - CameraBlocksY.get()
         )  #screen coords, world coords. world converted to screen
         #self.blockLocSize = (self.left - 1, self.top - 1, TOTALBoxSize.get(), TOTALBoxSize.get()) #screen coords
         self.blockLocSize = (self.left, self.top, BoxSize.get(),
                              BoxSize.get())  #screen coords
         if self.state:  # == True:
             self.drawBlock(TRANSISTORBLOCKON)
         else:
             self.drawBlock(TRANSISTORBLOCKOFF)
示例#4
0
 def __init__(self, boxx, boxy):  #screen coords
     self.boxx, self.boxy = (
         boxx + CameraBlocksX.get(), boxy + CameraBlocksY.get()
     )  #world coords, screen coords. screen converted to world
     self.left, self.top = leftTopCoordsOfBox(boxx, boxy)  #screen coords
     self.inputBlockL = (LEFTSIDEX + self.boxx, self.boxy)  #world coords
     self.inputBlockR = (RIGHTSIDEX + self.boxx, self.boxy)  #world coords
     self.inputBlockU = (self.boxx, UPSIDEY + self.boxy)  #world coords
     self.inputBlockD = (self.boxx, DOWNSIDEY + self.boxy)  #world coords
     self.blockLocSize = (self.left, self.top, BoxSize.get(), BoxSize.get()
                          )  #screen coords
     self.onScreen = True
     self.processedThisTick = False
     self.state = OFF
     self.editCount = -1
     self.wirePathID = None  #must be set in edit
     self.drawBlock()
 def __init__(self, type, boxx, boxy, inputSideX, inputSideY, outputSideX1,
              outputSideY1, outputSideX2, outputSideY2, outputSideX3,
              outputSideY3):  #screen coords
     self.boxx, self.boxy = (
         boxx + CameraBlocksX.get(), boxy + CameraBlocksY.get()
     )  #world coords, screen coords. screen converted to world
     self.left, self.top = leftTopCoordsOfBox(boxx, boxy)  #screen coords
     self.inputBlock = (inputSideX + self.boxx, inputSideY + self.boxy
                        )  #world coords
     self.outputBlock1 = (outputSideX1 + self.boxx, outputSideY1 + self.boxy
                          )  #world coords
     self.outputBlock2 = (outputSideX2 + self.boxx, outputSideY2 + self.boxy
                          )  #world coords
     self.outputBlock3 = (outputSideX3 + self.boxx, outputSideY3 + self.boxy
                          )  #world coords
     self.blockLocSize = (self.left, self.top, BoxSize.get(), BoxSize.get()
                          )  #screen coords
     self.onScreen = True  #set to false if block is outside camera view
     self.previousState = None
     self.state = OFF
     self.wirePathIDs = set()
def deleteBlock(boxx, boxy):  #screen coords
    worldX, worldY = boxx + CameraBlocksX.get(), boxy + CameraBlocksY.get()
    if (worldX, worldY) in mainDict:
        if (worldX, worldY) in TSet:
            transistorsPaths = mainDict[worldX, worldY].getWirePaths()
            for path in transistorsPaths:
                wirePathDict[path].remTrans((worldX, worldY))
                if wirePathDict[path].getTransTotal() == 0:
                    wirePathDict[path].turnOff()
            TSet.remove((worldX, worldY))
        else:
            try:  #if no key don't worry, this is for orphaned wirepaths (wirepaths with no transistor outputs attached)
                wirePathDict[mainDict[worldX,
                                      worldY].getWirePathID()].turnOff()
                WireBlock.setWireBlockDeleted(True)
            except KeyError:
                pass
            WSet.remove((worldX, worldY))
        del mainDict[worldX, worldY]
        left, top = leftTopCoordsOfBox(boxx, boxy)  #screen coords
        #drawRect(EMPTYBLOCKBORDER, (left - 1, top - 1, TOTALBoxSize.get(), TOTALBoxSize.get())) #clears yellow edge left from transistor, prevents haveing to redraw background
        drawRect(EMPTYBLOCK, (left, top, BoxSize.get(), BoxSize.get()))
        mapWirePaths()  #wirePathMapping def here
示例#7
0
def checkOnScreen(loc):  #checks if block is on screen
    return (loc[0] >= CameraBlocksX.get() and loc[0] <= CameraBlocksX.get() + BoardWidth.get() - 1) and \
            (loc[1] >= CameraBlocksY.get() and loc[1] <= CameraBlocksY.get() + BoardHeight.get() - 1 - (TOPBAR / BoxSize.get()))
def main():
    PROCESSRATE = 1  #process cycles at a set rate in milliseconds
    elapsedTime = 0  #ammount of time in milliseconds since last procress cycle

    leftMouseDown = False
    middleMouse = False
    rightMouseDown = False

    firstMoveMiddle = True  #set false after initial move with middle mouse down

    movementTotalX = 0
    movementTotalY = 0
    totalLeft = 0
    totalRight = 0
    totalUp = 0
    totalDown = 0

    pygame.init()

    FPSCLOCK = pygame.time.Clock()

    fontObj = pygame.font.Font('freesansbold.ttf', 25)
    textSurfaceObj = fontObj.render('FPS', True, YELLOW)
    textRectObj = textSurfaceObj.get_rect()
    textRectObj.center = (33, 25)

    mousex = 0  # used to store x coordinate of mouse event
    mousey = 0  # used to store y coordinate of mouse event

    boxx, boxy = None, None

    pygame.display.set_caption('Logic Simulator')

    #drawRect(BGCOLOR, GRIDBACKGROUND)
    drawBorder()

    #drawFullBoardTest()

    while True:  # main game loop
        for event in pygame.event.get():  # event handling loop
            if event.type == QUIT or (event.type == KEYUP
                                      and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            elif event.type == KEYUP:
                if event.key == K_1:
                    SelectedBlockType.set(1)
                    print('selected wire blocks')
                elif event.key == K_2:
                    SelectedBlockType.set(2)
                    print('selected left transistor blocks')
                elif event.key == K_3:
                    SelectedBlockType.set(3)
                    print('selected right transistor blocks')
                elif event.key == K_4:
                    SelectedBlockType.set(4)
                    print('selected up transistor blocks')
                elif event.key == K_5:
                    SelectedBlockType.set(5)
                    print('selected down transistor blocks')
                elif event.key == K_6:
                    pass
                    #print(getCtrBlockWorldCoords())
            elif event.type == MOUSEMOTION:
                if leftMouseDown ^ rightMouseDown:  #(Xor)prevents motion with none or both buttons pressed from triggering events
                    mousex, mousey = event.pos
                    boxx, boxy = getBoxAtPixel(mousex, mousey)
                if middleMouse:
                    if firstMoveMiddle is False:
                        relativePosition = pygame.mouse.get_rel()
                        movementTotalX += relativePosition[0]
                        movementTotalY += relativePosition[1]
                    if relativePosition[0] < 0:
                        totalLeft += abs(relativePosition[0])
                        totalRight = 0
                    if relativePosition[0] > 0:
                        totalRight += abs(relativePosition[0])
                        totalLeft = 0
                    if relativePosition[1] < 0:
                        totalUp += abs(relativePosition[1])
                        totalDown = 0
                    if relativePosition[1] > 0:
                        totalDown += abs(relativePosition[1])
                        totalUp = 0
                    if totalLeft >= BoxSize.get():
                        #CameraBlocksX.setPix(CameraBlocksX.get() + totalLeft)
                        CameraBlocksX.inc()
                        totalLeft = 0
                        clearDrawArea()
                        moveBlocks()
                    elif totalRight >= BoxSize.get():
                        #CameraBlocksX.setPix(CameraBlocksX.get() - totalRight)
                        CameraBlocksX.dec()
                        totalRight = 0
                        clearDrawArea()
                        moveBlocks()
                    if totalUp >= BoxSize.get():
                        #CameraBlocksY.setPix(CameraBlocksY.get() + totalUp)
                        CameraBlocksY.inc()
                        totalUp = 0
                        clearDrawArea()
                        moveBlocks()
                    elif totalDown >= BoxSize.get():
                        #CameraBlocksY.setPix(CameraBlocksY.get() - totalDown)
                        CameraBlocksY.dec()
                        totalDown = 0
                        clearDrawArea()
                        moveBlocks()
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    mousex, mousey = event.pos
                    boxx, boxy = getBoxAtPixel(mousex, mousey)
                    leftMouseDown = True
                elif event.button == 2:
                    middleMouse = True
                    firstMoveMiddle = False
                    relativePosition = pygame.mouse.get_rel(
                    )  #clears built up relative movements
                    relativePosition = (0, 0)
                    movementTotalX = 0
                    movementTotalY = 0
                    totalLeft = 0
                    totalRight = 0
                    totalUp = 0
                    totalDown = 0
                elif event.button == 3:
                    mousex, mousey = event.pos
                    boxx, boxy = getBoxAtPixel(mousex, mousey)
                    rightMouseDown = True
                elif event.button == 4:
                    clearDrawArea()
                    zoomIn()
                    zoomBlocks()
                elif event.button == 5:
                    clearDrawArea()
                    zoomOut()
                    zoomBlocks()
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1:  #add block
                    leftMouseDown = False
                elif event.button == 2:  #move camera
                    middleMouse = False
                    firstMoveMiddle = True
                elif event.button == 3:  #delete block
                    rightMouseDown = False

        if boxx != None and boxy != None:
            # The mouse is currently over a box.
            if leftMouseDown and rightMouseDown == False:  #prevents event if both buttons pressed
                setBlock(boxx, boxy)
            elif rightMouseDown and leftMouseDown == False:
                deleteBlock(boxx, boxy)

        #TRANSISTOR AND WIRE LOGIC PROCESSING HERE
        elapsedTime += FPSCLOCK.get_time()
        if elapsedTime >= PROCESSRATE:
            elapsedTime = 0

            if WireBlock.getWireBlockDeleted(
            ) == False:  #if wireBlockDeleted is true then do not process transistors this tick
                for operateTransistorKey in TSet:
                    mainDict[operateTransistorKey].Operation()
            else:  #if True
                WireBlock.setWireBlockDeleted(False)

            for processWirePathsKey in wirePathDict:
                wirePathDict[processWirePathsKey].processWirePath()
            #turn off every wire, then turn on only the wires that are comming on or staying on in this cycle that way we wont need to specifically turn off wires
            #for turnOffWiresKey in WSet:
            #    mainDict[turnOffWiresKey].turnOff()

            #process wires here instead of in transistor objects
            #for processWiresKey in TSet: #loop through all transistors
            #    if mainDict[processWiresKey].getState(): #check if current transistor is switched ON, if OFF skip

            #        #ask current transistor to return list of connected wires that have not been processed this tick (add def to transistor and wire classes that filters these wires)
            #        #add returned list to set/queue
            #        for transistorWire in mainDict[processWiresKey].returnConnectedUnprocessedWires():
            #            unprocessedWires.add(transistorWire)
            #        #loop through each wire in set
            #        while unprocessedWires: #while set/queue is not empty:
            #            currentWire = unprocessedWires.pop()
            #            mainDict[currentWire].turnOn() #turn on current wire setTick/processed-boolean
            #            for wire in mainDict[currentWire].returnConnectedUnprocessedWires(): #ask current wire to return list of surrounding unprocessed wires and add to set
            #                unprocessedWires.add(wire)
            #doDraws = True

        #draw only the blocks in the current draw area here
        #if doDraws == True:
        #    for drawWire in WSet:
        #        draw = mainDict[drawWire].getDrawTuple()
        #        pygame.draw.rect(DISPLAYSURF, draw[0], draw[1])
        #    for drawTransistor in TSet:
        #        draw = mainDict[drawTransistor].getDrawTuple()
        #        pygame.draw.rect(DISPLAYSURF, draw[0], draw[1])
        #    doDraws = False
        #Redraw the screen and wait a clock tick.

        if WireBlock.getWireBlockDeleted(
        ) == False:  #if wireBlockdeleted is true do not increment tick this tick or update display
            #drawBoard() #new draw function for combined rects
            pygame.display.update()
            #pygame.display.flip()

        FPSCLOCK.tick(FPS)
        drawFPSRect(BLACK, (0, 0, 78, 36))  #remove last blitted FPS
        textSurfaceObj = fontObj.render('%.2f' % FPSCLOCK.get_fps(), True,
                                        YELLOW)
        blitToSurf(textSurfaceObj, textRectObj)