-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGUI pygame.py
185 lines (166 loc) · 6.35 KB
/
GUI pygame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import pygame , Buttons
import copy
import enum
from pygame.locals import *
eventType = enum.Enum("eventType", "Quit Mouse_Motion Key_Down Mouse_Up")
def snapToGrid(x, y, gridspace):
if (x % gridspace < gridspace / 2):
x = x - (x % gridspace)
else:
x = (x + gridspace) - (x % gridspace)
if (y % gridspace < gridspace / 2):
y = y - (y % gridspace)
else:
y = (y + gridspace) - (y % gridspace)
return x,y
def flatten(listOfLists):
"Flatten one level of nesting"
z = [x for sublist in listOfLists for x in sublist]
return z
def initalize():
global gameDisplay
global clock
global gridspace
global linethickness
global lineColor
global backgroundColor
global compdict
# , draw coordinates,width and length,start and end point in grid coordinates, color , type
compdict = {"R":[0,-25,-5,50,10,1,0,-1,0,(255,150,60),"R"],"C":[0,-25,-8,50,16,1,0,-1,0,(200,150,200),"C"]
,"V":[0,-25,-10,50,20,1,0,-1,0,(255,0,0),"V"],"G":[0,-25,-10,50,20,1,0,1,0,(0,0,0),"G"]}
pygame.init()
clock = pygame.time.Clock()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption("nema-zag circuit simulator 2000 😎")
gridspace = 25
linethickness = 3
lineColor = (0,255,255)
backgroundColor = (150,150,150)
#res =pygame.image.load(fileobj, namehint="")
def render():
global componentOrientation
gameDisplay.fill(backgroundColor)
#Render components/wires currently being edited
if drawingLine:
if abs(initialCoordinates[0] - gridCoordinates[0]) >= abs(initialCoordinates[1] - gridCoordinates[1]):
componentOrientation = 0
pygame.draw.line(gameDisplay, lineColor, initialCoordinates, [gridCoordinates[0], initialCoordinates[1]],linethickness)
else:
componentOrientation = 1
pygame.draw.line(gameDisplay, lineColor, initialCoordinates, [initialCoordinates[0], gridCoordinates[1]],linethickness)
if drawingComponenet:
if componentOrientation == 0:
pygame.draw.rect(gameDisplay, currentComponent[9], [gridCoordinates[0] + currentComponent[1], gridCoordinates[1]+ currentComponent[2], currentComponent[3], currentComponent[4]])
if componentOrientation == 1:
pygame.draw.rect(gameDisplay, currentComponent[9], [gridCoordinates[0] + currentComponent[2], gridCoordinates[1] + currentComponent[1], currentComponent[4], currentComponent[3]])
#Render current components/wires
if len(lines) > 0:
for i in lines :
pygame.draw.line(gameDisplay, lineColor, i[0], i[1],linethickness)
if len(components) > 0:
for i in components:
pygame.draw.rect(gameDisplay, i[4], flatten(i)[0:4])
pygame.display.update()
def checkEvents():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return eventType.Quit, None
elif event.type == pygame.MOUSEMOTION:
return eventType.Mouse_Motion, event.pos
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
return eventType.Key_Down, "s"
elif event.key == pygame.K_r:
return eventType.Key_Down, "r"
elif event.key == pygame.K_l:
return eventType.Key_Down, "l"
elif event.key == pygame.K_o:
return eventType.Key_Down, "o"
elif event.key == pygame.K_v:
return eventType.Key_Down, "v"
elif event.key == pygame.K_g:
return eventType.Key_Down, "g"
elif event.key == pygame.K_c:
return eventType.Key_Down, "c"
else:
return eventType.Key_Down, 0
elif event.type == pygame.MOUSEBUTTONUP:
return eventType.Mouse_Up, None
return 0, 0
def generateNetlist():
nodes = []
netlist = []
def kill():
pygame.quit()
#variables
# clock = 0
# gameDisplay = None
killApp = False
drawingLine = False
drawingComponenet = False
currentComponent = None
initialCoordinates = [0, 0]
componentOrientation = 0 #0->H 1->v
gridCoordinates = [0, 0]
lines = []
components = []
verticalWire = False
initalize()
#main loop
while not killApp:
returnedEvent, eventParameter = checkEvents()
if (returnedEvent == eventType.Quit):
killApp = True
#update grid coordinates every time the mouse moves
elif (returnedEvent == eventType.Mouse_Motion):
gridCoordinates = [snapToGrid(eventParameter[0], eventParameter[1],25)[0],snapToGrid(eventParameter[0], eventParameter[1],25)[1]]
#print(gridCoordinates)
#if key pressed start drawing a component
elif (returnedEvent == eventType.Key_Down):
if (eventParameter == "s"):
drawingLine = False
currentComponent = compdict["R"]
componentOrientation = 0
drawingComponenet = not drawingComponenet
if (eventParameter == "c"):
drawingLine = False
currentComponent = compdict["C"]
componentOrientation = 0
drawingComponenet = not drawingComponenet
if (eventParameter == "g"):
drawingLine = False
currentComponent = compdict["G"]
componentOrientation = 0
drawingComponenet = not drawingComponenet
if (eventParameter == "v"):
drawingLine = False
currentComponent = compdict["V"]
componentOrientation = 0
drawingComponenet = not drawingComponenet
elif (eventParameter == "r"):
componentOrientation = not componentOrientation
#when mouse up
elif (returnedEvent == eventType.Mouse_Up):
#start drawing lines
if ((drawingLine == False) & (drawingComponenet == False)):
initialCoordinates = copy.copy(gridCoordinates)
drawingLine = True
#save drawn line
elif drawingLine == True:
if (componentOrientation):
lines.append([initialCoordinates, [initialCoordinates[0], gridCoordinates[1]]])
print(lines)
else:
lines.append([initialCoordinates, [gridCoordinates[0], initialCoordinates[1]]])
print(lines)
drawingLine = False
#or save drawn component
elif drawingComponenet == True:
drawingComponenet = False
if componentOrientation == 0:
components.append([[gridCoordinates[0] + currentComponent[1], gridCoordinates[1] + currentComponent[2]], [currentComponent[3], currentComponent[4]],[gridCoordinates[0] +currentComponent[5]*gridspace,gridCoordinates[1] +currentComponent[6]*gridspace],[gridCoordinates[0] +currentComponent[7]*gridspace,gridCoordinates[1] +currentComponent[8]*gridspace],currentComponent[9],currentComponent[10]])
if componentOrientation == 1:
components.append([[gridCoordinates[0] + currentComponent[2], gridCoordinates[1] + currentComponent[1]], [currentComponent[4], currentComponent[3]],[gridCoordinates[0] +currentComponent[6]*gridspace,gridCoordinates[1] +currentComponent[5]*gridspace],[gridCoordinates[0] +currentComponent[8]*gridspace,gridCoordinates[1] +currentComponent[7]*gridspace],currentComponent[9],currentComponent[10]])
render()
clock.tick(60)
kill()