ggMath Mathematics

These mathematics and geometry extensions subclass the App and Sprite classes to create a framework for building apps that mimic some of the functionality of online math tools like Geogebra.

This mathapp module implements base classes for Sprite-based classes defined in this module.

These extensions are very experimental and are not fully developed!

ggMath Application

MathApp

class ggame.mathapp.MathApp(scale=200)[source]

MathApp is a subclass of the ggame App class. It incorporates the following extensions:

  • Support for zooming the display using the mouse wheel
  • Support for click-dragging the display using the mouse button
  • Automatic execution of step functions in all objects and sprites
    sub-classed from _MathDynamic.
Parameters:scale (float) – Optional parameter sets the initial scale of the display in units of pixels per logical unit. The default is 200.
Returns:MathApp instance
viewPosition

Attribute is used to get or set the current logical coordinates at the center of the screen as a tuple of floats (x,y).

scale

Attribute reports the current view scale (pixels per logical unit).

width

Attribute reports the physical screen width (pixels).

classmethod getSpritesbyClass(sclass)

Returns a list of all active sprites of a given class.

Parameters:sclass (class) – A class name (e.g. ‘Sprite’) or subclass.
Returns:A (potentially empty) list of sprite references.
classmethod listenKeyEvent(eventtype, key, callback)

Register to receive keyboard events.

Parameters:
  • eventtype (str) – The type of key event to receive (value is one of: ‘keydown’, ‘keyup’ or ‘keypress’).
  • key (str) – Identify the keyboard key (e.g. ‘space’, ‘left arrow’, etc.) to receive events for.
  • callback (function) – The function or method that will be called with the KeyEvent object when the event occurs.
Returns:

Nothing

See the source for KeyEvent for a list of key names to use with the key paramter.

classmethod listenMouseEvent(eventtype, callback)

Register to receive mouse events.

Parameters:
  • eventtype (str) – The type of mouse event to receive (value is one of: ‘mousemove’, ‘mousedown’, ‘mouseup’, ‘click’, ‘dblclick’ or ‘mousewheel’).
  • callback (function) – The function or method that will be called with the ggame.event.MouseEvent object when the event occurs.
Returns:

Nothing

classmethod unlistenKeyEvent(eventtype, key, callback)

Use this method to remove a registration to receive a particular keyboard event. Arguments must exactly match those used when registering for the event.

Parameters:
  • eventtype (str) – The type of key event to stop receiving (value is one of: ‘keydown’, ‘keyup’ or ‘keypress’).
  • key (str) – The keyboard key (e.g. ‘space’, ‘left arrow’, etc.) to stop receiving events for.
  • callback (function) – The function or method that will no longer be called with the KeyEvent object when the event occurs.
Returns:

Nothing

See the source for KeyEvent for a list of key names to use with the key paramter.

classmethod unlistenMouseEvent(eventtype, callback)

Use this method to remove a registration to receive a particular mouse event. Arguments must exactly match those used when registering for the event.

Parameters:
  • eventtype (str) – The type of mouse event to stop receiving events for (value is one of: ‘mousemove’, ‘mousedown’, ‘mouseup’, ‘click’, ‘dblclick’ or ‘mousewheel’).
  • callback (function) – The function or method that will no longer be called with the ggame.event.MouseEvent object when the event occurs.
Returns:

Nothing

classmethod logicalToPhysical(lp)[source]

Transform screen coordinates from logical to physical space. Output depends on the current ‘zoom’ and ‘pan’ of the screen.

Parameters:lp (tuple(float,float)) – Logical screen coordinates (x, y)
Return type:tuple(float,float)
Returns:Physical screen coordinates (x, y)
classmethod physicalToLogical(pp)[source]

Transform screen coordinates from physical to logical space. Output depends on the current ‘zoom’ and ‘pan’ of the screen.

Parameters:lp (tuple(float,float)) – Physical screen coordinates (x, y)
Return type:tuple(float,float)
Returns:Logical screen coordinates (x, y)
classmethod translateLogicalToPhysical(pp)[source]

Transform screen translation from logical to physical space. Output only depends on the current ‘zoom’ of the screen.

Parameters:lp (tuple(float,float)) – Logical screen translation pair (delta x, delta y)
Return type:tuple(float,float)
Returns:Physical screen translation ordered pair (delta x, delta y)
classmethod translatePhysicalToLogical(pp)[source]

Transform screen translation from physical to logical space. Output only depends on the current ‘zoom’ of the screen.

Parameters:lp (tuple(float,float)) – Physical screen translation pair (delta x, delta y)
Return type:tuple(float,float)
Returns:Logical screen translation ordered pair (delta x, delta y)
classmethod distance(pos1, pos2)[source]

Utility for calculating the distance between any two points.

Parameters:
  • pos1 (tuple(float,float)) – The first point
  • pos2 (tuple(float,float)) – The second point
Return type:

float

Returns:

The distance between the two points (using Pythagoras)

classmethod addViewNotification(handler)[source]

Register a function or method to be called in the event the view position or zoom changes.

Parameters:handler (function) – The function or method to be called
Returns:Nothing
classmethod removeViewNotification(handler)[source]

Remove a function or method from the list of functions to be called in the event of a view position or zoom change.

Parameters:handler (function) – The function or method to be removed
Returns:Nothing
run(userfunc=None)

Calling the run() method begins the animation process whereby the step() method is called once per animation frame.

Parameters:userfunc (function) – Any function or method which shall be called once per animation frame.
Returns:Nothing

ggMath Base Class for Visual Objects

class ggame.mathapp._MathVisual(asset, *args, **kwargs)[source]

Abstract Base Class for all visual, potentially dynamic objects.

Parameters:
  • asset (Asset) – A valid ggame asset object.
  • args (list) – A list of required positional or non-positional arguments as named in the _posinputsdef and _nonposinputsdef lists overridden by child classes.
  • **kwargs – See below
Optional Keyword Arguments:
 
  • positioning (string) One of ‘logical’ or ‘physical’
  • size (int) Size of the object (in pixels)
  • width (int) Width of the object (in pixels)
  • color (Color) Valid Color object
  • style (LineStyle) Valid LineStyle object
selected = None

True if object is currently selected by the UI.

mouseisdown = None

True if object is tracking UI mouse button as down.

destroy()[source]

Prevent the sprite from being displayed or checked in collision detection. Once this is called, the sprite can no longer be displayed or used. If you only want to prevent a sprite from being displayed, set the visible attribute to False.

positioning

Whether object was created with ‘logical’ or ‘physical’ positioning.

movable

Whether object can be moved. Set-able and get-able.

selectable

Whether object can be selected by the UI. Set-able and get-able.

strokable

Whether the object supports a click-drag input from the UI mouse. Set-able and get-able.

select()[source]

Place the object in a ‘selected’ state.

Param:None
Returns:None
unselect()[source]

Place the object in an ‘unselected’ state.

Param:None
Returns:None
mousedown()[source]

Inform the object of a ‘mouse down’ event.

Param:None
Returns:None
mouseup()[source]

Inform the object of a ‘mouse up’ event.

Param:None
Returns:None
processEvent(event)[source]

Inform the object of a generic ggame event.

Parameters:event – The ggame event object to receive and process.
Returns:None

This method is intended to be overridden.

physicalPointTouching(ppos)[source]

Determine if a physical point is considered to be touching this object.

Parameters:ppos (tuple(int,int)) – Physical screen coordinates.
Return type:boolean
Returns:True if touching, False otherwise.

This method must be overridden.

translate(pdisp)[source]

Perform necessary processing in response to being moved by the mouse/UI.

Parameters:pdisp (tuple(int,int)) – Translation vector (x,y) in physical screen units.
Returns:None

This method must be overridden.

stroke(ppos, pdisp)[source]

Perform necessary processing in response to click-drag action by the mouse/UI.

Parameters:
  • ppos (tuple(int,int)) – Physical coordinates of stroke start.
  • pdisp (tuple(int,int)) – Translation vector of stroke action in physical screen units.
Returns:

None

This method is intended to be overridden.

canStroke(ppos)[source]

Can the object respond to beginning a stroke action at the given position.

Parameters:ppos (tuple(int,int)) – Physical coordinates of stroke start.
Return type:Boolean
Returns:True if the object can respond, False otherwise.

This method is intended to be overridden.

Point Objects

These classes are subclasses of Sprite and are used to represent points in geometry and mathematics.

_Point

This is the abstract base class for all point classes.

class ggame.point._Point(asset, *args, **kwargs)[source]

Abstract base class for all point classes.

Parameters:
  • asset (Asset) – A valid ggame Asset object
  • pos (tuple(float,float)) – The position (physical or logical)
step()[source]

Perform periodic processing.

physicalPointTouching(ppos)[source]

Determine if a physical point is considered to be touching this point.

Parameters:ppos (tuple(int,int)) – Physical screen coordinates.
Return type:boolean
Returns:True if touching, False otherwise.
translate(pdisp)[source]

Perform necessary processing in response to being moved by the mouse/UI.

Parameters:pdisp (tuple(int,int)) – Translation vector (x,y) in physical screen units.
Returns:None
distanceTo(otherpoint)[source]

Compute the distance to another _Point object.

Parameters:otherpoint (_Point) – A reference to the other _Point
Return type:float
Returns:The distance (in logical units) to the other point

Point

class ggame.point.Point(*args, **kwargs)[source]

Basic point object representing any point in a geometrical sense. An instantiated Point object is callable and will return a tuple with its logical position as an (x,y) pair.

Parameters:
  • pos (tuple(float,float)) – Position in physical or logical units.
  • **kwargs – See below
Optional Keyword Arguments:
 
  • positioning (str) One of ‘logical’ (default) or ‘physical’
  • size (int) Radius of the point (in pixels)
  • color (Color) Valid Color object
  • style (LineStyle) Valid LineStyle object

Example:

from ggame.asset import Color
from ggame.point import Point
from ggame.mathapp import MathApp

p1 = Point((0,1), color=Color(0xff8000, 1.0))
p1.movable = True
# An orange point that can be moved

p2 = Point(lambda: (p1()[0], p1()[1]+1))
# A point position based on P1
p3 = Point((1,0))
# A third, fixed point

MathApp().run()

ImagePoint

class ggame.point.ImagePoint(url, *args, **kwargs)[source]
Point object that uses an image as its on-screen
representation.
Parameters:
  • url (str) – Location of an image file (png, jpg)
  • *args – See below
  • **kwargs – See below
Required Arguments:
 
  • pos (tuple(float,float)) Position in physical or logical units.
Optional Keyword Arguments:
 
  • positioning (str) One of ‘logical’ (default) or ‘physical’
  • frame (Frame) The sub-frame location of image within the image file
  • qty (int) The number of sub-frames, when used as a sprite sheet
  • direction (str) One of ‘horizontal’ (default) or ‘vertical’
  • margin (int) Pixels between sub-frames if sprite sheet
physicalPointTouching(ppos)[source]

Determine if a physical point is considered to be touching point’s image.

Parameters:ppos (tuple(int,int)) – Physical screen coordinates.
Return type:boolean
Returns:True if touching, False otherwise.

Line Objects

This category currently only has one class: LineSegment, but will eventually be extended to include at least Line and Ray.

LineSegment

class ggame.line.LineSegment(*args, **kwargs)[source]

Create a line segment on the screen. This is a subclass of Sprite and _MathVisual but most of the inherited members are of little use and are not shown in the documentation.

Parameters:
  • *args – See below
  • **kwargs – See below
Required Arguments:
 
  • pos (tuple(float,float)) Starting point of the segment, which may
    be a literal tuple of floats, or a reference to any object or function that returns or evaluates to a tuple of floats.
  • end (tuple(float,float)) Ending point of the segment (see above)
Optional Keyword Arguments:
 
  • positioning (str) One of ‘logical’ or ‘physical’
  • style (LineStyle) Valid LineStyle object

Example:

from ggame.point import Point
from ggame.line import LineSegment
from ggame.mathapp import MathApp

p1 = Point((2,1))
ls = LineSegment(p1, Point((1,1)))

MathApp().run()
physicalPointTouching(ppos)[source]

This method always returns False.

translate(pdisp)[source]

This method is not implemented.

Circle

class ggame.circle.Circle(*args, **kwargs)[source]

Create a circle on the screen. This is a subclass of Sprite and _MathVisual but most of the inherited members are of little use and are not shown in the documentation.

Parameters:
  • *args – See below
  • **kwargs – See below
Required Arguments:
 
  • pos (tuple(float,float)) Center point of the circle, which may
    be a literal tuple of floats, or a reference to any object or function that returns or evaluates to a tuple of floats.
  • radius [float or Point] Radius of the circle (logical units)
    or a Point on the circle.
Optional Keyword Arguments:
 
  • positioning (str) One of ‘logical’ or ‘physical’
  • style (LineStyle) Valid LineStyle object
  • color (Color) Valid Color object`

Example:

from ggame.point import Point
from ggame.circle import Circle
from ggame.mathapp import MathApp

p1 = Point((2,1))
c = Circle(p1, 1.4)

MathApp().run()
center

This attribute represents the horizontal and vertical position of the sprite “center” as a tuple of floating point numbers. See the descriptions for fxcenter and fycenter for more details.

physicalPointTouching(ppos)[source]

Determine if a physical point is considered to be touching this object.

Parameters:ppos (tuple(int,int)) – Physical screen coordinates.
Return type:boolean
Returns:True if touching, False otherwise.

This method must be overridden.

translate(pdisp)[source]

Perform necessary processing in response to being moved by the mouse/UI.

Parameters:pdisp (tuple(int,int)) – Translation vector (x,y) in physical screen units.
Returns:None

This method must be overridden.

Text Objects

Label

class ggame.label.Label(*args, **kwargs)[source]

Create a text label on the screen. This is a subclass of Sprite and _MathVisual but most of the inherited members are of little use and are not shown in the documentation.

Parameters:
  • *args – See below
  • **kwargs – See below
Required Arguments:
 
  • pos (tuple(float,float)) Screen position of the label, which may
    be a literal tuple of floats, or a reference to any object or function that returns or evaluates to a tuple of floats.
  • text (str) Text to appear in the label. This may be a literal
    string or a reference to any object or function that returns or evaluates to a string.
Optional Keyword Arguments:
 
  • positioning (str) One of ‘logical’ or ‘physical’
  • size (int) Size of text font (in pixels)
  • width (int) Width of the label (in pixels)
  • color (Color) Valid Color object

Example:

from ggame.asset import Color
from ggame.label import Label
from ggame.mathapp import MathApp

Label(
    (20,80),                # physical location on screen
    "Initial Speed (m/s)",  # text to display
    size=15,                # text size (pixels)
    positioning="physical", # use physical coordinates
    color=Color(0x202000, 1.0)) # text color
    
MathApp().run()

Indicators

ImageIndicator

class ggame.indicator.ImageIndicator(url, *args, **kwargs)[source]

LEDIndicator

class ggame.indicator.LEDIndicator(*args, **kwargs)[source]

Input Controls

Slider

class ggame.slider.Slider(*args, **kwargs)[source]

Create a ‘slider’ style numeric input control on the screen. This is a subclass of Sprite and _MathVisual but most of the inherited members are of little use and are not shown in the documentation.

Parameters:
  • *args – See below
  • **kwargs – See below
Required Arguments:
 
  • pos (tuple(float,float)) Screen position of the slider, which may
    be a literal tuple of floats, or a reference to any object or function that returns or evaluates to a tuple of floats.
  • minval (float) The minimum value of the slider
  • maxval (float) The maximum value of the slider
  • initial (float) The initial value of the slider
Optional Keyword Arguments:
 
  • steps (int) Number of steps between minval and maxval (default 50)
  • leftkey (str) Name of a keyboard key that will make the
    slider control step down (move left) (default None). See KeyEvent for a list of names.
  • rightkey (str) Name of a keyboard key that will make the slider
    control step up (move right) (default None)
  • centerkey (str) Name of a keyboard key that will make the slider
    move to its center position (default None)
  • positioning (str) One of ‘logical’ or ‘physical’
  • size (int) Width of the slider (in pixels)
  • color (Color) Valid Color object
  • style (LineStyle) Valid LineStyle object

Example:

from ggame.slider import Slider
from ggame.mathapp import MathApp

s = Slider(
    (100, 150),     # screen position
    0,              # minimum value
    250,            # maximum value
    125,            # initial value
    positioning='physical', # use physical coordinates for position
    steps=10)       # 10 steps between 125 and 250
    
MathApp().run()

InputNumeric

class ggame.input.InputNumeric(pos, val, **kwargs)[source]

Create a Label that can be selected to accept new numeric input from the keyboard. This is a subclass of Sprite and _MathVisual but most of the inherited members are of little use and are not shown in the documentation.

Parameters:
  • pos (tuple(float,float)) – Screen position of the control, which may be a literal tuple of floats, or a reference to any object or function that returns or evaluates to a tuple of floats.
  • val (float) – Initial value of the input text
  • **kwargs – See below
Optional Keyword Arguments:
 
  • fmt (str) a Python format string (default is {0:.2f})
  • positioning (str) One of ‘logical’ or ‘physical’
  • size (int) Size of text font (in pixels)
  • width (int) Width of the label (in pixels)
  • color (Color) Valid Color object

Example:

from ggame.input import InputNumeric
from ggame.mathapp import MathApp

p = InputNumeric(
    (300, 275),     # screen coordinates of input
    3.14,           # initial value
    positioning="physical") # use physical coordinates

MathApp().run()

InputButton

class ggame.input.InputButton(callback, *args, **kwargs)[source]

Create a Label that can be clicked with a mouse to execute a user-defined function. This is a subclass of Sprite and _MathVisual but most of the inherited members are of little use and are not shown in the documentation.

Parameters:
  • callback (function) – A reference to a function to execute, passing this button object, when the button is clicked
  • pos (tuple(float,float)) – Screen position of the control, which may be a literal tuple of floats, or a reference to any object or function that returns or evaluates to a tuple of floats.
  • val (float) – Initial value of the input text
  • *args – See below
  • **kwargs – See below
Required Arguments:
 
  • pos (tuple(float,float)) Screen position of the button, which may
    be a literal tuple of floats, or a reference to any object or function that returns or evaluates to a tuple of floats.
  • text (str) Text to appear in the button. This may be a literal
    string or a reference to any object or function that returns or evaluates to a string.
Optional Keyword Arguments:
 
  • positioning (str) One of ‘logical’ or ‘physical’
  • size (int) Size of text font (in pixels)
  • width (int) Width of the label (in pixels)
  • color (Color) Valid Color object

Example:

from ggame.asset import Color
from ggame.label import InputButton
from ggame.mathapp import MathApp

def pressbutton(buttonobj):
    print("InputButton pressed!")

InputButton(
    pressbutton,            # reference to handler
    (20,80),                # physical location on screen
    "Press Me",             # text to display
    size=15,                # text size (pixels)
    positioning="physical") # use physical coordinates

MathApp().run()

InputImageButton

class ggame.inputpoint.InputImageButton(url, callback, *args, **kwargs)[source]

InputImageToggle

class ggame.inputpoint.InputImageToggle(url, statelist, initindex, *args, **kwargs)[source]

MetalToggle

class ggame.inputpoint.MetalToggle(initindex, *args, **kwargs)[source]

GlassButton

class ggame.inputpoint.GlassButton(callback, *args, **kwargs)[source]

Time Utility

Timer

class ggame.timer.Timer[source]