Cinema 4d Tutorial - Python Scripting SNAKE Game

Thanks! Share it with your friends!

You disliked this video. Thanks for the feedback!

Added by Dylan
9 Views
In a quick tutorial I show you how to code a simple controller in cinema4d: I use this to control a snake in a simple retro style snake game that you can play within cinema4d.

Here's the complete code:

import c4d
import random

last = [0,0]
bait = -1

def createBait():
global bait
snake = doc.SearchObject("snake").GetChildren()
randomX = int(random.uniform(-5,5)) * 10
randomY = int(random.uniform(-5,5)) * 10


bait = c4d.BaseObject(5159)
bait.SetAbsPos(c4d.Vector(randomX,randomY,0))
bait.SetName("bait")
bait[c4d.PRIM_CUBE_LEN] = c4d.Vector(8,8,8)
doc.InsertObject(bait)
return
def createNewLink(head):
posX = head.GetAbsPos().x
posY = head.GetAbsPos().y

link = c4d.BaseObject(5159)
link.SetAbsPos(c4d.Vector(posX,posY,0))
link[c4d.PRIM_CUBE_LEN] = c4d.Vector(10,10,10)

link.InsertUnder(doc.SearchObject("snake"))
return link

for i in doc.GetObjects():
if(i.GetName() == "snake"):
snake = i.GetChildren()
if(i.GetName() == "bait"):
bait = i

def main():

global last
global snake
global bait
if(bait == -1): createBait()

input = doc.SearchObject(“input”)
coordInput = input.GetAbsPos()
input.SetAbsPos(c4d.Vector(0,0,0))

if(coordInput.x != 0 or coordInput.y != 0):
if(coordInput.x != 0):
last[0] = coordInput.x / abs(coordInput.x) *10
last[1] = 0
if(coordInput.y != 0):
last[0] = 0
last[1] = coordInput.y / abs(coordInput.y) *10


for i in xrange(len(snake)):
if(i == len(snake)-1):
snake[0].SetAbsPos(c4d.Vector((snake[0].GetAbsPos().x) + last[0] , (snake[0].GetAbsPos().y) + last[1] ,0))
else:
inv = len(snake) - 1 - i
snake[inv].SetAbsPos(snake[inv-1].GetAbsPos())

if(snake[0].GetAbsPos() == bait.GetAbsPos()):
snake.extend([createNewLink(snake[0])])
bait.Remove()
c4d.EventAdd()
createBait()
Category
Cinema4D

Post your comment

Sign in or sign up to post comments.

Comments

Be the first to comment