Advertisement
Karp007

Terrain.py

Oct 23rd, 2022 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | Source Code | 0 0
  1. import pygame, random
  2. class Terrain:
  3.     def __init__(self):
  4.         #Simple variables
  5.         self.GRAVITY = 1
  6.         #create numberical map - I disabled it because I am doing chunk generated map
  7.         #f = open('map.txt' ,'r')
  8.         #data = f.read()
  9.         #f.close()
  10.         #data = data.split('\n')
  11.         #self.num_map = []
  12.         #for row in data:
  13.         #    self.num_map.append(list(row))
  14.         #create list that'll store generated world
  15.         self.game_map = {}
  16.         self.tiles_type = {1 : (26, 130, 1), 2 : (89, 42, 2), 3 : (255, 228, 0)}
  17.     def generate_chunk(self, x, y):
  18.         chunk_data = []
  19.         for y_pos in range(8):
  20.             for x_pos in range(8):
  21.                 target_x = x * 8 + x_pos
  22.                 target_y = y * 8 + y_pos
  23.                 tile_type = 0 # nothing
  24.                 if target_y > 10:
  25.                     tile_type = 2 # dirt
  26.                 elif target_y == 10:
  27.                     tile_type = 1 # grass
  28.                 elif target_y == 9:
  29.                     if random.randint(1,5) == 1:
  30.                         tile_type = 3 # plant
  31.                 if tile_type != 0:
  32.                     chunk_data.append([[target_x,target_y],tile_type])
  33.         return chunk_data
  34.     def draw_map(self, scroll, win):
  35.         tile_rects = []
  36.         coins = []
  37.         for y in range(5):
  38.             for x in range(5):
  39.                 target_x = x - 1 + int(round(scroll[0]/(8*32)))
  40.                 target_y = y - 1 + int(round(scroll[1]/(8*32)))
  41.                 target_chunk = str(target_x) + ';' + str(target_y)
  42.                 if target_chunk not in self.game_map:
  43.                     self.game_map[target_chunk] = self.generate_chunk(target_x, target_y)
  44.                 for tile in self.game_map[target_chunk]:
  45.                     pygame.draw.rect(win, self.tiles_type[tile[1]], (tile[0][0]*32-scroll[0],tile[0][1]*32-scroll[1], 32, 32))
  46.                     if tile[1] in [1,2]:
  47.                         tile_rects.append(pygame.Rect(tile[0][0]*32,tile[0][1]*32,32,32))  
  48.                     elif tile[1] in [3]:
  49.                         index = self.game_map[target_chunk].index(tile)
  50.                         coins.append([pygame.Rect(tile[0][0]*32,tile[0][1]*32,32,32), target_chunk, index])
  51.         return self.game_map, tile_rects, coins
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement