Game development live blogging - Part 1

Comment
I'm starting a project called "Snake On Acid" using Unity3d engine. Its the classic old Nokia snake game, but with all the game development magic I have learnt so far in my career.

It is going to have what I believe are some interesting time based twists, amazing sound and flashy graphics with lot of juicyness! Much in line with super hexagon's style.

Lets start..

I have setup a basic project with some graphics inspired from Neave snake. I hope to change the graphics later. I have a "gameboard" background graphic object (with sorting layer : background), a snake game object with three snake body objects (sorting layer : foreground) and a main camera object.

Oh I also have Demigiant's DoTween plugin added to the Unity project. There's going to be some amazing tweening :)

Now to make the snake move, but with some tween magic. I am going to focus only on the head snake object and attach a script to the body. Body part will be a prefab, and will be replicated at run-time. They might behave like a link-list.

Object called "body0"is going to be my head. Also I need measure my gameboard units. Looks like each snake body part is 0.22 units in size. Hmm, I would want my top-left to be 0, 0 for my snake grid. Right now the center is 0, 0.

Here is the setup for snake's head object
Now I am going to move the background so that 0, 0 is at the top-left.
Oops, the camera seems to have shifted. So I'm going to UNDO. Then add the camera as a child to the gameboard object, and then move the gameboard.
BINGO!

Now to fix the awkward snake head ready to bash the wall problem. If I start the game, and the snake moves left, it would die immediately. I will need to measure the width and the height in grid units soon. For now I will focus on moving the snake elsewhere.

Okay I moved the snake's head down, and oops, I notice that downwards is negative y. So I'd rather have bottom-left as 0,0. I like the right as +X and top as +Y.

I moved the snake head as top as possible, the Y value was 3.29. Now 3.29 / 0.22 (height of snake body) is 14.95, I could try to make 15 units. So 0.22 * 15 is 3.3. Let us see if 3.3 goes outside the border. 

Nope it seems to be perfect. Lucky me. Hope the same happens in X axis. Lesson learnt, next time, make the graphics to scale and proper units.

Now to check the X axis as planned. Let me drag it as far right as possible. Its 24 units measure with the same technique as above. So there it is my gameboard is a 24 x 15 grid.

Alright now to define some basic scripts and move snake body, starting with head. I'm choosing the head and placing it in the middle of the board (12, 7)

Now I will add a new script component called "SnakePart.cs" to the snake head. I am adding some variable as needed.
private float gridConverter = 0.22f;
private int maximumX = 24;
private int maximumY = 15;
public int X;
public int Y;
Now gridConverter is 0.22 because snake width and height is 0.22f. Next I need to place the snake in the right position on Start()

void Start ()
{
this.transform.localPosition = new Vector3 (X * gridConverter, Y * gridConverter, 0);
}

Next it would be nice to drag the snake's body and snap to grid in the gameboard. That would be cool. I remember there used to be a script attribute called [ExecuteInEditMode]. I am going to add that to the top. Then I need to write some code inside Update()

void Update ()
{
this.X = Mathf.RoundToInt (this.transform.localPosition.x / gridConverter);
this.Y = Mathf.RoundToInt (this.transform.localPosition.y / gridConverter);
this.transform.localPosition = new Vector3 (X * gridConverter, Y * gridConverter, 0);
}

Notice how I have repeated a line in Start() and Update(). I will refactor that later if needed.

Here is the final script

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class SnakePart : MonoBehaviour
{
private float gridConverter = 0.22f;

// private int maximumX = 24;
// private int maximumY = 15;

public int X;
public int Y;

// Use this for initialization
void Start ()
{
this.transform.localPosition = new Vector3 (X * gridConverter, Y * gridConverter, 0);
}
// Update is called once per frame
void Update ()
{
this.X = Mathf.RoundToInt (this.transform.localPosition.x / gridConverter);
this.Y = Mathf.RoundToInt (this.transform.localPosition.y / gridConverter);
this.transform.localPosition = new Vector3 (X * gridConverter, Y * gridConverter, 0);
}

}

In the next part, I will link the snake body, enjoy dragging a live snake in edit mode. For now I love how I can do that with snake body already.

P.S. I have commented the maximumX, and Y because it gives compiler warning. I just can't stand it! Will use it when needed later.