# BallQt.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2010-09-27 File created. # 2010-09-27 Last modification. # The Ball class represents a graphical ball that can be drawn # onto the screen. # The Ball class is used in programs MovingBallOOQt.py and # MovingBallWithMouseQt.py from PyQt4.QtCore import * from PyQt4.QtGui import * 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 = 100 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 get_ball_color( self ) : return self.ball_color 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, painter ) : painter.setBrush( self.ball_color ) ball_radius = self.ball_diameter / 2 painter.drawEllipse( self.ball_center_point_x - ball_radius, self.ball_center_point_y - ball_radius, self.ball_diameter, self.ball_diameter ) # If this ball is activated, it will have a thick black edge if self.this_ball_is_activated == True : painter.drawEllipse( self.ball_center_point_x - ball_radius + 1, self.ball_center_point_y - ball_radius + 1, self.ball_diameter - 2, self.ball_diameter - 2 ) painter.drawEllipse( self.ball_center_point_x - ball_radius + 2, self.ball_center_point_y - ball_radius + 2, self.ball_diameter - 4, self.ball_diameter - 4 )