March 30th, 2016

[KISSCODE] Let's make SNAKE Pt. 5

by Adhi.Lius

Originally posted in: http://blog.sinergistudio.com


Hello, it is time for the continuation for our #KISSCodeThursday. You can follow the previous post here if you missed it but now we are going to continue with our game.

For today we are going to make the snake body and how to make it follow the head. To do this we are going to need a few changes in what code to draw each item.

Before we are only using a single code to show snake body. It means if we are going to make our snake body go longer following the head than this is how the array going to be.




This is applicable but we can use a simpler way to do this. By changing the code for snake body from [if 2 then draw body] into [if > 0 then draw body], we can run the arrays and by using reduction we can make the snake move and remove the tail in one simple swoop.




And each time the body of the snake add then the head number is going to go bigger.




Okay. I hope with this you kind of can get the point I am trying to write. Anyway now we are going to change the code:

1. Wall = -1

2. Fruit = -2

3. Body = > 0

4. Empty = 0;

protected const int WALL = -1;

protected const int FRUIT = -2;

//create wall

protected void DrawScene(){

for (int i=0;i<40;i++){

for (int j=0;j<30;j++){

if (snakeArray [i, j] == WALL){

GameObject nWall = Instantiate (wallPrefab);

nWall.transform.parent = stageHolder;

float POS_X = FIRST_POS_X + (i * BLOCK_SiZE);

float POS_Y = FIRST_POS_Y - (j * BLOCK_SiZE);

nWall.transform.position = new Vector2 (POS_X, POS_Y);

}

}

}

}

protected void InitWalls(){

for (int i=0;i<40;i++){

for (int j=0;j<30;j++){

snakeArray [i, j] = 0;

}

}

//init the left right side walls

for (int i=0;i<30;i++){

snakeArray [0, i] = WALL;

snakeArray [39, i] = WALL;

}

//init the up down walls

for (int i=0;i<40;i++){

snakeArray [i, 0] = WALL;

snakeArray [i, 29] = WALL;

}

}

//put fruit

protected void PutNewFruit(){

Vector2 fruitPos = GetRandomPosition ();

snakeArray [(int)fruitPos.x, (int)fruitPos.y] = FRUIT;

}

//draw anything besides the wall

protected void RedrawScene(){

CleanScene ();

for (int i = 1;i<39;i++){

for (int j = 1; j < 29; j++) {

int code = snakeArray [i, j];

if (code != 0) {

float POS_X = FIRST_POS_X + (i * BLOCK_SiZE);

float POS_Y = FIRST_POS_Y - (j * BLOCK_SiZE);

if (code > 0 ) { // snake body

GameObject nSnakeBody = Instantiate (snakePrefab);

nSnakeBody.transform.parent = stageHolder;

nSnakeBody.transform.position = new Vector2 (POS_X, POS_Y);

drawnObject.Add (nSnakeBody);

} else if (code == FRUIT) { // fruit

Debug.Log ("draw fruit");

GameObject nFruit = Instantiate (fruitPrefab);

nFruit.transform.parent = stageHolder;

nFruit.transform.position = new Vector2 (POS_X, POS_Y);

drawnObject.Add (nFruit);

}

}

}

}

}

and we want in each loop/ update, the array will reduce the body point but not change any for the wall or fruit.

protected int cSnakeBodyLong = 1;

//init snake position

protected void InitSnake(){

snakeArray [(int)FIRST_SNAKE_POS.x, (int)FIRST_SNAKE_POS.y] = cSnakeBodyLong;

cSnakePos = FIRST_SNAKE_POS;

cSnakeDir = MOVE_LEFT;

}

//check snake hit

protected void CheckSnakeHitAnything(){

int code = snakeArray [(int)cSnakePos.x, (int)cSnakePos.y];

if (code != 0){ // hit something

if (code == FRUIT){ // eat fruit

PutNewFruit ();

cScore++;

cSnakeBodyLong++;

}

}

}

//recount the array

protected void RefreshSnakeArray(){

for (int i = 1; i < 39; i++) {

for (int j = 1; j < 29; j++) {

if (snakeArray[i,j] > 0){ // don't delete fruit

snakeArray [i, j] --;

}

}

}

CheckSnakeHitAnything ();

snakeArray [(int)cSnakePos.x, (int)cSnakePos.y] = cSnakeBodyLong;

}

Now hit the run button and you will get your snake moving like a snake :)




Okay that's the end for today #KISSCodeThursday. We will get to the lose condition, for next week.

Comments (1)

Michael Tanusenjaya  -  3 minutes ago

wah nice nih min ... cocok buat pemula kayak gua