Advertisement
billysback

pygame thing for cruor

Sep 13th, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.07 KB | None | 0 0
  1. import pygame, math, random
  2. from pygame import Rect
  3.  
  4. width, height = 400,400
  5. screen = pygame.display.set_mode((width, height))
  6. floor_layer = Surface((width, height))
  7. wall_layer = Surface((width, height))
  8. ceil_layer = Surface((width, height))
  9.  
  10. grid = []
  11. gridw, gridh = 10, 10
  12. tilew, tileh = math.floor(width/gridw), math.floor(height/gridh)
  13.  
  14. cols = [ [200, 200, 200], [140, 140, 140], [90, 90, 90] ]
  15.  
  16. def loadAssets(name, r, c):
  17.     raw = pygame.image.load(name)
  18.     w, h = raw.get_width(), raw.get_height()
  19.    
  20.     assets = []
  21.     for y in range(r):
  22.         for x in range(c):
  23.             asset = pygame.Surface((w/c, h/r))
  24.             asset.blit(raw, (0, 0), Rect(x*(w/c), y*(h/r), (w/c), (h/r)))
  25.             asset = pygame.transform.scale(asset, (tilew, tileh))
  26.             assets.append(asset)
  27.     return assets
  28.  
  29. tiles = loadAssets("assets.png", 2, 10)
  30.  
  31.  
  32. def findColIndex(col):
  33.     index = 0
  34.     for i in range(len(cols)):
  35.         curcol = cols[i]
  36.         if curcol[0] == col[0] and curcol[1] == col[1] and curcol[2] == col[2]:
  37.             index = i+1
  38.     return index
  39.  
  40. for i in range(gridw):
  41.     tab = []
  42.     for j in range(gridh):
  43.         typ = 0
  44.         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:
  45.             typ = 1
  46.         tab.append([typ, cols[0], cols[1], cols[2]])
  47.     grid.append(tab)
  48.  
  49. def get_tile(x, y):
  50.     if x >= 0 and x < gridw and y >= 0 and y < gridh:
  51.         return grid[x][y]
  52.     return None
  53.  
  54.  
  55.  
  56. def percent(val, per):
  57.     return int((val/100)*per)
  58.  
  59. def addNoise(noises):
  60.     for x in range(width):
  61.         for y in range(height):
  62.             col = screen.get_at((x, y))
  63.             noise = noises[findColIndex(col)]
  64.             ncol = []
  65.             for i in range(3):
  66.                 r = random.randint(-noise, noise)
  67.                 c = col[i] + r
  68.                 if c > 255: c = 255
  69.                 if c < 0: c = 0
  70.                 ncol.append(c)
  71.             screen.set_at((x, y), ncol)
  72.  
  73. def getAdjacents(x, y):
  74.     adjs = [] #left, above, right, below
  75.     for i in range(-1, 2):
  76.         if i != 0:
  77.             t1, t2 = get_tile(x+i, y), get_tile(x, y+i)
  78.             if t1 == None: adjs.append(1)
  79.             else: adjs.append(t1[0])
  80.             if t2 == None: adjs.append(1)
  81.             else: adjs.append(t2[0])
  82.     return adjs
  83.  
  84. def draw(x, y):
  85.     tile = get_tile(x, y)
  86.     if tile != None:
  87.         adjs = getAdjacents(x, y)
  88.         if tile[0] == 0: #floor
  89.             bl, br = get_tile(x-1, y+1), get_tile(x+1, y+1)
  90.             if bl == None: bl = [1]
  91.             if br == None: br = [1]
  92.             if adjs[3] == 0:
  93.                 floor_layer.blit(tiles[0], (x*tilew, y*tileh))
  94.             elif adjs[3] == 1 and ((adjs[0] == 1 and adjs[2] == 1) or (bl[0] == 1 and br[0] == 1)):
  95.                 floor_layer.blit(tiles[16], (x*tilew, y*tileh))
  96.                 wall_layer.blit(tiles[16], (x*tilew, y*tileh
  97.             elif adjs[3] == 1 and adjs[0] == 0 and adjs[2] == 0 and bl[0] == 0 and br[0] == 0:
  98.                 floor_layer.blit(tiles[17], (x*tilew, y*tileh))
  99.             elif adjs[3] == 1 and adjs[0] == 0 and (adjs[2] == 1 or br[0] == 1) and bl[0] == 0:
  100.                 floor_layer.blit(tiles[18], (x*tilew, y*tileh))
  101.             elif adjs[3] == 1 and (adjs[0] == 1 or bl[0] == 1) and adjs[2] == 0 and br[0] == 0:
  102.                 floor_layer.blit(tiles[19], (x*tilew, y*tileh))
  103.             else:
  104.                 floor_layer.blit(tiles[16], (x*tilew, y*tileh))
  105.         else:
  106.             if adjs[0] == 1 and adjs[1] == 1 and adjs[2] == 1 and adjs[3] == 1:
  107.                 ceil_layer.blit(tiles[1], (x*tilew, y*tileh))
  108.             if adjs[0] == 1 and adjs[1] == 1 and adjs[2] == 1 and adjs[3] == 0:
  109.                 wall_layer.blit(tiles[2], (x*tilew, y*tileh))
  110.             if adjs[0] == 0 and adjs[1] == 1 and adjs[2] == 0 and adjs[3] == 0:
  111.                 wall_layer.blit(tiles[3], (x*tilew, y*tileh))
  112.             if adjs[0] == 1 and adjs[1] == 1 and adjs[2] == 0 and adjs[3] == 0:
  113.                 wall_layer.blit(tiles[4], (x*tilew, y*tileh))
  114.             if adjs[0] == 0 and adjs[1] == 1 and adjs[2] == 1 and adjs[3] == 0:
  115.                 wall_layer.blit(tiles[5], (x*tilew, y*tileh))
  116.             if adjs[0] == 0 and adjs[1] == 1 and adjs[2] == 0 and adjs[3] == 1:
  117.                 wall_layer.blit(tiles[6], (x*tilew, y*tileh))
  118.             if adjs[0] == 1 and adjs[1] == 1 and adjs[2] == 0 and adjs[3] == 1:
  119.                 ceil_layer.blit(tiles[7], (x*tilew, y*tileh))
  120.             if adjs[0] == 0 and adjs[1] == 1 and adjs[2] == 1 and adjs[3] == 1:
  121.                 ceil_layer.blit(tiles[8], (x*tilew, y*tileh))
  122.             if adjs[0] == 1 and adjs[1] == 0 and adjs[2] == 1 and adjs[3] == 1:
  123.                 ceil_layer.blit(tiles[9], (x*tilew, y*tileh))
  124.             if adjs[0] == 1 and adjs[1] == 0 and adjs[2] == 1 and adjs[3] == 0:
  125.                 wall_layer.blit(tiles[10], (x*tilew, y*tileh))
  126.             if adjs[0] == 0 and adjs[1] == 0 and adjs[2] == 0 and adjs[3] == 0:
  127.                 wall_layer.blit(tiles[11], (x*tilew, y*tileh))
  128.             if adjs[0] == 1 and adjs[1] == 0 and adjs[2] == 0 and adjs[3] == 0:
  129.                 wall_layer.blit(tiles[12], (x*tilew, y*tileh))
  130.             if adjs[0] == 0 and adjs[1] == 0 and adjs[2] == 1 and adjs[3] == 0:
  131.                 wall_layer.blit(tiles[13], (x*tilew, y*tileh))
  132.             if adjs[0] == 0 and adjs[1] == 0 and adjs[2] == 0 and adjs[3] == 1:
  133.                 ceil_layer.blit(tiles[14], (x*tilew, y*tileh))
  134.             if adjs[0] == 1 and adjs[1] == 0 and adjs[2] == 0 and adjs[3] == 1:
  135.                 ceil_layer.blit(tiles[15], (x*tilew, y*tileh))
  136.             if adjs[0] == 0 and adjs[1] == 0 and adjs[2] == 1 and adjs[3] == 1:
  137.                 ceil_layer.blit(tiles[15], (x*tilew, y*tileh))
  138.                
  139.     else:
  140.         print("non existent tile @ ",x,",",y)
  141. def drawGrid():
  142.     for x in range(gridw):
  143.         for y in range(gridh):
  144.             draw(x, y)
  145.     #addNoise([0, 15, 15, 15])
  146.     pygame.display.flip()
  147. drawGrid()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement