Skip to content
Christian Thompson edited this page Jul 2, 2017 · 29 revisions

Welcome to the SPGL wiki!

SPGL (Simple Python Game Library) is a simple Python game library / framework used for creating simple 2D games. It is built on the Python Turtle module and is compatible with Python 2.x and 3.x.

Overview:

The purpose of the Simple Python Game Library is to give beginning Python coders a simple framework to make basic 2D games. It is intended as an alternative to Pygame. As it is built on the Turtle module, it has the same features and limitations of that module. It does not require any external libraries to be added.

Changelog:

Version 0.7

This was a major update to the structure of the code. The code is structured a bit more logically to facility importing into the main namespace and to keep the code more consistent. Progroms written for Version 0.6 and below are not compatible with this version.

There are 2 ways to import the code:

1) Keep a separate namespace

import SPGL

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

# Initial Game setup
game = SPGL.Game()

# Keyboard Binding
game.set_keyboard_binding(SPGL.KEY_UP, player.accelerate)

2) Import into the main namespace

from SPGL import *

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

# Initial Game setup
game = Game()

# Keyboard Binding
game.set_keyboard_binding(KEY_UP, player.accelerate)

Version 0.6

SPGL.py

SPGL Class

  • Added error logging
  • Added basic button class
  • Added method for clicking on the screen
  • Simplified .load_data method
  • .print_game_info() shows more information, including error logs
  • Added default splash screen
  • Added modal popups (ask_yes_no, show_info, show_warning)

Sprite class

  • init can now take image file (.gif only) as shape argument (along with width and height arguents)

Label class

  • Labels stored in Game.labels and automatically updated each tick

Button class

  • Simple button class added

Version 0.5

A way to save and load data added .save_data(key, value) .load_data()

More work on the Splash Screen Done (Not finished yet)

Version 0.4

Added a basic Label class for adding text to the screen. Small engine improvements (turtle.setundobuffer(0))

Version 0.3

Initial version with basic game framework and Sprite class.

Documentation:

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

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.

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