Я немного изменил код, так что теперь, если общее количество нарисованных пикселей больше пустых, оно будет проверять координаты нового пикселя на пустых пикселях. Поскольку пустые пиксели (которые готовы к окраске) становятся все меньше и меньше, становится проще и быстрее проверить, можно ли нарисовать новый пиксель. 200 гонок, 25 spreadSize, x = 200, y = 200 было завершено за 512,4622814655304 секунды до его изменения, теперь это 384,0333812236786 секунд.
Новая версия:
from graphics import *
import time, random, io
date = "{}.{}.{}".format(time.strftime("%d"),time.strftime("%m"),time.strftime("%Y"))
racepoints = {} # {0:[(),()],1:[(),()]}
allpoints = []
space = []
races = {}
tx, ty = int(input("x=")), int(input("y="))
for i in range(tx+1):
for a in range(ty+1):
space.append((i, a))
def Window():
global win, sure, spreadSize
win = GraphWin("Pencere", tx, ty)
Start()
spreadSize = int(input("Başlangıç boyutu?"))
starttime = time.time()
Spread(spreadSize)
finishtime = time.time()
sure = (finishtime - starttime)
writeTime()
print("Bitti! Ve {} saniye sürdü!".format(sure))
time.sleep(5)
def writeTime():
with io.open("C:\\Python36\\timefile.py", "r", encoding="utf-8") as timefile:
gotta_rewrite = timefile.readlines()
timefile.close()
gotta_rewrite.append("\n{} ırk, {} genişlik, {}*{} alan, {} saniye sürdü. {}".format(racecount, spreadSize, tx, ty, sure, date))
with io.open("C:\\Python36\\timefile.py", "w", encoding="utf-8") as timefile:
timefile.seek(0)
timefile.writelines(gotta_rewrite)
timefile.close()
def Start():
global racecount
racecount = int(input("Kaç tane ırk olsun?"))
for i in range(racecount):
randomcolor = color_rgb(random.randrange(255), random.randrange(255), random.randrange(255))
races[i] = randomcolor
racepoints[i] = []
nx, ny = 0, 0
while (nx, ny) == (0,0) or (nx,ny) in allpoints:
nx = random.randrange(tx)
ny = random.randrange(ty)
randomstartpoint = Point(nx, ny)
randomstartpoint.setFill(races[i])
randomstartpoint.draw(win)
allpoints.append((nx, ny))
(racepoints[i]).append((nx, ny))
space.remove((nx, ny))
def Spread(maxsize):
defaultsize = maxsize
for i in range(racecount):
maxsize = defaultsize
while maxsize > 0:
for point in list(racepoints[i]):
lx, ly = point
ax, ay = 0, 0
while ax == 0 and ay == 0:
ax = random.choice([-1, 0, 1])
ay = random.choice([-1, 0, 1])
lx += ax
ly += ay
if len(space) > len(allpoints) and (lx, ly) not in allpoints and lx in range(tx) and ly in range(ty):
newpoint = Point(lx, ly)
newpoint.setFill(races[i])
newpoint.draw(win)
racepoints[i].append((lx, ly))
allpoints.append((lx, ly))
space.remove((lx, ly))
elif len(allpoints) > len(space) and (lx, ly) in space and lx in range(tx) and ly in range(ty):
newpoint = Point(lx, ly)
newpoint.setFill(races[i])
newpoint.draw(win)
racepoints[i].append((lx, ly))
space.remove((lx, ly))
else:
pass
maxsize -= 1
Window()