# author : Radek Z. and troszke Kuba S. (akustyczny kolega)
import pygame
import random
pygame.init()
class GenDungeon() :
def __init__(self , max_plate) :
self.max_plate = max_plate
self.map = []
self.room_type = {
0 : "empty_room" ,
1 : "start_room" ,
2 : "end_room" ,
3 : "enemy_room" ,
4 : "boss_room" ,
5 : "chest_room"
}
def gen_map(self ) :
self.gen_clear_map()
self.map[self.max_plate // 2][self.max_plate // 2] = 1
enemy_room = random.randint(4, 6)
chest_room = random.randint(2, 5)
boss_room = random.random()
if boss_room < 0.2:
boss_room = 1
else:
boss_room = 0
for _ in range(enemy_room):
self.gen_room(3)
print("enemy room")
for _ in range(chest_room):
self.gen_room(5)
print("chest room")
for _ in range(boss_room):
self.gen_room(4)
print("boss room")
self.gen_room(2)
def gen_clear_map(self) :
self.map = []
for x in range(self.max_plate) :
column = []
for y in range(self.max_plate) :
column.append(0)
self.map.append(column)
def gen_room(self, room_type):
while True:
x = random.randint(0 , self.max_plate-1)
y = random.randint(0 , self.max_plate-1)
somsiad = 0
start_room = False
cord = (
(x + 1 , y),
(x - 1 , y),
(x , y + 1),
(x , y - 1)
)
for h in cord:
try:
cx , cy = h
if self.map[cx][cy] in (1 ,2 , 3 , 4 , 5):
somsiad += 1
if self.map[cx][cy] == 1:
start_room = True
except:
pass
if (room_type == 2) or (room_type == 4) or (room_type == 5):
if (somsiad > 0 and somsiad < 4 and not start_room) and self.map[x][y] == 0:
self.map[x][y] = room_type
break
else:
if (somsiad > 0 and somsiad < 4) and self.map[x][y] == 0:
self.map[x][y] = room_type
break
self.gen_door()
self.update_door()
def gen_door(self):
self.door = []
for x in range(self.max_plate):
column = []
for y in range(self.max_plate):
column.append([False , False , False , False])
self.door.append(column)
for x in range(self.max_plate):
for y in range(self.max_plate):
if self.map[x][y] == 1:
self.door[x][y]
cord = (
(x + 1 , y),
(x , y + 1),
(x , y - 1),
(x - 1 , y)
)
for c in cord:
sx , sy = c
if self.map[sx][sy] != 0:
if sy == y-1:
self.door[x][y][0] = True
elif sy == y+1:
self.door[x][y][1] = True
elif sx == x+1:
self.door[x][y][2] = True
elif sx == x-1:
self.door[x][y][3] = True
self.update_door()
for _ in range(6):
for x in range(self.max_plate):
for y in range(self.max_plate):
if (self.door[x][y] == [False , False , False , False]) and (self.map[x][y] > 0):
try:
if True in self.door[x][y-1]:
self.door[x][y][0] = True
except:
pass
try:
if True in self.door[x][y+1]:
self.door[x][y][1] = True
except:
pass
try:
if True in self.door[x-1][y]:
self.door[x][y][3] = True
except:
pass
try:
if True in self.door[x+1][y]:
self.door[x][y][2] = True
except:
pass
self.update_door()
def update_door(self):
for x in range(self.max_plate):
for y in range(self.max_plate):
try:
if self.door[x][y-1][1] == True:
self.door[x][y][0] = True
except:
pass
try:
if self.door[x][y+1][0] == True:
self.door[x][y][1] = True
except:
pass
try:
if self.door[x+1][y][3] == True:
self.door[x][y][2] = True
except:
pass
try:
if self.door[x-1][y][2] == True:
self.door[x][y][3] = True
except:
pass
"""
- zamienić potem try except na lepsze sprawdzanie wyjścia poza mapę
"""
class Display():
def print_room(self , room , cord):
x , y = cord
cords = (
x * 50 ,
y * 50 ,
50 ,
50
)
match room:
case 0 :
color = "grey"
case 1 :
color = (255 , 175 , 10)
case 2 :
color = (0 ,200 , 0)
case 3 :
color = (255 , 0 , 0)
case 4 :
color = (255 , 255 , 0)
case 5 :
color = (0 , 0 , 255)
case None :
color = "black"
pygame.draw.rect(screen , color , cords)
def print_door(self , cord):
x , y , pos = cord
if pos[0]:
cords = (
x * 50 + 20 ,
y * 50 ,
10 ,
10
)
pygame.draw.rect(screen , "green" , cords)
if pos[1]:
cords = (
x * 50 + 20 ,
y * 50 + 40 ,
10 ,
10
)
pygame.draw.rect(screen , "green" , cords)
if pos[2]:
cords = (
x * 50 + 40 ,
y * 50 + 20,
10 ,
10
)
pygame.draw.rect(screen , "green" , cords)
if pos[3]:
cords = (
x * 50 ,
y * 50 + 20 ,
10 ,
10
)
pygame.draw.rect(screen , "green" , cords)
def print_map(self , g) :
plate = g.map
for x in range(g.max_plate ) :
for y in range(g.max_plate) :
self.print_room(plate[x][y] , (x , y) )
for x in range(g.max_plate):
for y in range(g.max_plate):
self.print_door((x , y , g.door[x][y]))
def main() :
global screen
sze = 50 * 11
screen = pygame.display.set_mode((sze , sze))
pygame.display.set_caption("dungeony")
g = GenDungeon(11)
d = Display()
g.gen_map()
while True :
for event in pygame.event.get() :
if event.type == pygame.QUIT :
quit()
screen.fill("black")
d.print_map(g)
pygame.display.update()
if __name__ == "__main__" :
main()
#jakby co to python wersja 3.13.9