March 10th, 2016

[KISSCODE] Let's make SNAKE Pt. 2

by Adhi.Lius

Hi everyone happy Thursday. Hope you have a good week. Now for the continuation of our part 1, now, we are going to get to some basic of Snake game.

The first basic game logic I get is to interpret the game as series of pixels / blocks that needs to be drawn on screen. It is formed as two level array, with x as the width and y as the height. And then for each iteration (time), each block will be redrawn or move - in a block space far - to show the movement of things in the screen. This is the usual in any 2D game, so imagining it this way, will make things easier when you try to imagine the game logic.

Putting images in only x and y axis used to be hard on Unity 3D but now with its new 2D feature, this actually makes life easier for 2D game. First let us decide that we are going to do this in a 400 x 300 screen size. With each block will take 10 x 10 pixels, making it 40 x 30 array and with walls, the game area is going to be 38 x 28 size.

Then we are going to use few simple value to put into the array:

1. Wall = 1

2. Empty = 0

3. Snake = 2

4. Fruit = 3

So an empty game scene will have an array like this:

Notes: Image is just for presentation only, so not the actual array size

Now for the actual some works, first create an empty 2D project. then you can put your images for the snake game like in the part 1 post, inside the project structure. The create two empty objects inside the scene: one to hold all the images created; the other is our controller object. Your project should kind of looks like this now.

Next we add new script to controller. Start by creating an array the size of 40 x 30 and then init the with the walls value.

using UnityEngine;

using System.Collections;

public class SnakeController : MonoBehaviour {

protected int[,] snakeArray = new int[40,30];

// Use this for initialization

void Start () {

InitWalls ();

}

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] = 1;

snakeArray [39, i] = 1;

}

//init the up down walls

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

snakeArray [i, 0] = 1;

snakeArray [i, 29] = 1;

}

}

// Update is called once per frame

void Update () {

}

}

Next we drag the image for the snake wall to the scene to create a wall object. Then drag it again to the project structure to create the wall prefab.

Then modify the script a little to add Wall prefab and StageHolder object

...

public class SnakeController : MonoBehaviour {

public GameObject wallPrefab;

public Transform stageHolder;

protected int[,] snakeArray = new int[40,30];

...

And put them to the controller script

Then we create the draw screen method, and call it after the initscreen is called.

...

// Use this for initialization

void Start () {

InitWalls ();

DrawScene ();

}

protected float FIRST_POS_X = -2.0f; //the left top x position

protected float FIRST_POS_Y = 1.5f; //the left top y position

protected float BLOCK_SiZE = .1f; //size of the block on screen (default: 1000 pixel = 1)

protected void DrawScene(){

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

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

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

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

}

}

}

}

...

Then push the run button and Voila, you got the wall created for you.

You might need to adjust the MainCamera field of view to your liking so that it will show all the tiles in the nice position.

And that's it for today #KISSCodeThursday, next week we are going to start creating the snake. See you next week :).

Comments (0)