Advertisement
Ridz112

Dungeon Generation

Nov 22nd, 2021
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pygame import *
  2.  
  3. init()
  4. import random
  5.  
  6. class dungeon():
  7.     def __init__(self): #
  8.         self.map = [[0,0,0,0,0],
  9.                     [0,0,0,0,0],
  10.                     [0,0,0,0,0],
  11.                     [0,0,0,0,0],
  12.                     [0,0,0,0,0]]
  13.  
  14.         self.spawnpointx = random.randint(0,2)
  15.         self.spawnpointy = random.randint(0, 2)
  16.         self.playerlocationx = self.spawnpointx
  17.         self.playerlocationy = self.spawnpointy
  18.         self.enemy_room = random.randint(1,2)
  19.  
  20.  
  21.  
  22.     def generatemap(self):
  23.         posx = 0
  24.         posy = 0
  25.         while posx < 5 and posy < 5:
  26.             self.map[posx][posy] = "R"
  27.             posx = posx + 1
  28.             while posx == 5:
  29.                 posx = 0
  30.                 posy = posy + 1
  31.  
  32.         posx = 0
  33.         posy = 0
  34.  
  35.         while posx < 5 and posy < 5:
  36.             if self.enemy_room == 2:
  37.                 self.map[posx][posy] = "E"
  38.             self.enemy_room = random.randint(1, 2)
  39.             posx = posx + 1
  40.             while posx == 5:
  41.                 posx = 0
  42.                 posy = posy + 1
  43.  
  44.         self.map[self.spawnpointy][self.spawnpointx] = "S"
  45.  
  46.         return self.map
  47.  
  48.     def display(self):
  49.         i = 0
  50.         while i < len(self.map):
  51.             print(self.map[i])
  52.             i += 1
  53.  
  54.     def findlocation(self):
  55.         if Dungeon.playerlocationx < 0:
  56.             Dungeon.playerlocationx = 0
  57.             print("No room there")
  58.         if Dungeon.playerlocationx > 4:
  59.             Dungeon.playerlocationx = 4
  60.             print("No room there")
  61.  
  62.         if Dungeon.playerlocationy < 0:
  63.             Dungeon.playerlocationy = 0
  64.             print("No room there")
  65.  
  66.         if Dungeon.playerlocationy > 4:
  67.             Dungeon.playerlocationy = 4
  68.             print("No room there")
  69.  
  70.         print("You are in the coordinates:","(",self.playerlocationx,",",self.playerlocationy,")","Your are in:",self.map[self.playerlocationy][self.playerlocationx])
  71.  
  72.         self.display()
  73.         print("")
  74.  
  75.  
  76.  
  77.     def moveright(self):
  78.         print("You have moved right")
  79.         self.playerlocationx += 1
  80.         self.findlocation()
  81.  
  82.  
  83.     def moveleft(self):
  84.         print("You have moved left")
  85.         self.playerlocationx -= 1
  86.         self.findlocation()
  87.  
  88.     def moveup(self):
  89.         print("You have moved up")
  90.         self.playerlocationy -= 1
  91.         self.findlocation()
  92.  
  93.     def movedown(self):
  94.         print("You have moved down")
  95.         self.playerlocationy += 1
  96.         self.findlocation()
  97.  
  98.  
  99.  
  100.  
  101. Dungeon = dungeon()
  102. print("Note - the starting indexes is 0")
  103.  
  104. Dungeon.generatemap()
  105. Dungeon.display()
  106. while True:
  107.  
  108.     for e in event.get():
  109.         if e.type == constants.QUIT: # to quit game (close tab)
  110.             pass
  111.  
  112.  
  113.  
  114.         if e.type == KEYDOWN:
  115.  
  116.             if e.key == K_RIGHT:
  117.                 Dungeon.moveright()
  118.  
  119.             if e.key == K_LEFT:
  120.                 Dungeon.moveleft()
  121.  
  122.             if e.key == K_UP:
  123.                 Dungeon.moveup()
  124.  
  125.             if e.key == K_DOWN:
  126.                 Dungeon.movedown()
  127.  
  128.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement