Cinema 4d Tutorial - Python Scripting SNAKE Game

Published: 10 March 2015
on channel: Marc O'Donnel
13,651
182

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()


On this page of the site you can watch the video online Cinema 4d Tutorial - Python Scripting SNAKE Game with a duration of hours minute second in good quality, which was uploaded by the user Marc O'Donnel 10 March 2015, share the link with friends and acquaintances, this video has already been watched 13,651 times on youtube and it was liked by 182 viewers. Enjoy your viewing!