按照妳的要求,遍歷貪吃蛇的坐標位置,並和隨機生成的坐標對比,如果相同就重新取值,不相同的時候輸出的Python程序如下
import?randommap_width=30
map_height=30
a?=?[{'x':?2,?'y':?4},?{'x':?5,?'y':?6},?{'x':?7,?'y':?8},?{'x':?4,?'y':?4}]
random_x=random.randint(3,map_width-8)
random_y=random.randint(3,map_height-8)
flag=True
for?i?in?range(len(a)):
if?random_x==a[i]['x']?and?random_y==a[i]['y']: flag==False breakif?flag==True:
print("random_x=%d,random_y=%d"%(random_x,random_y))else:
random_x=random.randint(3,map_width-8) random_y=random.randint(3,map_height-8)源代碼(註意源代碼的縮進)