Advertisement
Ridz112

terrain generator

Jan 14th, 2022
1,418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. from pygame import *
  2. import random
  3. init()
  4.  
  5. width = 1000
  6.  
  7. height = 1000
  8.  
  9. screen = display.set_mode((width, height))
  10.  
  11. clock = time.Clock()
  12.  
  13. # ------------------------------------------------------------------------------------------
  14. class map():
  15.     def __init__(self):
  16.         self.map = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  17.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  18.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  19.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  20.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  21.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  22.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  23.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  24.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  25.                     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
  26.  
  27.  
  28.         self.dirtImg = image.load("dirt.jpg")
  29.  
  30.         self.dirtImg = transform.scale(self.dirtImg, (100, 100))
  31.  
  32.         self.goldImg = image.load("gold.png")
  33.  
  34.         self.goldImg = transform.scale(self.goldImg, (100, 100))
  35.  
  36.         self.gold = random.randint(1,10)
  37.  
  38.     def createterrain(self):
  39.         posx = 0
  40.         posy = 0
  41.         while posx < 10 and posy < 10:
  42.             self.map[posx][posy] = "D"
  43.             posx = posx + 1
  44.             while posx == 10:
  45.                 posx = 0
  46.                 posy = posy + 1
  47.  
  48.         posx = 0
  49.         posy = 0
  50.  
  51.         while posx < 10 and posy < 10:
  52.             if self.gold == 1:
  53.                 self.map[posx][posy] = "G"
  54.             self.gold = random.randint(1, 5)
  55.             posx = posx + 1
  56.             while posx == 10:
  57.                 posx = 0
  58.                 posy = posy + 1
  59.  
  60.         return self.map
  61.  
  62.     def display(self,screen):
  63.         posx = 0
  64.         posy = 0
  65.         self.x = 0
  66.         self.y = 100
  67.         while posx < 10 and posy < 10:
  68.             if self.map[posy][posx] == "D":
  69.                 screen.blit(self.dirtImg,(self.x,self.y))
  70.             if self.map[posy][posx] == "G":
  71.                 screen.blit(self.goldImg,(self.x,self.y))
  72.             posx = posx + 1
  73.             self.x = self.x + 100
  74.  
  75.             while posx == 10:
  76.                 posx = 0
  77.                 self.x = 0
  78.                 self.y = self.y + 100
  79.                 posy = posy + 1
  80.  
  81.  
  82. screen.fill((0, 0, 0))
  83. world = map()
  84. world.createterrain()
  85. world.display(screen)
  86.  
  87.  
  88. game = True
  89. while game == True:
  90.  
  91.     for e in event.get():
  92.         if e.type == constants.QUIT: # to quit game (close tab)
  93.             game = False
  94.  
  95.         if e.type == KEYDOWN:
  96.  
  97.             if e.key == K_RIGHT:
  98.                 world.createterrain()
  99.                 world.display(screen)
  100.  
  101.     display.flip()
  102.  
  103.  
  104.     clock.tick(60)
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement