小甲鱼学习 发表于 2018-11-14 16:29:26

呃。。。。。

20180906 发表于 2018-11-16 18:06:29

还是不太懂闭包到底有什么用

nihaosay123 发表于 2018-11-28 13:37:57

请问为什么不用一个函数去定义这4个变量:
pos_x,pos_y,direction,step

zs123456tf 发表于 2018-11-28 18:49:55

有点难懂,慢慢看

liutingloveliu 发表于 2018-11-28 19:57:05

{:10_266:}看不懂

15828434714 发表于 2018-11-28 23:12:33

这个如果移到数据太大一样也会超过限定坐标吧

iyimi 发表于 2018-12-9 12:31:23

fyincheng 发表于 2018-12-13 06:12:47

origin =
legal_x = [-100, 100]
legal_y = [-100, 100]

def create(position_x=0, position_y=0):
    def moving(direction, step):
      nonlocal position_x,position_y
      new_x = position_x + direction * step
      new_y = position_y + direction * step
      if new_x < legal_x:
            position_x = legal_x#超出边界无法前进
      elif new_x > legal_x:
            position_x = legal_x#超出边界无法前进
      else:
            position_x = new_x

      if new_y < legal_y:
            position_y = legal_y#超出边界无法前进
      elif new_y > legal_y:
            position_y = legal_y#超出边界无法前进

      else:
            position_y = new_y
      return position_x, position_y
    return moving

move = create()
print('向上移动10步', move(,10))
print('向右移动130步', move(,10))
print('向左下移动100步', move([-1,-1],100))
print('向左上动10步', move([-1,1],10))
print('向左移动10步', move([-1,0],10))

thethrone 发表于 2018-12-23 11:23:23

studying

草儿飞花 发表于 2018-12-24 22:13:54

没看懂

千锦襄 发表于 2018-12-25 19:52:28

好东东,谢谢!

四点好 发表于 2018-12-26 00:19:43

其实不用看懂上面的代码,只要move = create()这个函数在,没有释放,每一次执行move都会数据连续性,不会被破坏,不知道这样理解对不对。

我觉的应该没错

仓鼠二号 发表于 2018-12-29 13:16:16

我的理解。。
刚开始用move = create() 调用create() 这个函数 里面没输入任何数值所以是初始值(0,0)   之后move 就变成了create()里返回的一个函数 moving
之后print 里调用move 就是在使用moving() 在这里就需要输入两个参数direction 和 step 运行后会返回 pos_x 和 pox_y
之后再次运行的print 就是再用moving ()函数,这个时候 没有运行creat( )   所以pos_x 和 pos_y没有初始化 后面继续移动会在移动后的基础上继续移动
要回到原点就 再次运行move = create() 才行

QQ2995859168 发表于 2019-1-13 19:38:29

一看就想关..........感觉这个对我作用不大.

风丶少 发表于 2019-1-17 10:16:14

看是看懂了,就是不会写{:10_249:}

Siry 发表于 2019-1-19 17:10:43

支持小甲鱼

1623422960 发表于 2019-1-20 20:29:31

啊是大苏打撒旦撒打算打算打算打算重新注册咨询

164151897@qq.co 发表于 2019-1-23 16:33:13

向楼主学习!

158864285 发表于 2019-1-24 22:03:29

有点难以理解

zq_zack 发表于 2019-1-28 12:00:08

没怎么看懂这个逻辑,特别是检查是否超范围那
页: 6 7 8 9 10 11 12 13 14 15 [16] 17 18 19 20 21 22 23 24 25
查看完整版本: 游戏中的角色移动:闭包(closure)在实际开发中的作用