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

While unsupported in the base class, most subclasses of GObject provide primitive collision detection via the contains method. This method takes a Point2 object from the introcs module.

Constructor

class game2d.GObject(**keywords)

An class representing a basic graphics object.

A graphics object is an object to draw on the screen. To draw it, you will need an instance of GView, which is passed to the draw() method.

You should never make a GObject directly. Instead, you should use one of the subclasses: GRectangle, GEllipse, GImage, GLabel, GTriangle, GPolygon, or GPath.

Attributes

GObject.x

The horizontal coordinate of the object center.

invariant: Value must be an int or float

GObject.y

The vertical coordinate of the object center.

invariant: Value must be an int or float

GObject.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 may slow down your framerate.

invariant: Value must be an int or float.

GObject.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 may slow down your framerate.

invariant: Value must be an int or float.

GObject.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 may slow down your framerate.

invariant: Value must be an int or float.

GObject.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 may slow down your framerate.

invariant: Value must be an int or float.

GObject.linecolor

The object line color

This is the border color of the shape. If there no value (e.g. the linecolor is None), this shape will have no border.

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: Value must be None or a 4-element list of floats between 0 and 1.

GObject.fillcolor

The object fill color

This value is used to color the backgrounds or, in the case of solid shapes, the shape interior. If there no value (e.g. the fillcolor is None), this shape will have no 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: Value must be None or a 4-element list of floats between 0 and 1.

GObject.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: Value must be either a number (int or float) or a pair of numbers.

GObject.angle

The angle of rotation about the center.

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

invariant: Value must be an int or float

GObject.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: Value must be a str or None

Methods

GObject.contains(point)

Checks whether this shape contains the point

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

Warning: Using this method on a rotated object may slow down your framerate.

Parameters

point (Point2 or a pair of numbers) – the point to check

Returns

True if the shape contains this point

Return type

bool

GObject.transform(point)

Transforms the point to the 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.

Parameters

point (Point2 or a pair of numbers) – the point to transform

Returns

The point transformed to local coordinate system

Return type

Point2

GObject.draw(view)

Draws this shape in the provide view.

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

Parameters

view (GView) – view to draw to

Return to top level