-
Notifications
You must be signed in to change notification settings - Fork 14
Home
Welcome to the SGE wiki!
SGE is a simple Python game engine / 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.
The purpose of the Simple Game Engine 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.
from SGE import *
# WINDOW_WIDTH, WINDOW_HEIGHT, Background Color, and Window Title
game = SGE(800, 600, "blue", "SGE Game Demo")
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)
game.set_keyboard_binding(key, function)
SGE.KEY_UP = "Up"
SGE.KEY_DOWN = "Down"
SGE.KEY_LEFT = "Left"
SGE.KEY_RIGHT = "Right"
SGE.KEY_SPACE = "space"
set_title(title)
game.set_title("My Game Title")
set_score(score)
game.set_score(100)
update_screen() This is called automatically by the SGE.tick() method.
game.update_screen()
play_sound(soundfile)
The soundfile should be a .wav file in the same folder as the SGE.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()
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()
###SGE.Sprite Class
####Create a Sprite:
The following example shows how to create a sprite (child of the SGE.Sprite class). Sprites are automatically added to the SGE.sprites list. Each frame of the game calls the .tick() method for all sprites whose state is not None.
class Player(SGE.Sprite):
def __init__(self, shape, color, x, y):
SGE.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
sprite.set_image("image.gif", width, height)
Follow me on Twitter @tokyoedtech
Various tutorials available on my YouTube Channel