def __init__(self, width=640, height=480, title="Gasp", background=(255, 255, 255), back_end=None): global backend backend = back_end if isinstance(width, (int, float)) and width > 0: self.width = int(width) else: raise backend.GaspException( "invalid screen width, expected int got " + str(type(width))) if isinstance(height, (int, float)) and height > 0: self.height = int(height) else: raise backend.GaspException( "invalid screen height, expected int got " + str(type(height))) if isinstance(title, str): self.title = title else: raise backend.GaspException("Title must be a string") try: self.background = backend.color_convert(background) except: raise backend.GaspException("background argument is not a color") # backend.check_screen_atts(self) # checks the variables backend.create_screen(self)
def __init__(self, center, radius, start_angle, end_angle, filled=False, color=(0, 0, 0), thickness=1): Shape.__init__(self, center, filled, color, thickness) if isinstance(radius, (int, float)): self.radius = radius else: raise backend.GaspException("radius must be an int or float") if isinstance(start_angle, (int, float)): self.start_angle = 360 - start_angle else: raise backend.GaspException( "start angle argument must be an int of float") if isinstance(end_angle, (int, float)): self.end_angle = 360 - end_angle else: raise backend.GaspException( "end angle argument must be an int of float") self.on_screen = False screen.action_objects.put([["arc", self], "new object"]) screen.objects.append(self)
def rotate_by(obj, angle): """ Rotates a screen object by a specified angle """ if obj in screen.objects: if isinstance(angle, (int, float)): screen.action_objects.put([obj, "rotate_by", math.radians(angle)]) else: raise backend.GaspException("angle must fe an int or a float") else: raise backend.GaspException("this object is not on screen")
def move_to(obj, pos): """ Moves a screen object to a specified location """ try: if isinstance(pos, tuple): screen.action_objects.put([obj, "move_to", pos]) else: raise backend.GaspException("position must be a tuple") except: raise backend.GaspException("this object is not on screen")
def move_by(obj, dx, dy): """ Moves a screen object by specified X and Y amounts """ try: if isinstance(dx, (int, float)) and isinstance(dy, (int, float)): screen.action_objects.put([obj, "move_by", (dx, dy)]) else: raise backend.GaspException( "improper arguments, must move by int or float") except: raise backend.GaspException("this object is not on screen")
def __init__(self, pos, color=(0, 0, 0), size=1): try: self.center = make_it_a_point(center) except: raise backend.GaspException("center is not in (x,y) format") try: self.color = backend.color_convert(color) except: raise backend.GaspException("color argument is not a color") if isinstance(size, (int, float)): self.size = size else: raise backend.GaspException("size argument is not a number") self.key = time.time() * random.random() self.on_screen = False screen.action_objects.put([["plot", self], "new object"]) screen.objects.append(self)
def set_speed(speed): """ Sets program frame rate in frames per second """ if isinstance(speed, (int, float)): backend.set_frame_rate(int(speed)) else: raise backend.GaspException("set_speed must take an int or float")
def sleep(duration): """ Sleeps for a length of time """ if isinstance(duration, (int, float)): backend.sleep(duration) else: raise backend.GaspException("sleep takes either int or float")
def remove_from_screen(obj): """ Removes a specified object from the graphics window """ try: backend.remove(obj) except: raise backend.GaspException("This object is not on screen")
def __init__(self, center, width, height, filled=False, color=(0, 0, 0), thickness=1): Shape.__init__(self, center, filled, color, thickness) if isinstance(width, (int, float)): self.width = width else: raise backend.GaspException("width must be an int or a float") if isinstance(height, (int, float)): self.height = height else: raise backend.GaspException("height must be an int or a float") self.on_screen = False screen.action_objects.put([["oval", self], "new object"]) screen.objects.append(self)
def __init__(self, file_path, center, width=0, height=0): self.path_name = file_path try: self.center = Point(backend.flip_coords(center)) except: raise backend.GaspException( "center argument is not in (x,y) format") if isinstance(width, (int, float)): self.width = width else: raise backend.GaspException("width must be an int or a float") if isinstance(height, (int, float)): self.height = height else: raise backend.GaspException("height must be an int or a float") self.key = time.time() * random.random() self.on_screen = False screen.action_objects.put([["image", self], "new object"]) screen.objects.append(self)
def __init__(self, center, filled=False, color=(0, 0, 0), thickness=1): try: self.center = make_it_a_point(center) except: raise backend.GaspException("center is not in (x,y) format") if isinstance(filled, bool): self.filled = filled else: raise backend.GaspException("filled argument must be a boolean") try: self.color = backend.color_convert(color) except: raise backend.GaspException("color argument is not a color") if isinstance(thickness, (int, float)): self.thickness = thickness else: raise backend.GaspException("thickness must be an int") self.rot = 0 self.key = time.time() * random.random()
def __init__(self, start, end, color=(0, 0, 0), thickness=1): try: self.start = make_it_a_point(start) except: raise backend.GaspException("start is not is (x,y) format") try: self.end = make_it_a_point(end) except: raise backend.GaspException("end is not is (x,y) format") self.center = make_it_a_point( ((start[0] + end[0]) / 2, (start[1] + end[1]) / 2)) try: self.color = backend.color_convert(color) except: raise backend.GaspException("color argument is not a color") self.thickness = thickness self.key = time.time() * random.random() self.on_screen = False screen.action_objects.put([["line", self], "new object"]) screen.objects.append(self)
def __init__(self, text, pos, color=(0, 0, 0), size=12): if isinstance(text, str): self.text = text else: raise backend.GaspException("Text must take a string argument") try: self.color = backend.color_convert(color) except: raise backend.GaspException("color argument is not a color") if isinstance(size, (int, float)): self.size = size else: raise backend.GaspException( "size argument must be an int or float") try: self.pos = backend.flip_coords(pos) except: raise backend.GaspException( "pos argument is not in the (x,y) format") self.key = time.time() * random.random() self.on_screen = False screen.action_objects.put([["text", self], "new object"]) screen.objects.append(self)
def __init__(self, center, radius, filled=False, color=(0, 0, 0), thickness=1): Shape.__init__(self, center, filled, color, thickness) if isinstance(radius, (int, float)): self.radius = radius else: raise backend.GaspException("radius must be a int or float") self.on_screen = False screen.action_objects.put([["circle", self], "new object"]) screen.objects.append(self)
def __init__(self, points, filled=False, color=(0, 0, 0), thickness=1): self.thickness = thickness if isinstance(points, list): self.points = points else: raise backend.GaspException("points must be a list") x_values = [] y_values = [] for point in self.points: if isinstance(point, tuple): x_values.append(point[0]) y_values.append(point[1]) else: raise backend.GaspException("points for be tuples") self.height = (max(y_values) - min(y_values)) + self.thickness * 2 self.width = (max(x_values) - min(x_values)) + self.thickness * 2 self.topleft = (min(x_values) - self.thickness, min(y_values) - self.thickness) self.center = Point(self.width / 2 + min(x_values), self.height / 2 + min(y_values)) self.on_screen = False Shape.__init__(self, self.center, filled, color) screen.action_objects.put([["polygon", self], "new object"]) screen.objects.append(self)
def update_when(event_type): """ Event Types include 'key_pressed' 'mouse_clicked' 'next_tick' """ if event_type == 'key_pressed' or event_type == 'mouse_clicked' or event_type == "next_tick": if event_type == "key_pressed": return backend.update_when(event_type) else: backend.update_when(event_type) else: raise backend.GaspException( "update_when must take of 'key_pressed', 'mouse_clicked', or 'next_tick'" )
def __init__(self, lower_left_corner, width, height, filled=False, color=(0, 0, 0), thickness=1): if isinstance(lower_left_corner, (tuple, list)) and isinstance( width, (int, float)) and isinstance(height, (int, float)): center = Point((int(lower_left_corner[0] + width / 2.0), int(lower_left_corner[1] + height / 2.0))) self.height = height self.width = width else: raise backend.GaspException("improper arguments") self.on_screen = False Shape.__init__(self, center, filled, color, thickness) screen.action_objects.put([["box", self], "new object"]) screen.objects.append(self)
def get_text_length(text): if isinstance(text, Text): return backend.get_text_length(text) else: raise backend.GaspException("argument must be instance of Text")