18
22
self.height = height
20
self.background = background
24
self.background = backend.color_convert(background)
22
26
# backend.check_screen_atts(self) # checks the variables
23
27
backend.create_screen(self)
26
return 'Screen: \n width = %d \n height = %d \n title = %s \n' % \
27
(self.width, self.height, self.title)
30
31
def begin_graphics(width=640, height=480, title="Gasp",
31
background=(255,255,255), back_end=real_backend):
32
background=(255,255,255), back_end=backend):
33
34
Initialize a graphics window in GASP
35
Screen(width, height, title, background, back_end)
37
screen = Screen(width, height, title, background, back_end)
38
39
def end_graphics():
87
86
return Point(point)
90
# =============================== Shapes ====================================
91
def __init__(self, center, filled=False, color=(0,0,0), thickness=1):
92
self.center = make_it_a_point(center)
94
self.color = backend.color_convert(color)
95
self.thickness = thickness
97
self.key = time.time()*random.random()
99
def move_by(self,x,y):
101
self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
102
self.center = Point(self.center.x+x,self.center.y+y)
103
else: move_by(self,x,y)
105
def move_to(self,pos):
107
x,y = pos[0]-self.center.x,pos[1]-self.center.y
108
self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
109
self.center = Point(self.center.x+x,self.center.y+y)
110
else: move_to(self,pos)
112
def draw(self,context):
114
x,y = backend.flip_coords(self.topleft)
115
context.translate(x,y-self.surface.get_height())
117
context.translate(self.surface.get_width()/2,self.surface.get_height()/2)
118
context.rotate(self.rot)
119
context.translate(-1*self.surface.get_width()/2,-1*self.surface.get_height()/2)
120
context.set_source_surface(self.surface)
94
126
Allows screen objects to be placed in the graphics window
96
128
def __init__(self, pos, color=(0,0,0), size=1):
97
self.pos = make_it_a_point(pos)
129
self.center = make_it_a_point(pos)
98
130
self.color = color
132
self.key = time.time()*random.random()
133
self.on_screen = False
134
screen.action_objects.put([["plot",self],"new object"])
105
139
Allows the creation of lines in the graphics window
107
def __init__(self, start, end, color=(0,0,0)):
141
def __init__(self, start, end, color=(0,0,0),thickness=1):
108
142
self.start = make_it_a_point(start)
109
143
self.end = make_it_a_point(end)
112
backend.create_line(self)
144
self.center = make_it_a_point(((start[0]+end[0])/2,(start[1]+end[1])/2))
145
self.color = backend.color_convert(color)
146
self.thickness=thickness
147
self.key = time.time()*random.random()
148
self.on_screen = False
149
screen.action_objects.put([["line",self],"new object"])
151
def move_by(self,x,y):
153
self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
154
self.center = Point(self.center.x+x,self.center.y+y)
155
else: move_by(self,x,y)
157
def move_to(self,pos):
159
x,y = pos[0]-self.center.x,pos[1]-self.center.y
160
self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
161
self.center = Point(self.center.x+x,self.center.y+y)
162
else: move_to(self,pos)
114
164
def __repr__(self):
115
165
return "A Line instance from (%d, %d) to (%d, %d)" % \
116
166
(self.start.x, self.start.y, self.end.x, self.end.y)
121
Allows the creation of shapes in the graphics window
123
def __init__(self, center, filled=False, color=(0,0,0), thickness=1):
124
self.center = make_it_a_point(center)
127
self.thickness = thickness
130
def move_to(self, center):
132
Moves a shape to a designated point
134
self.center = make_it_a_point(center)
135
backend.move_to(self, self.center)
137
def move_by(self, dx=0, dy=0):
139
Moves a shape by specified X and Y amounts
143
backend.move_to(self, self.center)
145
def rotate_to(self, angle):
147
Rotates a shape to an angle
149
change = angle - self.angle
151
backend.rotate_by(self, change)
153
def rotate_by(self, angle):
155
Rotates a shape by an angle
158
backend.rotate_by(self, angle)
161
168
class Box(Shape):
168
175
int(lower_left_corner[1] + height/2.0)))
169
176
self.height = height
170
177
self.width = width
178
self.on_screen = False
171
179
Shape.__init__(self, self.center, filled, color, thickness)
173
backend.create_box(self)
180
screen.action_objects.put([["box",self],"new object"])
175
def move_to(self, lower_left_corner):
176
new_center = Point((int(lower_left_corner[0] + self.width/2.0),
177
int(lower_left_corner[1] + self.height/2.0)))
178
Shape.move_to(self, new_center)
182
def move_to(self,pos):
184
x,y = pos[0]-self.topleft[0],pos[1]-self.topleft[1]
185
self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
186
self.center = Point(self.center.x+x,self.center.y+y)
180
190
def __repr__(self):
181
191
return "Box instance at (%d, %d) with width %d and height %d" % \
182
192
(self.center.x, self.center.y, self.width, self.height)
185
194
class Polygon(Shape):
187
196
Allows the creation of polygons in the graphics window
189
def __init__(self, points, filled=False, color=(0,0,0)):
198
def __init__(self, points, filled=False, color=(0,0,0),thickness=1):
199
self.thickness = thickness
190
200
self.points = points
193
203
for point in self.points:
194
204
x_values.append(point[0])
195
205
y_values.append(point[1])
196
self.height = (max(y_values) - min(y_values))
197
self.width = (max(x_values) - min(x_values))
206
self.height = (max(y_values) - min(y_values))+self.thickness*2
207
self.width = (max(x_values) - min(x_values))+self.thickness*2
208
self.topleft = (min(x_values)-self.thickness, min(y_values)-self.thickness)
198
209
self.center = Point(self.width/2 + min(x_values), self.height/2 +
211
self.on_screen = False
200
212
Shape.__init__(self, self.center, filled, color)
201
backend.create_polygon(self)
213
screen.action_objects.put([["polygon",self],"new object"])
203
215
def __repr__(self):
204
216
return "Polygon instance at (%i, %i) with the points:\n %t" % \
205
217
(self.center.x , self.center.y, self.points)
208
219
class Circle(Shape):
213
224
filled=False, color=(0,0,0), thickness=1):
214
225
Shape.__init__(self, center, filled, color, thickness)
215
226
self.radius = radius
217
backend.create_circle(self)
227
self.on_screen = False
228
screen.action_objects.put([["circle",self],"new object"])
229
#backend.create_circle(self)
219
231
def __repr__(self):
220
232
return "Circle instance at (%d, %d) with radius %d" % \
221
233
(self.center.x, self.center.y, self.radius)
224
235
class Arc(Shape):
226
237
Allows the creation of arcs in the graphics window
229
240
filled=False, color=(0,0,0), thickness=1):
230
241
Shape.__init__(self, center, filled, color, thickness)
231
242
self.radius = radius
232
self.start_angle = start_angle
233
self.end_angle = end_angle
235
backend.create_arc(self)
243
self.start_angle = 360-start_angle
244
self.end_angle = 360-end_angle
245
self.on_screen = False
246
screen.action_objects.put([["arc",self],"new object"])
237
248
def __repr__(self):
238
249
return "Arc instance at (%d, %d) with start angle %d and end angle %d" \
239
250
% (self.center.x, self.center.y, self.start_angle, self.end_angle)
242
254
class Oval(Shape):
244
256
Allows the creation of circles in the graphics window
248
260
Shape.__init__(self, center, filled, color, thickness)
249
261
self.width = width
250
262
self.height = height
252
backend.create_oval(self)
263
self.on_screen = False
264
screen.action_objects.put([["oval",self],"new object"])
254
266
def __repr__(self):
255
267
return "Oval instance at (%d, %d) with width %d and height %d" % \
256
268
(self.center.x, self.center.y, self.width, self.height)
259
270
class Image(Shape):
261
272
Allows the placing of images in the graphics window
263
274
def __init__(self, file_path, center, width=0, height=0):
264
self.name = ''.join(file_path.split('.')[:-1]).split('/')[-1]
265
275
self.path_name = file_path
268
self.center = Point(center)
276
self.center = Point(backend.flip_coords(center))
269
277
self.width = width
270
278
self.height = height
271
backend.create_image(self)
279
self.key = time.time()*random.random()
280
self.on_screen = False
281
screen.action_objects.put([["image",self],"new object"])
283
def draw(self,context):
285
context.translate(*self.topleft)
286
context.translate(self.width/2,self.height/2)
287
context.rotate(self.rot)
288
context.translate(-1*self.width/2,-1*self.height/2)
290
context.scale(self.scale[0],self.scale[1])
291
context.set_source_pixbuf(self.pixbuf,0,0)
296
def move_by(self,x,y):
298
self.topleft = (self.topleft[0]+x,self.topleft[1]-y)
299
self.center = Point(self.center.x+x,self.center.y-y)
303
def move_to(self,pos):
305
x,y = pos[0]-self.center.x,pos[1]-self.center.y
306
self.topleft = (self.topleft[0]+x,self.topleft[1]-y)
307
self.center = Point(self.center.x+x,self.center.y-y)
273
311
def __repr__(self):
274
312
return "Image instance at (%i, %i) from the file %s" % \
280
318
Moves a screen object to a specified location
320
screen.action_objects.put([obj,"move_to",pos])
285
323
def move_by(obj, dx, dy):
287
325
Moves a screen object by specified X and Y amounts
327
screen.action_objects.put([obj,"move_by",(dx,dy)])
329
def get_text_length(text):
330
return backend.get_text_length(text)
292
333
def rotate_to(obj, angle):
294
335
Rotates a screen object to a specified angle
337
screen.action_objects.put([obj,"rotate_to",math.radians(angle)])
299
340
def rotate_by(obj, angle):
301
342
Rotates a screen object by a specified angle
344
screen.action_objects.put([obj,"rotate_by",math.radians(angle)])
306
347
# =============================== Text ======================================
310
351
Allows the creation of text in the graphics window
312
353
def __init__(self, text, pos, color=(0,0,0), size=12):
314
self.pos = make_it_a_point(pos)
355
self.color = backend.color_convert(color)
318
backend.create_text(self)
321
# =============================== Sound =====================================
357
self.pos = backend.flip_coords(pos)
358
self.key = time.time()*random.random()
359
self.on_screen = False
360
screen.action_objects.put([["text",self],"new object"])
361
#screen.action_objects.put(obj)
362
#screen.objects.append(obj)
364
def draw(self, context):
366
context.move_to(*self.pos)
368
context.rotate(self.rot)
369
context.set_source_rgb(*self.color)
370
context.set_font_size(self.size)
371
context.show_text(self.text)
375
def move_to(self,pos):
377
self.pos = backend.flip_coords(pos)
381
def move_by(self,dx,dy):
383
self.pos = (self.pos[0]+dx,self.pos[1]-dy)
387
# ============================ Text Entry ==================================
389
def Text_Entry(message="Enter Text:", pos=(0,0), color=(0,0,0), size=12):
390
entry = backend.Entry(message,pos,color,size)
394
"""# =============================== Sound =====================================
325
398
Allows the creation and playback of sounds
327
400
def __init__(self, file_path):
328
401
self.path = file_path
329
self.name = ''.join(file_path.split('.')[:-1]).split('/')[-1]
330
backend.create_sound(self)
402
self.filename = ''.join(file_path.split('.')[:-1]).split('/')[-1]
333
405
backend.play_sound(self)
380
452
return backend.keys()
383
def key_pressed(key, wait_for_upstroke=False):
385
Returns a boolean of whether a key has been pressed
387
if wait_for_upstroke:
388
return key in backend.waiting_keys()
390
return key in keys_pressed()
454
def key_pressed(key):
455
return key in backend.keys()
458
return backend.get_screen_size()
394
460
# ============================= GASP TOOLS ==================================
396
def screen_shot(filename):
462
def screen_shot(filename="screen_shot"):
398
464
Saves a screenshot to a specified file name
400
466
backend.screen_picture(filename)
402
random_choice = random.choice
405
468
def random_between(num1, num2):
406
469
if (num1 == num2):