Skip to content

API Reference

Christian Thompson edited this page Jul 7, 2017 · 18 revisions

SPGL Classes

Game Class

Initial Game Setup:

from SPGL import *
# WINDOW_WIDTH, WINDOW_HEIGHT, Background Color, and Window Title
game = Game(800, 600, "blue", "SGE Game Demo") 

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

SPGL.KEY_UP = "Up"
SPGL.KEY_DOWN = "Down"
SPGL.KEY_LEFT = "Left"
SPGL.KEY_RIGHT = "Right"
SPGL.KEY_SPACE = "space"

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])

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

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()

Collision Checking

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

game.is_collision(sprite_1, sprite_2)

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). There is no method to resize an image.

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

Connect with Me

Clone this wiki locally