make a python code that opens window and generates a random house floorplan, doors,…
- User
make a python code that opens window and generates a random house floorplan, doors, windows, etc, use PyQt6
- Assistant
make a python code that opens window and generates a random house floorplan, doors, windows, etc, use PyQt6
Certainly! Below is a Python script that uses PyQt6 to create a window and generate a random house floorplan with rooms, doors, and windows. This script creates random rooms and connects them with doors. Windows are added randomly to the walls of the rooms.
import sys
import random
from PyQt6.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsRectItem, QGraphicsLineItem
from PyQt6.QtGui import QPen, QBrush, QPainter
from PyQt6.QtCore import Qt, QRectF
# Constants for room dimensions
ROOM_MIN_SIZE = 100
ROOM_MAX_SIZE = 200
class FloorPlanGenerator(QGraphicsView):
def __init__(self):
super().__init__()
# Create a scene to hold the items
self.scene = QGraphicsScene()
self.setScene(self.scene)
# Generate the floor plan
self.generate_floorplan()
# Set up the view
self.setRenderHint(QPainter.RenderHint.Antialiasing)
self.setWindowTitle('Random House Floorplan Generator')
self.resize(800, 600)
def generate_floorplan(self):
# Generate rooms
rooms = []
num_rooms = random.randint(5, 10)
for _ in range(num_rooms):
width = random.randint(ROOM_MIN_SIZE, ROOM_MAX_SIZE)
height = random.randint(ROOM_MIN_SIZE, ROOM_MAX_SIZE)
x = random.randint(-400, 400 - width)
y = random.randint(-300, 300 - height)
rect = QRectF(x, y, width, height)
room = QGraphicsRectItem(rect)
room.setPen(QPen(Qt.GlobalColor.black, 3))
room.setBrush(QBrush(Qt.GlobalColor.lightGray))
self.scene.addItem(room)
rooms.append(room)
# Add doors between adjacent rooms
for i in range(len(rooms) - 1):
room1 = rooms[i]
room2 = rooms[i + 1]
# Get the center points of each room
center1 = room1.rect().center()
center2 = room2.rect().center()
# Draw a line representing a door between rooms
door = QGraphicsLineItem(center1.x(), center1.y(), center2.x(), center2.y())
pen = QPen(Qt.GlobalColor.brown, 5)
door.setPen(pen)
self.scene.addItem(door)
# Add windows to the outer walls of each room
for room in rooms:
rect = room.rect()
window_length = 40
window_pen = QPen(Qt.GlobalColor.blue, 5)
# Top wall
if random.choice([True, False]):
x1 = rect.left() + rect.width() / 3
x2 = x1 + window_length
y = rect.top()
window = QGraphicsLineItem(x1, y, x2, y)
window.setPen(window_pen)
self.scene.addItem(window)
# Bottom wall
if random.choice([True, False]):
x1 = rect.left() + rect.width() / 3
x2 = x1 + window_length
y = rect.bottom()
window = QGraphicsLineItem(x1, y, x2, y)
window.setPen(window_pen)
self.scene.addItem(window)
# Left wall
if random.choice([True, False]):
y1 = rect.top() + rect.height() / 3
y2 = y1 + window_length
x = rect.left()
window = QGraphicsLineItem(x, y1, x, y2)
window.setPen(window_pen)
self.scene.addItem(window)
# Right wall
if random.choice([True, False]):
y1 = rect.top() + rect.height() / 3
y2 = y1 + window_length
x = rect.right()
window = QGraphicsLineItem(x, y1, x, y2)
window.setPen(window_pen)
self.scene.addItem(window)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = FloorPlanGenerator()
window.show()
sys.exit(app.exec())
Explanation:
Imports:
sys and random modules are used for system functions and generating random numbers.Constants:
ROOM_MIN_SIZE and ROOM_MAX_SIZE define the minimum and maximum sizes of the rooms.FloorPlanGenerator Class:
QGraphicsView.generate_floorplan() to build the floorplan.generate_floorplan Method:
window_length) drawn on the walls.Main Execution:
QApplication.FloorPlanGenerator and displays the window.Note: This is a simplified example to demonstrate how you can create a random floorplan using PyQt6. Rooms are placed randomly and might overlap. The doors and windows are added for visual representation but might not perfectly align with the walls in a real architectural plan.
Requirements:
Install PyQt6 if you haven't already:
pip install PyQt6
Running the Script:
Save the script to a file, say random_floorplan.py, and run it using:
python random_floorplan.py
Screenshot:
(As I cannot provide an actual screenshot, running the script will display a window similar to the one described.)
Feel free to modify the code to suit more specific requirements, such as preventing room overlaps or adding more detailed architectural elements.
Not yet included in a published release.