# BallGTK.py Copyright (c) 2006 Kari Laitinen # http://www.naturalprogramming.com # 2006-09-25 File created. # 2009-02-14 The ball coordinates refer now to the center point of the ball. # 2009-02-14 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_center_point_x, given_center_point_y, given_color ) : self.ball_center_point_x = given_center_point_x self.ball_center_point_y = given_center_point_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_center_point_x( self ) : return self.ball_center_point_x def get_ball_center_point_y( self ) : return self.ball_center_point_y def get_ball_diameter( self ) : return self.ball_diameter def move_right( self ) : self.ball_center_point_x += 3 def move_left( self ) : self.ball_center_point_x -= 3 def move_up( self ) : self.ball_center_point_y -= 3 def move_down( self ) : self.ball_center_point_y += 3 def move_this_ball( self, movement_in_direction_x, movement_in_direction_y ) : self.ball_center_point_x += movement_in_direction_x self.ball_center_point_y += movement_in_direction_y def move_to_position( self, new_center_point_x, new_center_point_y ) : self.ball_center_point_x = new_center_point_x self.ball_center_point_y = new_center_point_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 def enlarge( self ) : self.ball_diameter += 6 def set_diameter( self, new_diameter ) : if new_diameter > 5 : self.ball_diameter = new_diameter 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 # 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( self.ball_center_point_x - given_point_x, 2 ) + math.pow( self.ball_center_point_y - given_point_y, 2 ) ) ) return distance_from_given_point_to_ball_center <= ball_radius # def intersects_rectangle( self, given_rectangle ) : # This method to be written later. # Area this_ball_area = new Area( # new Ellipse2D.Float( self.ball_center_point_x, # self.ball_center_point_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 ball_radius = self.ball_diameter / 2 given_drawable.draw_arc( graphics_context, True, self.ball_center_point_x - ball_radius, self.ball_center_point_y - ball_radius, 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_center_point_x - ball_radius, self.ball_center_point_y - ball_radius, 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_center_point_x - ball_radius + 1, self.ball_center_point_y - ball_radius + 1, self.ball_diameter - 2, self.ball_diameter - 2, 0, 360*64 ) given_drawable.draw_arc( graphics_context, False, self.ball_center_point_x - ball_radius + 2, self.ball_center_point_y - ball_radius + 2, self.ball_diameter - 4, self.ball_diameter - 4, 0, 360*64 )