def __init__(self, max_x, max_y, initial_distance=1000): ''' Initializes the CanvasedFallingObject instance. @precondition: none. @postcondition: The object is ready to use. @param max_x: The number of pixels along the x axis. Used to calculate positions when moving. @param max_y: The number of pixels along the y axis. Used to calculate positions when moving. @param initial_distance: The initial distance of the ball with respect to the planet. Defaults to 1,000m. ''' # initialize the super class. FallingObject.__init__(self, initial_distance=initial_distance) self.max_x = max_x # We want to keep the image within the bounds of the allocated # max_y, so our max is max - image height. self.max_y = max_y - CanvasedFallingObject.HEIGHT self._image = FreeFallCanvasImage('falling_object', 'asteroid.gif')
class CanvasedFallingObject(FallingObject): ''' A representation of the falling object on the canvas. Represents the canvas object that is the free falling object during a free fall calculation. You can perform the normal free fall calculations, but you can also store and change the state of the object on the canvas. ''' ''' The size of the object.''' WIDTH = 25 HEIGHT = 54 def __init__(self, max_x, max_y, initial_distance=1000): ''' Initializes the CanvasedFallingObject instance. @precondition: none. @postcondition: The object is ready to use. @param max_x: The number of pixels along the x axis. Used to calculate positions when moving. @param max_y: The number of pixels along the y axis. Used to calculate positions when moving. @param initial_distance: The initial distance of the ball with respect to the planet. Defaults to 1,000m. ''' # initialize the super class. FallingObject.__init__(self, initial_distance=initial_distance) self.max_x = max_x # We want to keep the image within the bounds of the allocated # max_y, so our max is max - image height. self.max_y = max_y - CanvasedFallingObject.HEIGHT self._image = FreeFallCanvasImage('falling_object', 'asteroid.gif') def create_object(self): ''' Adds the falling object to the top of the canvas. @precondition: self._image and self._canvas are initialized. @postcondition: The self._image is added to the self._canvas. ''' if not self._image.on_canvas(): start_x = (self.max_x / 2) - (CanvasedFallingObject.WIDTH / 2) self._image.add_to_canvas(self._canvas, start_x, 0, anchor=tkinter.NW, tags="falling_object") def remove_object(self): ''' Removes the object from the canvas. @precondition: none. @postcondition: The self._image is removed from the self._canvas. ''' if self._image.on_canvas(): self._image.remove_from_canvas(self._canvas) def add_to_canvas(self, canvas): ''' Adds the object to the given canvas. @precondition: The given canvas is setup. @postcondition: This object is added to the given canvas. @param canvas: The canvas on which you want the object added. ''' # This is the canvas we'll work with when moving, etc. self._canvas = canvas # Create the canvas object. self.create_object() def move_object(self, gravity=9.81, time=0): ''' Moves the object based on the given gravity and time. @precondition: none. @postcondition: The objects is moved down the canvas to the point it would be at the given time. @param gravity: The amount of gravitational force being applied to the object in m/s^2. @param time: The point in time at which the object should be moved to in seconds. ''' # Set my current distance. self.move_to_time(gravity=gravity, time=time) # Determine the percentage moved against the height of the canvas. percent_fallen = (self.initial_distance - self._cur_distance) / self.initial_distance # Determine the new x and y coordinate for the object on the canvas. new_x = (self.max_x / 2) - (CanvasedFallingObject.WIDTH / 2) new_y = (self.max_y * percent_fallen) # Move the canvas object. self._canvas.coords(self._image.get_id(), new_x, new_y) self._canvas.tag_raise("falling_object")