Skip to content

Instantly share code, notes, and snippets.

/game.py Secret
Created Mar 18, 2013

Embed
What would you like to do?
Implementation of Wang Tiles in Python 3 (using pygame)
import sys, pygame
from tileset import *
from map import *
pygame.init()
COLOR_BG = 70, 70, 100
size = WIDTH, HEIGHT = 1280, 800
screen = pygame.display.set_mode(size)
tileset = Tileset("res/tiles.png", 16, 16)
map = Map(80, 50)
map.generateMap()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: map.generateMap()
elif event.key == pygame.K_ESCAPE: sys.exit()
screen.fill(COLOR_BG)
for x in range(map.mapWidth):
for y in range(map.mapHeight):
tile = map.map[y][x].getTile(tileset) # Get the correct tile
tile[1].x = tileset.tileWidth * x
tile[1].y = tileset.tileHeight * y
screen.blit(tile[0], tile[1]) # Blit at the position
pygame.display.flip()
from random import *
from tile import *
class Map:
def __init__(self, mapWidth, mapHeight):
self.mapWidth = mapWidth
self.mapHeight = mapHeight
self.map = []
def generateMap(self):
del self.map[:] # Clear map if one is present
for y in range(self.mapHeight):
self.map.append([])
for x in range(self.mapWidth):
self.map[y].append(Tile())
n = None
w = None
if y > 0: # if the map extends north of the current tile
n = self.map[y-1][x].south() # Get the northern tile's south side type
if x > 0: # If the map extends west of the current tile
w = self.map[y][x-1].east() # Get the western tile's east side type
# Build lists with matching n and w properties
if n:
LN = [x for x in tileTypes if x[2] is n]
if w:
LW = [x for x in tileTypes if x[5] is w]
if n and w:
LNW = list(set(LN).intersection(set(LW)))
if n is not None: # Northern tile exists
if w is not None: # Western tile exists
self.map[y][x].setType(choice(LNW))
else:
self.map[y][x].setType(choice(LN))
else: # No northern tile exists
if w is not None: # Western tile exists
self.map[y][x].setType(choice(LW))
else: # No restrictions so choose any tile at random
self.map[y][x].setType(choice(tileTypes))
from tileset import *
tileTypes = []
# Row 1 X Y N E S W
tileTypes.append((0, 0, 'o', 'c', 'o', 'c'))
tileTypes.append((1, 0, 'o', 'c', 'c', 'o'))
tileTypes.append((2, 0, 'o', 'o', 'c', 'c'))
tileTypes.append((3, 0, 'o', 'o', 'c', 'o'))
tileTypes.append((4, 0, 'o', 'c', 'o', 'o'))
tileTypes.append((5, 0, 'o', 'o', 'o', 'c'))
tileTypes.append((6, 0, 'o', 'o', 'o', 'o'))
tileTypes.append((7, 0, 'c', 'c', 'c', 'c'))
# Row 2 X Y N E S W
tileTypes.append((0, 1, 'c', 'o', 'c', 'o'))
tileTypes.append((1, 1, 'c', 'o', 'o', 'c'))
tileTypes.append((2, 1, 'c', 'c', 'o', 'o'))
tileTypes.append((3, 1, 'c', 'o', 'o', 'o'))
class Tile:
def __init__(self):
self.type = None
def setType(self, type):
self.type = type
def getTile(self, tileset):
return tileset.getTile(self.type[0], self.type[1])
def north(self):
return self.type[2]
def east(self):
return self.type[3]
def south(self):
return self.type[4]
def west(self):
return self.type[5]
import pygame
class Tileset:
def __init__(self, path, tileWidth, tileHeight, scale2x=False):
self.tilesetImage = pygame.image.load(path)
self.tileWidth = tileWidth
self.tileHeight = tileHeight
if scale2x:
scale2x(self)
def scale2x(self):
self.tilesetImage = pygame.transform.scale2x(self.tilesetImage)
self.tileWidth *= 2
self.tileHeight *= 2
def getTile(self, x, y):
subtile = self.tilesetImage.subsurface(
x * self.tileWidth,
y * self.tileHeight,
self.tileWidth,
self.tileHeight)
return subtile, subtile.get_rect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.