Untitled

 avatar
unknown
python
a year ago
5.1 kB
3
Indexable
import tkinter as tk
import random

# Generates the 2 menu buttons
class Menu:
    def __init__(self, master, dungeon):
        self.master = master
        self.dungeon = dungeon

        self.frame = tk.Frame(master)
        self.frame.pack()

        self.start_button = tk.Button(self.frame, text="Generate", command=self.start_program)
        self.start_button.pack(side=tk.LEFT)

        self.exit_button = tk.Button(self.frame, text="EXIT", command=self.master.destroy)
        self.exit_button.pack(side=tk.LEFT)

    # Starts the program
    def start_program(self):
        self.dungeon.generate_dungeon()

class DungeonGenerator:
    def __init__(self, master, width, height, cell_size):
        self.master = master
        self.width = width
        self.height = height
        self.cell_size = cell_size
        self.cells = []

        self.canvas = tk.Canvas(master, width=width*cell_size, height=height*cell_size, bg='white')
        self.canvas.pack()

    def generate_dungeon(self):
        # Clears previous dungeon
        self.canvas.delete("all")
        self.cells = []

        # Initializes new dungeon with clean canvas
        for y in range(self.height):
            row = []
            for x in range(self.width):
                cell = self.canvas.create_rectangle(x*self.cell_size, y*self.cell_size,
                                                    (x+1)*self.cell_size, (y+1)*self.cell_size,
                                                    fill='white', outline='gray')
                row.append(cell)
            self.cells.append(row)

        # Generates rooms
        num_rooms = random.randint(4, 20)  # Generates random amount of rooms
        rooms = []
        for i in range(num_rooms):
            room_width = random.randint(4, 20)  # Dictates room width
            room_height = random.randint(4, 20)  # Dictates room height
            x = random.randint(0, self.width - room_width - 1)
            y = random.randint(0, self.height - room_height - 1)

            # Checks if rooms overlap with each other
            if self.check_room_overlap(x, y, room_width, room_height, rooms):
                continue
            
            # Creates room and creates it's coordinates
            self.create_room(x, y, room_width, room_height)
            rooms.append((x, y, room_width, room_height))

        # Connects rooms with corridors
        connected = set()  
        for i, room1 in enumerate(rooms):
            if len(connected) > 0:
                room2_index = random.choice(list(connected))
                self.connect_rooms(room1, rooms[room2_index])
                connected.add(i)
                connected.add(room2_index)
            else:
                connected.add(0)  # connects the rooms


    def check_room_overlap(self, x, y, width, height, rooms):
        for room in rooms:
            rx, ry, rwidth, rheight = room
            if (x < rx + rwidth + 2 and x + width + 2 > rx and
                y < ry + rheight + 2 and y + height + 2 > ry):
                return True
        return False

    def create_room(self, x, y, width, height):
        for i in range(x, x + width):
            for j in range(y, y + height):
                self.canvas.itemconfig(self.cells[j][i], fill='#F71212')  # Red for rooms

    def connect_rooms(self, room1, room2):
        start_x, start_y, room1_width, room1_height = room1
        end_x, end_y, room2_width, room2_height = room2

        # Randomly choose starting point in room 1 and ending point in room 2
        start_point = (random.randint(start_x + room1_width // 4, start_x + 3 * room1_width // 4),
                       random.randint(start_y + room1_height // 4, start_y + 3 * room1_height // 4))
        end_point = (random.randint(end_x + room2_width // 4, end_x + 3 * room2_width // 4),
                     random.randint(end_y + room2_height // 4, end_y + 3 * room2_height // 4))

        # Creates a corridor between the rooms
        self.draw_corridor(start_point, end_point)

    def draw_corridor(self, start_point, end_point):
        x1, y1 = start_point
        x2, y2 = end_point

        # Determine direction of corridor
        dx = 1 if x1 < x2 else -1
        dy = 1 if y1 < y2 else -1

        # Draws corridor
        while x1 != x2: #for X values
            self.canvas.itemconfig(self.cells[y1][x1], fill='#1275F7')  # Light gray for corridors
            x1 += dx
        while y1 != y2: #for values
            self.canvas.itemconfig(self.cells[y1][x1], fill='#1275F7')  # Light gray for corridors
            y1 += dy

def main():
    root = tk.Tk() #tkinter interface
    root.title("Random Dungeon Generator")
    dungeon_width = 80
    dungeon_height = 60
    cell_size = 10
    dungeon = DungeonGenerator(root, dungeon_width, dungeon_height, cell_size)
    menu = Menu(root, dungeon)  
    root.mainloop()

if __name__ == "__main__":
    main()
Leave a Comment