mardi 5 mars 2019

Python: I can't come up with the right code to generate streets for a town

I am currently making a town generator. I am mostly making this just for fun. One problem I have that I have been trying to fix for hours is how should I make the program generate streets. Here is all the code that I have written that has to do with this.

#Import#
from random import *

#Functions#
#Generate town
def generateTown(size):
    #Generate town layout#
    #Set town list
    town = [[["-","",""] for b in range(size*10)] for i in range(size*10)]

    #Generate where the center will be
    temp = ["top","bottom","left","right","center"]
    centerLocation = temp[randint(0,len(temp)-1)]

    #Set center variable
    if centerLocation == "top":
        #Set center variable
        center = [int((size*10)/-1.3),int((size*10)/2)]
    elif centerLocation == "bottom":
        #Set center variable
        center = [int((size*10)/1.3),int((size*10)/2)]
    elif centerLocation == "left":
        #Set center variable
        center = [int((size*10)/2),int((size*10)/-1.3)]
    elif centerLocation == "right":
        #Set center variable
        center = [int((size*10)/2),int((size*10)/1.3)]
    elif centerLocation == "center":
        #Set center variable
        center = [int((size*10)/2),int((size*10)/2)]

    #Place streets
    #Loop 4 times for all four corners
    for i in range(4):
        #Loop through the size of the town
        for b in range(size*10):
            #Make sure that if the for loop goes out of the boundaries of the town nothing crashes
            try:
                #Place street depending on what corner we are currently working on 
                if i == 0:
                    #Set street
                    town[center[0]][center[1]-b][0] = "street"
                elif i == 1:
                    #Set street
                    town[center[0]][center[1]+b][0] = "street"
                elif i == 2:
                    #Set street
                    town[center[0]-b][center[1]][0] = "street"
                elif i == 3:
                    #Set street
                    town[center[0]+b][center[1]][0] = "street"
            except:
                break

    #Place center
    town[center[0]][center[1]][0] = "center"

    #Print town just for testing
    print(name)
    for i in range(len(town)):
        line = ""
        for b in range(len(town[i])):
            temp = list(town[i][b][0])
            line += temp[0]
        print(line)

#Run
generateTown(3)
input()

Right after it places the starting streets but before it actually places the center space, I have been putting things that will generate more streets. But the problem with the stuff I have been trying is that sometimes for some reason it bleeds over. Meaning, if it is setting a street and the street leaves the boundaries of the town, it just goes to the opposite end, like Pac-man. I have no idea why this happens. And also sometimes it creates streets that are right next to each other, and I want at least a one gap in-between. All the stuff I have been trying don't work and I don't know why. The code that has been sent here all works fine, try it out yourself. Please help me!




Aucun commentaire:

Enregistrer un commentaire