我已经编写了一些代码来绘制随机定位的点,尽可能多的,并让它们像流体一样传播一段时间。我也希望他们不要互相接触。运行它没有问题,但在某些情况下,完成一个点的传播需要很长时间,所以我意识到这不是那么有效。
我很乐意得到一些帮助以提高效率。
from graphics import *
import time, random
racepoints = {} # {0:[(),()],1:[(),()]}
allpoints = []
races = {}
tx, ty = int(input("x=")), int(input("y="))
def Window():
global win, sure, spreadSize
win = GraphWin("Pencere", tx, ty)
starttime = time.time()
Start()
spreadSize = int(input("Başlangıç boyutu?"))
Spread(spreadSize)
finishtime = time.time()
sure = (finishtime - starttime)
writeTime()
print("Bitti! Ve {} saniye sürdü!".format(sure))
time.sleep(5)
def writeTime():
timefile = open("C:\\Python36\\timefile.py", "r")
gotta_rewrite = timefile.readlines()
timefile.close()
timefile = open("C:\\Python36\\timefile.py", "w")
gotta_rewrite.append("\n{} ırk, {} genişlik, {}*{} alan, {} saniye sürdü.".format(racecount, spreadSize, tx, ty, sure))
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 = 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))
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])
if (lx + ax, ly + ay) not in allpoints:
lx += ax
ly += ay
newpoint = Point(lx, ly)
newpoint.setFill(races[i])
newpoint.draw(win)
(racepoints[i]).append((lx, ly))
allpoints.append((lx, ly))
else:
pass
maxsize -= 1
Window()