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)

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. For example, to initialize the x position and the fill color, use the constructor call:

GObject(x=2,fillcolor='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.

Parameters

keywords (keys are attribute names) – dictionary of keyword arguments

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 introcs, or a string with a valid color name. If you chose either of these alternate representations (a string or an object from introcs), 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 introcs, or a string with a valid color name. If you chose either of these alternate representations (a string or an object from introcs), 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

GObject.hitbox

The hitbox for this object.

The hitbox is a rectangle that need not agree with the bounding rectangle of this object. For example, images are have transparencies on their edges, which means tha the visible part of the image is smaller than the image file as a hole. The hitbox attribute reflects this difference, so that we can have accurate collisions.

If hitbox is None, then the normal bounding box (given by x, y, width, height) is used for collisions. Otherwise, hitbox is an 4-element list of offsets. Each value is the amount to pull the hitbox in towards the center of the object. For example, a hitbox of (1,2,3,4) says to shift the left edge of the hitbox 1 pixel to the right, the top edge 2 pixels down, the right edge 3 pixels to the left, and the bottom edge 4 pixels up.

Invariant: Value is either None or a 4-element tuple of numbers.

Methods

GObject.collides(obj)

Checks whether this object collides with another.

This collision method takes hitboxes into account

Parameters

obj (GObject) – the object to check for collision

Returns

True if the shape collides with object

Return type

bool

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 provided view.

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

Parameters

view (GView) – view to draw to

Return to top level