2017-06-11 73 views
0

我试图在python中为犀牛做一个步进函数, 这个函数应该在随机的方向上做一个步骤,而不会退步。 我如何防止退后一步?防止犀牛回到python

import rhinoscriptsyntax as rs 
import random as r 
r.seed(seed) 



class Walker: 

def __init__(self): 
    self.x = 0 
    self.y = 0 


def point(self): 
    shape = rs.AddPoint(self.x, self.y, 0) 
    return shape 

def step(self): 
    choice = r.randint(0,3) 
     choice = r.randint(1,3) 
    if (choice == 0): 
     self.x = self.x + r.choice(uList) 
    elif (choice == 1): 
     self.x = self.x - r.choice(uList) 
    elif (choice == 2): 
     self.y = self.y + r.choice(uList) 
    else: 
     self.y = self.y - r.choice(uList) 

uList = [8,11,14] 

w = Walker() 


pList = [] 
for t in range(time): 
w.step() 
pList.append(w.point()) 

for t-1 in range(time): 

a = pList 
+0

你回电了什么? 'x'方向?如果是这样:只添加(正数)到'self.x'。 –

回答

0

这条线:

choice = r.randint(0,3) 

选择的4个方向随机之一。但是如果你不想倒退,那么你只需要向前,向左和向右。因此,将参数更改为randint(),以便它只从3个可能的数字中选择,避免与您要拨打的方向向后相对应的那一个,无论哪一个。