-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.py
63 lines (48 loc) · 1.5 KB
/
star.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import random
mapx = 50 # 地图宽度(百光年)
mapy = 50 # 地图长度(百光年)
density = 2 # 密度,平均星球间隔距离(百光年)
starnum = int(mapx * mapy / (density * density))
def pos():
posx = random.randint(-mapx, mapx)
posy = random.randint(-mapy, mapy)
return posx, posy
def starmap():
map = []
for i in range(starnum):
map.append(pos())
return map
maplist = [] #pos是位置,owner是占领文明
for i in range(starnum):
map_i = {'pos': starmap()[i], 'owner': None}
maplist.append(map_i)
pass
def distance(star1, star2): #获取两个星系的距离
x1 = maplist[star1]['pos'][0]
y1 = maplist[star1]['pos'][1]
x2 = maplist[star2]['pos'][0]
y2 = maplist[star2]['pos'][1]
dist = int(((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5)
return dist
# distlist = []
# for i in range(starnum):
# for t in range(i, starnum):
# dist = {}
# dist['stars'] = {i, t}
# dist['dist']= distance(i,t)
# distlist.append(dist)
# pass
# pass
def distto(starid, starlist = list(range(starnum))): #获取starlist中各个星系到starid星系的距离,按照距离从小到大排序,(星系id,距离)
distolist = {}
l = starlist
if starid in l:
l.remove(starid)
for i in l:
distolist[i]=distance(i,starid)
pass
distolist = sorted(distolist.items(), key=lambda d:d[1])
return distolist
# print(distto(10))
# print(maplist)
# print(distlist)