|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random as r
level_x = [0,10]
level_y = [0,10]
class Turtle:
inibody = 100
iniX = r.randint(level_x[0],level_x[1])
iniY = r.randint(level_y[0],level_y[1])
new_X = 0+iniX
new_Y = 0+iniY
def move(self):
direction = []##我想要实现去哪个方向,所以这里加了一个方向的列表
dire=-1
direcion.append(r.choice(['x','y']))
dire += 1
if direction[dire] == 'x':
new_X = new_X+r.choice([1,2,-1,-2])
if new_X >level_x[1]:
new_X = level_x[1]-(new_X-level_x[1])
if new_X < level_x[0]:
new_X = level_x[0]+(level_x[0]-new_X)
if direction[dire] == 'y':
new_Y = new_Y+r.choice([1,2,-1,-2])
if new_Y >level_y[1]:
new_Y = level_y[1]-(new_Y-level_y[1])
if new_Y < level_y[0]:
new_Y = level_y[0]+(level_y[0]-new_Y)
newbody = self.inibody - 1
print('乌龟的当前位置为%s,%s.体力为%s'%(new_X,new_Y,newbody))
但是运行后报错说NameError: name 'direction' is not defined,我十分疑惑,求大佬救救孩子吧
|
|