# Make path move like snake in snake game

In my game I have a Nurbs curve in the XY plane. I have a rigged car, that has a follow path constraint, so that it follows this curve.

I would like to make the Nurbs curve behave like a snake in the snake game. The snake should move by extending its head and cutting off its tail. When the player presses the 'up' arrow, the head should extend forward and the tail should be cut off. When the player presses the 'right' arrow, than the head should make a turn to the right, and idem for the 'left' arrow.

Is it possible to do that? In the pictures, the dots are the vertices of the Nurbs curve. I can give more details if necessary. Thanks :)

Examples of the curve turning to the left, the right, and moving straight:

• You said ..[the head will extend forward and the tail will be cut off]. Some readers may not understand. How is this different than regular movement? Can you include a picture with some frames or a small animation file.gif? Jun 8 '15 at 14:28
• So you want the previous nurb curve vertex to be placed at the position where the next vertex was during previous frame if the position isn't where the next vertex is right now? I don't know how to do it, I'll be honest. Mar 4 '16 at 19:39

The following code moves each point in the curve ahead one spot. So the second point moves to where the first point is, the third to the second, and so on.

All you need to do is decide where the first point needs to be moved next.

import bpy

# Assumes your curve is the only one in the scene.
curve = bpy.data.curves[0]

items = curve.splines[0].points.items()
point_count = len(items)

for index in range(point_count - 1, 0, -1):
current = items[index][1].co
previous = items[index - 1][1].co
current.x = previous.x
current.y = previous.y
current.z = previous.z

first = items[0][1].co
first.x -= 1


The final part moves the first point to the new position. So you could do first.x -= 1 for left, first.x += 1 for right, and first.y -= 1 for forward. This is making assumptions about the orientation of the scene, so you should adjust x, y, and z to suit your scene.

This code is very simple, but hopefully it is enough to get started.

• Consider using shape keys. You have the shapes above. Also move the entire curve object in the direction that suits you.
• Thanks, but I would like to keep extending its head and cutting off its tail, while the 'up arrow' key is pressed Jun 8 '15 at 14:15