EDIT: The engine was updated to a new version (1.1).
I would like to introduce you to the engine that I'm developing in Python/Pygame. It is open-source and cross-platform, and it is pretty simple to understand. It is in its very beginning, but it's possible to grasp the main principle already. I'll update it constantly. (Soon I'll introduce a background-redrawing method, and methods to deal with images and sprite animations on a grid-based approach (And also collision-detection)). If you have some technical knowledge, you can understand it all and implement them already (The engine is not complicated).
Download link on sourceforge: https://sourceforge.net/projects/pygrid/
My introduction video on Youtube:
The zip file has two files: pygrid.py and main.py.
The main.py is just a simple pygame program introducing the Pygrid classes.
pygrid.py
"""
====================================================================
PYGRID ENGINE 1.0
Copyright (C) <2014> <Ericson Willians.>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
====================================================================
Engine written by Ericson Willians, a brazilian composer and programmer.
CONTACT: ericsonwrp@gmail.com
AS A COMPOSER: https://soundcloud.com/r-p-ericson-willians
YOUTUBE CHANNEL: http://www.youtube.com/user/poisonewein
====================================================================
"""
__author__ = 'EricsonWillians'
from pygame import *
class Grid():
# A list for the grid's width and height, and a list for the width and height of each individual rect in the grid.
# Lists are mutable, and, therefore, it's possible to change the grid in real-time easily.
def __init__(self, wh, rwh):
self.gridRectWidth = rwh[0]
self.gridRectHeight = rwh[1]
self.gridWidth = wh[0]
self.gridHeight = wh[1]
self.gridWidthInPixels = rwh[0]*wh[0]
self.gridHeightInPixels = rwh[1]*wh[1]
# X and Y positions as lists (They can be altered).
self.keys = [[x for x in range(self.gridWidth)], [x for x in range(self.gridHeight)]]
# X and Y keys. Each key represents a real value in pixels. All positioning on the grid is made through keys.
self.x = dict(zip([x for x in self.keys[0]], [x for x in range(0, self.gridWidthInPixels, self.gridRectWidth)]))
self.y = dict(zip([x for x in self.keys[1]], [x for x in range(0, self.gridHeightInPixels, self.gridRectHeight)]))
self.colors = {"BLACK": (0,0,0), "WHITE": (255,255,255), "RED": (255,0,0), "GREEN": (0,255,0), "BLUE": (0,0,255)}
# These two methods return the true position in pixels of a key.
def getX(self, key):
return self.x.get(key)
def getY(self, key):
return self.y.get(key)
# A grect, or GRID RECTANGLE, is an object.
# All grects have grid-fixed sizes (That's the main philosophy behind this engine):
# Perfect controls and collision-detection giving the grid-fixed movement/interaction of objects.
class Grect():
def __init__(self, x, y, grid, surface, color):
self.x = grid.getX(x) # Its real x position in pixels.
self.y = grid.getY(y) # # Its real y position in pixels.
self.color = color
def draw(self, surface, color, rect):
try: # If the grect position is offset the gridsize, it raises a TypeError, and here we avoid it.
draw.rect(surface, color, rect)
except: # So that when you're offset, it just does not draw it.
pass
# The move method moves the grect in the grid in a specific direction by steps.
# It takes a grid argument, because the game could have more than one grid.
def move(self, direction, step, grid):
if direction == "UP":
self.y -= grid.getX(step)
elif direction == "DOWN":
self.y += grid.getX(step)
elif direction == "LEFT":
self.x -= grid.getX(step)
elif direction == "RIGHT":
self.x += grid.getX(step)
# To be continued...
main.py
"""
====================================================================
PYGRID ENGINE 1.0
Copyright (C) <2014> <Ericson Willians.>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
====================================================================
Engine written by Ericson Willians, a brazilian composer and programmer.
CONTACT: ericsonwrp@gmail.com
AS A COMPOSER: https://soundcloud.com/r-p-ericson-willians
YOUTUBE CHANNEL: http://www.youtube.com/user/poisonewein
====================================================================
"""
__author__ = 'EricsonWillians'
from pygame import *
from random import *
from pygrid import *
init()
# The Grid() constructor takes two arguments: A list of two objects for the grid size in width and height,
# and another one for the width and height of each individual rectangle in the grid.
grid = Grid([20, 20], [30, 30])
speed = 16
clock = time.Clock()
done = False
# The screen has the size of the grid. Essential for the grid-fixed principle of perfect rect-interaction.
screen = display.set_mode((grid.gridWidthInPixels, grid.gridHeightInPixels))
display.set_caption(str(grid.gridWidth) + " by " + str(grid.gridHeight) + " Pygrid")
# Grects.
# ============================================
def createGrect(x, y, grid, color):
g = Grect(x, y, grid, screen, color)
return g
player = createGrect(0, 0, grid, (255,0,0))
# ============================================
def update():
player.draw(screen, (255,0,0), (player.x, player.y, grid.gridRectWidth, grid.gridRectHeight))
display.update() # Pygame default display-thing.
display.flip() # It updates the whole thing at each frame of the loop, or something like that.
clock.tick(speed) # The pygame speed, or something like that.
while not done:
keys = key.get_pressed()
if keys[K_UP]:
player.move("UP", 1, grid)
if keys[K_DOWN]:
player.move("DOWN", 1, grid)
if keys[K_LEFT]:
player.move("LEFT", 1, grid)
if keys[K_RIGHT]:
player.move("RIGHT", 1, grid)
for e in event.get():
if e.type == QUIT or keys[K_ESCAPE]:
done = True
update()
quit()