Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, math, random
- from pygame import Rect
- width, height = 400,400
- screen = pygame.display.set_mode((width, height))
- floor_layer = Surface((width, height))
- wall_layer = Surface((width, height))
- ceil_layer = Surface((width, height))
- grid = []
- gridw, gridh = 10, 10
- tilew, tileh = math.floor(width/gridw), math.floor(height/gridh)
- cols = [ [200, 200, 200], [140, 140, 140], [90, 90, 90] ]
- def loadAssets(name, r, c):
- raw = pygame.image.load(name)
- w, h = raw.get_width(), raw.get_height()
- assets = []
- for y in range(r):
- for x in range(c):
- asset = pygame.Surface((w/c, h/r))
- asset.blit(raw, (0, 0), Rect(x*(w/c), y*(h/r), (w/c), (h/r)))
- asset = pygame.transform.scale(asset, (tilew, tileh))
- assets.append(asset)
- return assets
- tiles = loadAssets("assets.png", 2, 10)
- def findColIndex(col):
- index = 0
- for i in range(len(cols)):
- curcol = cols[i]
- if curcol[0] == col[0] and curcol[1] == col[1] and curcol[2] == col[2]:
- index = i+1
- return index
- for i in range(gridw):
- tab = []
- for j in range(gridh):
- typ = 0
- if (i == 0 or i == gridw-1 or j == 0 or j == gridh-1 or i == int(gridw/2) or j == int(gridh/2) or j == int(gridh/2)-1 or j == 1) and random.randint(0, 100) < 90:
- typ = 1
- tab.append([typ, cols[0], cols[1], cols[2]])
- grid.append(tab)
- def get_tile(x, y):
- if x >= 0 and x < gridw and y >= 0 and y < gridh:
- return grid[x][y]
- return None
- def percent(val, per):
- return int((val/100)*per)
- def addNoise(noises):
- for x in range(width):
- for y in range(height):
- col = screen.get_at((x, y))
- noise = noises[findColIndex(col)]
- ncol = []
- for i in range(3):
- r = random.randint(-noise, noise)
- c = col[i] + r
- if c > 255: c = 255
- if c < 0: c = 0
- ncol.append(c)
- screen.set_at((x, y), ncol)
- def getAdjacents(x, y):
- adjs = [] #left, above, right, below
- for i in range(-1, 2):
- if i != 0:
- t1, t2 = get_tile(x+i, y), get_tile(x, y+i)
- if t1 == None: adjs.append(1)
- else: adjs.append(t1[0])
- if t2 == None: adjs.append(1)
- else: adjs.append(t2[0])
- return adjs
- def draw(x, y):
- tile = get_tile(x, y)
- if tile != None:
- adjs = getAdjacents(x, y)
- if tile[0] == 0: #floor
- bl, br = get_tile(x-1, y+1), get_tile(x+1, y+1)
- if bl == None: bl = [1]
- if br == None: br = [1]
- if adjs[3] == 0:
- floor_layer.blit(tiles[0], (x*tilew, y*tileh))
- elif adjs[3] == 1 and ((adjs[0] == 1 and adjs[2] == 1) or (bl[0] == 1 and br[0] == 1)):
- floor_layer.blit(tiles[16], (x*tilew, y*tileh))
- wall_layer.blit(tiles[16], (x*tilew, y*tileh
- elif adjs[3] == 1 and adjs[0] == 0 and adjs[2] == 0 and bl[0] == 0 and br[0] == 0:
- floor_layer.blit(tiles[17], (x*tilew, y*tileh))
- elif adjs[3] == 1 and adjs[0] == 0 and (adjs[2] == 1 or br[0] == 1) and bl[0] == 0:
- floor_layer.blit(tiles[18], (x*tilew, y*tileh))
- elif adjs[3] == 1 and (adjs[0] == 1 or bl[0] == 1) and adjs[2] == 0 and br[0] == 0:
- floor_layer.blit(tiles[19], (x*tilew, y*tileh))
- else:
- floor_layer.blit(tiles[16], (x*tilew, y*tileh))
- else:
- if adjs[0] == 1 and adjs[1] == 1 and adjs[2] == 1 and adjs[3] == 1:
- ceil_layer.blit(tiles[1], (x*tilew, y*tileh))
- if adjs[0] == 1 and adjs[1] == 1 and adjs[2] == 1 and adjs[3] == 0:
- wall_layer.blit(tiles[2], (x*tilew, y*tileh))
- if adjs[0] == 0 and adjs[1] == 1 and adjs[2] == 0 and adjs[3] == 0:
- wall_layer.blit(tiles[3], (x*tilew, y*tileh))
- if adjs[0] == 1 and adjs[1] == 1 and adjs[2] == 0 and adjs[3] == 0:
- wall_layer.blit(tiles[4], (x*tilew, y*tileh))
- if adjs[0] == 0 and adjs[1] == 1 and adjs[2] == 1 and adjs[3] == 0:
- wall_layer.blit(tiles[5], (x*tilew, y*tileh))
- if adjs[0] == 0 and adjs[1] == 1 and adjs[2] == 0 and adjs[3] == 1:
- wall_layer.blit(tiles[6], (x*tilew, y*tileh))
- if adjs[0] == 1 and adjs[1] == 1 and adjs[2] == 0 and adjs[3] == 1:
- ceil_layer.blit(tiles[7], (x*tilew, y*tileh))
- if adjs[0] == 0 and adjs[1] == 1 and adjs[2] == 1 and adjs[3] == 1:
- ceil_layer.blit(tiles[8], (x*tilew, y*tileh))
- if adjs[0] == 1 and adjs[1] == 0 and adjs[2] == 1 and adjs[3] == 1:
- ceil_layer.blit(tiles[9], (x*tilew, y*tileh))
- if adjs[0] == 1 and adjs[1] == 0 and adjs[2] == 1 and adjs[3] == 0:
- wall_layer.blit(tiles[10], (x*tilew, y*tileh))
- if adjs[0] == 0 and adjs[1] == 0 and adjs[2] == 0 and adjs[3] == 0:
- wall_layer.blit(tiles[11], (x*tilew, y*tileh))
- if adjs[0] == 1 and adjs[1] == 0 and adjs[2] == 0 and adjs[3] == 0:
- wall_layer.blit(tiles[12], (x*tilew, y*tileh))
- if adjs[0] == 0 and adjs[1] == 0 and adjs[2] == 1 and adjs[3] == 0:
- wall_layer.blit(tiles[13], (x*tilew, y*tileh))
- if adjs[0] == 0 and adjs[1] == 0 and adjs[2] == 0 and adjs[3] == 1:
- ceil_layer.blit(tiles[14], (x*tilew, y*tileh))
- if adjs[0] == 1 and adjs[1] == 0 and adjs[2] == 0 and adjs[3] == 1:
- ceil_layer.blit(tiles[15], (x*tilew, y*tileh))
- if adjs[0] == 0 and adjs[1] == 0 and adjs[2] == 1 and adjs[3] == 1:
- ceil_layer.blit(tiles[15], (x*tilew, y*tileh))
- else:
- print("non existent tile @ ",x,",",y)
- def drawGrid():
- for x in range(gridw):
- for y in range(gridh):
- draw(x, y)
- #addNoise([0, 15, 15, 15])
- pygame.display.flip()
- drawGrid()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement