Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, random
- class Terrain:
- def __init__(self):
- #Simple variables
- self.GRAVITY = 1
- #create numberical map - I disabled it because I am doing chunk generated map
- #f = open('map.txt' ,'r')
- #data = f.read()
- #f.close()
- #data = data.split('\n')
- #self.num_map = []
- #for row in data:
- # self.num_map.append(list(row))
- #create list that'll store generated world
- self.game_map = {}
- self.tiles_type = {1 : (26, 130, 1), 2 : (89, 42, 2), 3 : (255, 228, 0)}
- def generate_chunk(self, x, y):
- chunk_data = []
- for y_pos in range(8):
- for x_pos in range(8):
- target_x = x * 8 + x_pos
- target_y = y * 8 + y_pos
- tile_type = 0 # nothing
- if target_y > 10:
- tile_type = 2 # dirt
- elif target_y == 10:
- tile_type = 1 # grass
- elif target_y == 9:
- if random.randint(1,5) == 1:
- tile_type = 3 # plant
- if tile_type != 0:
- chunk_data.append([[target_x,target_y],tile_type])
- return chunk_data
- def draw_map(self, scroll, win):
- tile_rects = []
- coins = []
- for y in range(5):
- for x in range(5):
- target_x = x - 1 + int(round(scroll[0]/(8*32)))
- target_y = y - 1 + int(round(scroll[1]/(8*32)))
- target_chunk = str(target_x) + ';' + str(target_y)
- if target_chunk not in self.game_map:
- self.game_map[target_chunk] = self.generate_chunk(target_x, target_y)
- for tile in self.game_map[target_chunk]:
- pygame.draw.rect(win, self.tiles_type[tile[1]], (tile[0][0]*32-scroll[0],tile[0][1]*32-scroll[1], 32, 32))
- if tile[1] in [1,2]:
- tile_rects.append(pygame.Rect(tile[0][0]*32,tile[0][1]*32,32,32))
- elif tile[1] in [3]:
- index = self.game_map[target_chunk].index(tile)
- coins.append([pygame.Rect(tile[0][0]*32,tile[0][1]*32,32,32), target_chunk, index])
- return self.game_map, tile_rects, coins
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement