Class GObject

This class provides basic functionality for drawing to a GView. You should never make a GObject directly. Instead, you should use one of its many subclasses: GRectangle, GEllipse, GImage, GLabel

Constructor

GObject(**keywords)

Constructor: Creates a new GObject to be drawn.

To use the constructor for this class, you should provide it with a list of keyword arguments that initialize various attributes. The keywords are the same as the attribute names. For example, to initialize the x position and the fill color, use the constructor call

GObject(x=2,fillcolor=colormodel.RED)

You do not need to provide the keywords as a dictionary. The ** in the parameter keywords does that automatically.

Any attribute of this class may be used as a keyword. The argument must satisfy the invariants of that attribute. See the list of attributes of this class for more information.

Parameter:keywords – dictionary of keyword arguments

Precondition: See above.

Attributes

x

The horizontal coordinate of the object center.

Invariant: Must be an int or float.

y

The vertical coordinate of the object center..

Invariant: Must be an int or float.

left

The left edge of this shape.

The value depends on the current angle of rotation. If rotation is 0, it is x-width/2. Otherwise, it is the left-most value of the bounding box.

Changing this value will shift the center of the object so that the left edge matches the new value.

Warning: Accessing this value on a rotated object will slow down your framerate significantly.

Invariant: Must be an int or float.

right

The right edge of this shape.

The value depends on the current angle of rotation. If rotation is 0, it is x+width/2. Otherwise, it is the right-most value of the bounding box.

Changing this value will shift the center of the object so that the right edge matches the new value.

Warning: Accessing this value on a rotated object will slow down your framerate significantly.

Invariant: Must be an int or float.

bottom

The vertical coordinate of the bottom edge.

The value depends on the current angle of rotation. If rotation is 0, it is y-height/2. Otherwise, it is the bottom-most value of the bounding box.

Changing this value will shift the center of the object so that the bottom edge matches the new value.

Warning: Accessing this value on a rotated object will slow down your framerate significantly.

Invariant: Must be an int or float.

top

The vertical coordinate of the top edge.

The value depends on the current angle of rotation. If rotation is 0, it is y+height/2. Otherwise, it is the top-most value of the bounding box.

Changing this value will shift the center of the object so that the top edge matches the new value.

Warning: Accessing this value on a rotated object will slow down your framerate significantly.

Invariant: Must be an int or float.

linecolor

The object line color.

The default representation of color in GObject is a 4-element list of floats between 0 and 1 (representing r, g, b, and a). As with the Turtle, you may also assign color an RGB or HSV object from colormodel, or a string with a valid color name. If you chose either of these alternate representations (a string or an object from colormodel), Python will automatically convert the result into a 4-element list.

Invariant: Must be a 4-element list of floats between 0 and 1.

fillcolor

The object fill color.

This value is used to color the backgrounds or, in the case of solid shapes, the shape interior.

The default representation of color in GObject is a 4-element list of floats between 0 and 1 (representing r, g, b, and a). As with the Turtle, you may also assign color an RGB or HSV object from colormodel, or a string with a valid color name. If you chose either of these alternate representations (a string or an object from colormodel), Python will automatically convert the result into a 4-element list.

Invariant: Must be a 4-element list of floats between 0 and 1.

scale

The scaling factor of this shape.

The scale is a fast way to cause a shape to grow or shrink in size. Essentially, the object will multiple the width and height by the scale. So a scale less than 1 will shrink the object, while a scale greater than 1 will enlarge the object.

The scale may either be a single number, or a pair of two numbers. If it is a single number, it will scale the width and height by the same amount. If it is a pair, it will scale the width by the first value, and the height by the second.

Invariant: Must be either a number (int or float) or a pair of numbers.

angle

The angle of rotation about the center.

The angle is measured in degrees (not radians) counter-clockwise.

Invariant: Must be an int or float.

name

The name of this object.

This value is for debugging purposes only. If you name an object, the name will appear when you convert the object to a string. This will allow you to tell which object is which in your watches.

Invariant: Must be a string or None.

Methods

contains(x, y)

Returns: True if this shape contains the point (x,y), False otherwise.

By default, this method just checks the bounding box of the shape.

Warning: Accessing this value on a rotated object may slow down your framerate significantly.

Parameter:x – x coordinate of point to check

Precondition: an int or float

Parameter:y – y coordinate of point to check

Precondition: an int or float

transform(point)

Returns: The given point transformed to local coordinate system

This method is important for mouse selection. It helps you understand where in the shape the selection takes place. In the case of objects with children, like GScene, this method is necessary to properly use the contains method on the children.

The value returned is a GPoint.

Parameter:point – the point to transform

Precondition: a GPoint or a pair of numbers (int or float)

draw(view)

Draws this shape in the provide view.

Ideally, the view should be the one provided by GameApp.

Parameter:view – the view to draw to

Precondition: an instance of GView