Skip to content

API Reference

Christian Thompson edited this page Oct 10, 2019 · 18 revisions

SPGL Classes

Game Class

def __init__(self, screen_width = 800, screen_height = 600, background_color = "black", title = "Simple Game Engine by /u/wynand1004 AKA @TokyoEdTech", splash_time = 3):

Initial Game Setup:

# Import into a separate namespace
import spgl

# Initial Game setup
game = spgl.Game(800, 600, "black", "My Game Title", 5)

SPGL Attributes

title # Window title text (string)
gravity # Game gravity constanst (float)
state # Game state (string) or None
FPS # Game FPS - default is 30 FPS (float)
SCREEN_WIDTH # Width of the screen in pixels (int)
SCREEN_HEIGHT # Height of the screen in pixels (int)
time # Time in seconds (float)

SPGL Methods and Attibutes

Keyboard Bindings

game.set_keyboard_binding(key, function)

Built-in Key Definitions

KEY_UP = "Up"
KEY_DOWN = "Down"
KEY_LEFT = "Left"
KEY_RIGHT = "Right"
KEY_SPACE = "space"
KEY_ENTER = "Return"
KEY_RETURN = "Return"
KEY_SHIFT_LEFT = "Shift_L"
KEY_SHIFT_RIGHT = "Shift_R"
KEY_CONTROL_LEFT = "Control_L"
KEY_CONTROL_RIGHT = "Control_R"
KEY_ALT_LEFT = "Alt_L"
KEY_ALT_RIGHT = "Alt_L"
KEY_CAPS_LOCK = "Caps_Lock"
KEY_F1 = "F1"
KEY_F2 = "F2"
KEY_F3 = "F3"
KEY_F4 = "F4"
KEY_F5 = "F5"
KEY_F6 = "F6"
KEY_F7 = "F7"
KEY_F8 = "F8"
KEY_F9 = "F9"
KEY_F10 = "F10"
KEY_F11 = "F11"
KEY_F12 = "F12"

set_title(title)

game.set_title("My Game Title")

update_screen() This is called automatically by the SPGL.tick() method.

game.update_screen()

play_sound(soundfile, time = 0)

The soundfile should be a .wav file in the same folder as the SPGL.py file and your game file. Depending on your system configuration, .mp3, .ogg, .aiff, etc. files may also work. .Wav files are recommended for maximum cross platform compatibility. If time is greater than zero, then the sound will repeat after time seconds. This would be mostly used for adding background music.

game.play_sound("explosion.wav")

stop_all_sounds()

game.stop_all_sounds() # Not implemented in Windows yet

Pop ups

game.ask_yes_no(title, message)

game.show_info(title, message)

game.show_warning(title, message)

game.show_error(title, message)

game.ask_question(title, message)

game.ask_ok_cancel(title, message):

game.ask_retry_cancel(title, message):

game.game.ask_open_filename():
 - Returns file path to chosen file

clear_terminal_screen()

game.clear_terminal_screen()

print_game_info()

Prints the Game Title, Window Dimensions, Number of Sprites, and Frames Per Second.

game.print_game_info()

is_collision(sprite_1, sprite_2)

is_collision uses an Axis Aligned Bounding Box and returns True or False.

game.is_collision(sprite_1, sprite_2)

is_circle_collision(sprite_1, sprite_2, distance)

is_circle_collision uses the Pythagorean Theorem to calculate the distance between the center of two sprites. If the distance is less than the given value, the function will return True, otherwise it will return False.

after(function, milliseconds)

This will call a function after the specified number of milliseconds.

game.after(function, milliseconds) # Note, the function should NOT have parentheses

exit()

This will stop all sounds and exit the game.

game.exit()

SPGL.Sprite Class

Note: The Sprite Class is a child of the turtle module's Turtle class. So, all methods and attributes of the Turte class are inherited by the Sprite class. One exception is the .speed() method. For more information on the Turtle class, see the Python Turtle Module Documentation.

Create a Sprite:

The following example shows how to create a sprite (child of the SPGL.Sprite class). Sprites are automatically added to the Game.sprites list. Each frame of the game calls the .tick() method for all sprites whose state is not None.

class Player(spgl.Sprite):
    def __init__(self, shape, color, x, y):
        spgl.Sprite.__init__(self, shape, color, x, y)

# Shape, Color, starting x coordinate, starting y coordinate
player = Player("triangle", "red", -400, 0)

Sprite Attributes

dx # x velocity (float)
dy # y velocity (float)
speed # speed (float)
acceleration # acceleration (float)
width # width in pixels (float)
height # height in pixels (float)
state # object state - default is "active" Use None for sprites that are no longer needed.
friction # friction (float)
solid # (bool)

Sprite Methods

Tick

This method is called once for each frame automatically for all sprites whose state is not None.

sprite.tick()

Move

sprite.move()

Destroy

The destroy method hides the sprite, moves it off the screen, and sets its state to None. There is no way to delete a sprite from memory - this is a limitation of the Turtle Module.

sprite.destroy()

Set Image

This method changes the shape of the sprite to an external image. The image must be a .gif. There is no method to rotate the image. Also, the width and height are the actual dimensions of the image (in pixels) or the size of the bounding box to be used for collision detection. There is no method to resize an image.

sprite.set_image("image.gif", width, height)

SPGL.Label Class

The SPGL.Label Class provides a simple way to display text on the screen.

Create a Label

class Label(turtle.Turtle):
    def __init__(self, text, color, x = 0, y = 0, font_name = "Arial", font_size = 12, font_type = "normal", align = "left"):

The Font information is optional. The default is Arial, 12pt, Normal Font.

label_name = spgl.Label("I am a white label.", "white", -280, 280)

Label Attributes

text # the text to be displayed

Label Methods

update(text) # Updates the text in the label

SPGL.Button Class

The SPGL.Button Class provides a simple graphical button that can be clicked. The button can use the turtle primitives (square, circle, triangle, etc.) or a custom .gif image can be used.

class Button(turtle.Turtle):
    def __init__(self,shape, color, x = 0, y = 0):

Create a Button

button_name = spgl.Button("square", "blue", -100, 100)

Button Attributes

Button Methods

set_image(image) # Change the image

click(x, y) # What to do if image is clicked.  Arguments passed automatically.

Connect with Me