processing 蛇 X 方块 X 食物
2019-07-26 本文已影响0人
homeless_honey


1. 会动的蛇头
- x += xspeed * scl
- 蛇头有位置有方向.
- if (keyCode == RIGHT) {s.dir(1, 0);}
- 键盘控制蛇的方向
- x = constrain(x, 0, width-scl)
- 蛇运动的范围限制

2. 刷出食物
- food = new PVector(floor(random(cols)), floor(random(rows))).mult(scl)
- 随机刷出
- if (dist(x, y, food.x, food.y) < 1) {}
- 吃掉食物,蛇身变长
- tail.add(new PVector(x, y))
- 用数组tail存下蛇头经过的蛇身长度个位置

3. 死亡审判
- if (dist(x, y, tail.get(i).x, tail.get(i).y)< 1) {}
- 当尾巴撞到头

4. 自己动吧
-
x = (x<=food.x)
-
?lerp(ceil(x/scl), food.x/scl, 0.005)*scl
-
:lerp(floor(x/scl), food.x/scl, 0.005)*scl
- 使用lerp求位置先水平移动,再竖直
