减少方向码

时间:2018-08-03 15:45:16

标签: python numpy pygame python-3.7

每次编写具有方向性的代码时,我总是必须制作4个不同的代码实例,每个方向一个实例,这使代码难以维护和开发。下面的示例是一个示例,我想知道是否可以将其简化为1个实例。每个实例都非常相似,只有很小的差异,如何将它们全部组合为一个?

#This is a simcity sort of remake where everything is split into tiles(48x48)

#I am storing the grid in a 2d numpy array, called grid
#This triggers on mouse release
#startX amd #startY are where you clicked the mouse down
coords = pygame.mouse.get_pos()
finishX = int(coords[0]/TILESIZE)
finishY = int(coords[1]/TILESIZE)
try:
    if abs(startX - finishX) < abs(startY - finishY): #If it is verticle rather then horizontal
        if startY > finishY: #If it's going up

            for i in range(abs(startY - finishY+1)): #Loop through entire length
                if grid[startX, startY-i] == 'g': #If the tile is grass/free (For plots)
                    if tileType == 'r' or tileType == 'c' or tileType == 'i': #If it's a plot
                        if grid[startX-1, startY-i] == 't' or grid[startX, startY-i-1] == 't' or grid[startX+1, startY-i] == 't' or grid[startX, startY-i+1] == 't': #If it has a neighbouring road                                                grid[startX, startY-i] = tileType
                    elif tileType == 't': #If it's a road
                        if cash >= roadCost: #If you can afford a road
                            cash -= roadCost #Decrease cash by said road cost
                            grid[startX, startY-i] = tileType #Set that tile to the new one
                        else:
                            text = "Not enough money to build more road!" # This sets it for the next function
                            displayText(window, text) #Will display text at the bottom of the screen
                else:
                    if tileType == 'g': #If it's the demolish tool, setting the tile to grass
                        if grid[startX, startY-i] == 't':
                            cash += roadCost/2 #Increase Cash by the cost of a road/2
                        grid[startX, startY-i] = 'g' #Set the ground to grass

        else: #If it's going down
            for i in range(abs(startY - finishY)+1):
                if grid[startX, startY+i] == 'g':
                    if tileType == 'r' or tileType == 'c' or tileType == 'i':
                        if grid[startX-1, startY+i] == 't' or grid[startX, startY+i-1] == 't' or grid[startX+1, startY+i] == 't' or grid[startX, startY+i+1] == 't':
                             grid[startX, startY+i] = tileType
                    elif tileType == 't':
                        if cash >= roadCost:
                            cash -= roadCost
                            grid[startX, startY+i] = tileType
                            roadMap[startX, startY+i] = 1
                        else:
                            text = "Not enough money to build more road!"
                            displayText(window, text)
                else:
                    if tileType == 'g':
                        if grid[startX, startY-i] == 't':
                            cash += roadCost/2
                            roadMap[startX-i, startY] = 0
                            roadMap[startX, startY+i] = 0
                        grid[startX, startY+i] = 'g'

    else: #If it's horizontal
        if startX > finishX: #If it's going left
            for i in range(abs(startX - finishX)+1):
                if grid[startX-i, startY] == 'g':
                    if tileType == 'r' or tileType == 'c' or tileType == 'i':
                        if grid[startX-i-1, startY] == 't' or grid[startX-i, startY-1] == 't' or grid[startX-i+1, startY] == 't' or grid[startX-i, startY+1] == 't':
                            grid[startX-i, startY] = tileType
                    elif tileType == 't':
                        if cash > roadCost:
                            cash -= roadCost
                            grid[startX-i, startY] = tileType
                            roadMap[startX-i, startY] = 1
                        else:
                            text = "Not enough money to build more road!"
                            displayText(window, text)
                else:
                    if tileType == 'g':
                        if grid[startX-i, startY] == 't':
                            cash += roadCost/2
                        grid[startX-i, startY] = 'g'

        else: #If it's going right
            for i in range(abs(startX - finishX)+1):
                if grid[startX+i, startY] == 'g':
                    if tileType == 'r' or tileType == 'c' or tileType == 'i':
                        if grid[startX+i-1, startY] == 't' or grid[startX+i, startY-1] == 't' or grid[startX+i+1, startY] == 't' or grid[startX+i, startY+1] == 't':
                            grid[startX+i, startY] = tileType
                    elif tileType == 't':
                        if cash >= roadCost:
                            cash -= roadCost
                            grid[startX+i, startY] = tileType
                            roadMap[startX+i, startY] = 1
                        else:
                            text = "Not enough money to build more road!"
                            displayText(window, text)
                else:
                    if tileType == 'g':
                        if grid[startX, startY-i] == 't':
                            cash += roadCost/2
                            roadMap[startX+i, startY] = 0
                        grid[startX+i, startY] = 'g'



except:
    pass
startX = None
startY = None
finishX = None
finishY = None

0 个答案:

没有答案