Skip to content

Commit fc1d8db

Browse files
committed
- Default to Python3
- Lots of PEP8 compliance goodness - New-style string formatting - print is now print() - raw_input is now input(). Eliminated MyInput
1 parent 28430fb commit fc1d8db

File tree

16 files changed

+512
-491
lines changed

16 files changed

+512
-491
lines changed

basics.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
#
4-
# Logic Circuits
4+
# Logic Circuits - basics.py
5+
# Show off the basic gates in a simple circuit
56
#
67
# Copyleft 2015 Chris Meyers <[email protected]> 2015.12.13
78
#
@@ -20,7 +21,6 @@
2021
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
2122
# Boston, MA 02110-1301, USA.
2223
#
23-
#
2424

2525
from __future__ import print_function
2626
from six.moves import input # use raw_input when I say input
@@ -34,41 +34,39 @@
3434
__maintainer__ = "Chris Meyers, Jeff Elkner, Kevin Cole"
3535
__email__ = "[email protected]"
3636
__status__ = "Development" # "Prototype", "Development" or "Production"
37-
__appname__ = "Logic Circuits"
37+
__appname__ = "Logic Circuits - Basic"
3838

39-
# basics.py
40-
# Show off the basic gates in a simple circuit
39+
from gates import MultPuls, Nand2, Inv, Swt
40+
from circuit import Circuit
4141

42-
from gates import MultPuls, Nand2, Inv, Swt
43-
from circuit import Circuit
4442

45-
class TestBasic1 (Circuit) :
46-
def setupGraphics(self) :
43+
class TestBasic1(Circuit):
44+
def setupGraphics(self):
4745
self.banner = "Basic components. Switch, Multipulsar, Nand, Inverter"
4846

49-
n1 = Nand2(self,"N1", self.scale1((50, 30))) # Nand Gate
50-
i1 = Inv (self,"I1") # Inverter
51-
s1 = Swt (self,"S1") # Switch feeding input A
52-
m1 = MultPuls (self,"MP1") # Pulsar feeding input B
47+
n1 = Nand2(self, "N1", self.scale1((50, 30))) # Nand Gate
48+
i1 = Inv(self, "I1") # Inverter
49+
s1 = Swt(self, "S1") # Switch feeding input A
50+
m1 = MultPuls(self, "MP1") # Pulsar feeding input B
5351

54-
s1.align(s1.B, n1.A, -50, 0) # line up the gates
52+
s1.align(s1.B, n1.A, -50, 0) # line up the gates
5553
m1.align(m1.B, n1.B, -30, 0)
56-
i1.align(i1.B, n1.C, 80, 0) # inverter follows Nand
54+
i1.align(i1.B, n1.C, 80, 0) # inverter follows Nand
5755

58-
n1.A.labelQuad = 2 # Label all connectors
56+
n1.A.labelQuad = 2 # Label all connectors
5957
n1.B.labelQuad = 2
6058
n1.C.labelQuad = 1
6159
i1.A.labelQuad = 2
6260
i1.B.labelQuad = 1
6361

64-
self.gates = (m1,s1,n1,i1)
62+
self.gates = (m1, s1, n1, i1)
6563
s1.B.addWire(n1.A)
6664
m1.B.addWire(n1.B)
6765
n1.C.addWire(i1.A)
6866
i1.B.addWire( (i1.B.x(30), i1.B.y())) # tail to see
6967

70-
if __name__ == "__main__" :
68+
if __name__ == "__main__":
7169
import gameloop
7270
from settings import CmdScale
73-
circuit = TestBasic1 (None, "TestBasic1",(100,100),scale=CmdScale)
71+
circuit = TestBasic1(None, "TestBasic1", (100, 100), scale=CmdScale)
7472
gameloop.gameloop(circuit)

circuit.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
#
4-
# Logic Circuits
4+
# Logic Circuits - circuit.py
55
#
66
# Copyleft 2015 Chris Meyers <[email protected]> 2015.12.13
77
#
@@ -20,7 +20,6 @@
2020
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
2121
# Boston, MA 02110-1301, USA.
2222
#
23-
#
2423

2524
from __future__ import print_function
2625
from six.moves import input # use raw_input when I say input
@@ -36,36 +35,39 @@
3635
__status__ = "Development" # "Prototype", "Development" or "Production"
3736
__appname__ = "Logic Circuits"
3837

39-
# circuit.py
40-
4138
import pygame as pg
4239
from component import Component
4340
from text import textDraw, textFont
4441
from settings import GREEN, BLUE
4542

46-
class Circuit(Component) :
47-
def computeOutput(self) :
48-
for gate in self.gates :
43+
44+
class Circuit(Component):
45+
def computeOutput(self):
46+
for gate in self.gates:
4947
gate.computeOutput()
5048
gate.sendOutput()
5149

52-
def checkClicked(self, pos) :
53-
for gate in self.gates :
50+
def checkClicked(self, pos):
51+
for gate in self.gates:
5452
gate.checkClicked(pos)
55-
56-
def draw(self, screen) :
57-
if self.hidden() : return
58-
font = pg.font.Font(None,12)
5953

60-
if self.encapsulated() :
61-
bx1,bx2,bx3,bx4 = self.scaleM((0,0),(20,0),(20,40),(0,40))
62-
pg.draw.polygon(screen,BLUE,(bx1,bx2,bx3,bx4),1)
63-
textDraw(screen, self.scale1((3,3)), BLUE, self.name, quad=4)
64-
elif not self.hidden() :
65-
if self.banner :
66-
textDraw(screen, self.scale1((0,0)), GREEN, self.banner,
67-
quad=1, font=textFont(24))
54+
def draw(self, screen):
55+
if self.hidden():
56+
return
57+
font = pg.font.Font(None, 12)
58+
59+
if self.encapsulated():
60+
bx1, bx2, bx3, bx4 = self.scaleM(( 0, 0),
61+
(20, 0),
62+
(20, 40),
63+
( 0, 40))
64+
pg.draw.polygon(screen, BLUE, (bx1, bx2, bx3, bx4), 1)
65+
textDraw(screen, self.scale1((3, 3)), BLUE, self.name, quad=4)
66+
elif not self.hidden():
67+
if self.banner:
68+
textDraw(screen, self.scale1((0, 0)), GREEN, self.banner,
69+
quad=1, font=textFont(24))
6870
gates = self.gates
69-
for g in gates :
71+
for g in gates:
7072
g.output.drawWires(screen)
7173
g.draw(screen)

component.py

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
#
4-
# Logic Circuits
4+
# Logic Circuits - component.py
55
#
66
# Copyleft 2015 Chris Meyers <[email protected]> 2015.12.13
77
#
@@ -20,7 +20,6 @@
2020
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
2121
# Boston, MA 02110-1301, USA.
2222
#
23-
#
2423

2524
from __future__ import print_function
2625
from six.moves import input # use raw_input when I say input
@@ -36,12 +35,11 @@
3635
__status__ = "Development" # "Prototype", "Development" or "Production"
3736
__appname__ = "Logic Circuits"
3837

39-
# component.py
40-
#
4138
import settings as set
4239

43-
class Component :
44-
def __init__(self, parent, name, pos=(0,0), scale=1) :
40+
41+
class Component:
42+
def __init__(self, parent, name, pos=(0, 0), scale=1):
4543
self.parent = parent
4644
self.kids = ()
4745
self.name = name
@@ -50,65 +48,76 @@ def __init__(self, parent, name, pos=(0,0), scale=1) :
5048
self.layer = 1
5149
self.value = 1
5250
self.banner = ""
53-
if parent :
51+
if parent:
5452
self.scale *= parent.scale
55-
self.layer = parent.layer+1
53+
self.layer = parent.layer + 1
5654
parent.kids += (self,)
5755
self.setupGraphics()
5856

59-
def x(s, val=0) : return s.pos[0]+val*s.scale
60-
def y(s, val=0) : return s.pos[1]+val*s.scale
57+
def x(s, val=0):
58+
return s.pos[0] + val * s.scale
59+
60+
def y(s, val=0):
61+
return s.pos[1] + val * s.scale
6162

62-
def align(s,myCon,pivotCon, xoff, yoff) :
63+
def align(s, myCon, pivotCon, xoff, yoff):
6364
# Align so that seperation of 2 connectors is (xoff,yoff)
6465
pivX, pivY = pivotCon.pos
6566
oldX, oldY = myCon.pos
66-
xoff *= s.scale ; yoff *= s.scale
67-
newX = pivX+xoff; newY = pivY+yoff
68-
xmov = newX-oldX; ymov = newY-oldY
67+
xoff *= s.scale
68+
yoff *= s.scale
69+
newX = pivX + xoff
70+
newY = pivY + yoff
71+
xmov = newX - oldX
72+
ymov = newY - oldY
6973
s.scootOver(xmov, ymov)
7074

71-
def encapsulated(s) :
75+
def encapsulated(s):
7276
return s.layer == set.CmdEncapsuleLayer
7377

74-
def hidden(s) :
78+
def hidden(s):
7579
return s.layer > set.CmdEncapsuleLayer
7680

77-
def scaleM(self, *args) : # scale multiple args
81+
def scaleM(self, *args): # scale multiple args
7882
return map(self.scale1, args)
7983

80-
def scale1(s, arg) : # transform single point or scalar
81-
if type(arg) == type(5) :
82-
return arg*s.scale
83-
elif type(arg) == type((4,)) or type(arg)==type([4]) :
84-
return posAdd(s.pos,posMult(arg,s.scale))
84+
def scale1(s, arg): # transform single point or scalar
85+
if type(arg) == type(5):
86+
return arg * s.scale
87+
elif type(arg) == type((4,)) or type(arg)==type([4]):
88+
return posAdd(s.pos, posMult(arg, s.scale))
8589

86-
def sendOutput(self) :
90+
def sendOutput(self):
8791
self.output.sendOutput() # between connectors
8892

89-
def checkClicked(self, clicked) :
90-
if abs(clicked[0]-self.pos[0]) <= 15 :
91-
if abs(clicked[1]-self.pos[1]) <= 15 :
93+
def checkClicked(self, clicked):
94+
if abs(clicked[0] - self.pos[0]) <= 15:
95+
if abs(clicked[1] - self.pos[1]) <= 15:
9296
self.takeClick()
9397

94-
def takeClick(self) : # default action
95-
if set.Debug : print self.name, "was clicked"
98+
def takeClick(self): # default action
99+
if set.Debug:
100+
print("{0} was clicked".format(self.name))
96101

97-
def scootOver (self, xmov, ymov, nest="") :
102+
def scootOver(self, xmov, ymov, nest=""):
98103
# not only yourself but everything inside too
99-
self.pos = posAdd(self.pos, (xmov,ymov))
100-
for kid in self.kids :
104+
self.pos = posAdd(self.pos, (xmov, ymov))
105+
for kid in self.kids:
101106
# kid is either an inner component or a connector
102-
kid.scootOver (xmov, ymov, nest+" ")
107+
kid.scootOver(xmov, ymov, nest + " ")
103108

104-
def __repr__ (s) :
109+
def __repr__(s):
105110
par = s.parent
106-
if par : par = par.name
107-
return "<Comp: %s.%s %s %s>" % (par,s.name,s.pos,s.layer)
111+
if par:
112+
par = par.name
113+
return "<Comp: {0}.{1} {2} {3}>".format(par, s.name, s.pos, s.layer)
114+
108115

109-
def posMult(pos,factor) : # position math
110-
return (pos[0]*factor,pos[1]*factor)
116+
def posMult(pos, factor): # position math
117+
return (pos[0] * factor,
118+
pos[1] * factor)
111119

112-
def posAdd(pos1, pos2) :
113-
return (pos1[0]+pos2[0],pos1[1]+pos2[1])
114120

121+
def posAdd(pos1, pos2):
122+
return (pos1[0] + pos2[0],
123+
pos1[1] + pos2[1])

0 commit comments

Comments
 (0)