# BallGTK.py Copyright (c) 2006 Kari Laitinen # http://www.naturalprogramming.com # 2006-09-25 File created. # 2006-09-25 Last modification. # The Ball class represents a graphical ball that can be drawn # onto the screen. # The Ball class is used in programs MovingBallOOGTK.py and # MovingBallWithMouseGTK.py import gtk import math class Ball : def __init__( self, given_position_x, given_position_y, given_color ) : self.ball_position_x = given_position_x self.ball_position_y = given_position_y self.ball_color = given_color self.ball_diameter = 80 self.this_ball_is_activated = False def activate_ball( self ) : self.this_ball_is_activated = True def deactivate_ball( self ) : self.this_ball_is_activated = False def get_ball_position_x( self ) : return self.ball_position_x def get_ball_position_y( self ) : return self.ball_position_y def get_ball_diameter( self ) : return self.ball_diameter def move_right( self ) : self.ball_position_x += 3 def move_left( self ) : self.ball_position_x -= 3 def move_up( self ) : self.ball_position_y -= 3 def move_down( self ) : self.ball_position_y += 3 def move_this_ball( self, movement_in_direction_x, movement_in_direction_y ) : self.ball_position_x += movement_in_direction_x self.ball_position_y += movement_in_direction_y def move_to_position( self, new_position_x, new_position_y ) : self.ball_position_x = new_position_x self.ball_position_y = new_position_y def shrink( self ) : # The if-construct ensures that the ball does not become # too small. if self.ball_diameter > 10 : self.ball_diameter -= 6 # When the ball position is adjusted with suitable # values it seems to stay in its position when it is # made smaller. self.ball_position_x += 3 self.ball_position_y += 3 def enlarge( self ) : self.ball_diameter += 6 # Coordinates are adjusted so that the ball appears # to stay in its current position while its size is being # changed. self.ball_position_x -= 3 self.ball_position_y -= 3 def set_diameter( self, new_diameter ) : if new_diameter > 5 : diameter_change = new_diameter - self.ball_diameter self.ball_diameter = new_diameter # Coordinates are adjusted so that the ball appears # to stay in its current position while its size is being # changed. self.ball_position_x -= diameter_change / 2 self.ball_position_y -= diameter_change / 2 def set_color( self, new_color ) : self.ball_color = new_color def contains_point( self, given_point_x, given_point_y ) : ball_radius = self.ball_diameter / 2 ball_center_point_x = self.ball_position_x + ball_radius ball_center_point_y = self.ball_position_y + ball_radius # Here we use the Pythagorean theorem to calculate the distance # from the given point to the center point of the ball. # See the note at the end of this file. distance_from_given_point_to_ball_center = \ int( math.sqrt( math.pow( ball_center_point_x - given_point_x, 2 ) + math.pow( ball_center_point_y - given_point_y, 2 ) ) ) return distance_from_given_point_to_ball_center <= ball_radius # def intersects_rectangle( self, given_rectangle ) : # Area this_ball_area = new Area( # new Ellipse2D.Float( self.ball_position_x, # self.ball_position_y, # self.ball_diameter, # self.ball_diameter ) ) # return this_ball_area.intersects( given_rectangle ) def draw( self, given_drawing_area ) : graphics_context = given_drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] given_drawable = given_drawing_area.window graphics_context.foreground = \ given_drawable.get_colormap().alloc_color( self.ball_color ) # Note that angles are expressed in 1/64ths of a degree. # 360 degrees is thus represented by value 360*64 given_drawable.draw_arc( graphics_context, True, self.ball_position_x, self.ball_position_y, self.ball_diameter, self.ball_diameter, 0, 360*64 ) graphics_context.foreground = \ given_drawable.get_colormap().alloc_color( "black" ) given_drawable.draw_arc( graphics_context, False, self.ball_position_x, self.ball_position_y, self.ball_diameter, self.ball_diameter, 0, 360*64 ) # If this ball is activated, it will have a thick black edge if self.this_ball_is_activated == True : given_drawable.draw_arc( graphics_context, False, self.ball_position_x + 1, self.ball_position_y + 1, self.ball_diameter - 2, self.ball_diameter - 2, 0, 360*64 ) given_drawable.draw_arc( graphics_context, False, self.ball_position_x + 2, self.ball_position_y + 2, self.ball_diameter - 4, self.ball_diameter - 4, 0, 360*64 )