Skip to content

Instantly share code, notes, and snippets.

@Nordaj
Created December 31, 2017 01:17
Show Gist options
  • Save Nordaj/d253c42bf268554d0c93254690c11575 to your computer and use it in GitHub Desktop.
Save Nordaj/d253c42bf268554d0c93254690c11575 to your computer and use it in GitHub Desktop.
Unity editor script simple snake game
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class Snake : EditorWindow
{
//Settings
int unit = 25;
Color headCol = new Color(0, 0.5f, 1);
Color tailCol = new Color(0, 0.5f, 0.8f);
Color foodCol = new Color(0, 1, 0.4f);
float speed = 0.1f;
//Private Vairables
private bool first;
private int score;
private Vector2 gameSize;
private Vector2 foodPos;
private Vector2 head;
private Vector2 startSize;
private List<Vector2> tail;
private Texture2D headTex;
private Texture2D tailTex;
private Texture2D foodTex;
int dir = 4; //0=up, 1=right, 2=down, 3=left, 4=still
private float lastTime;
[MenuItem("Window/Snake")]
static void Init()
{
EditorWindow win = EditorWindow.GetWindow(typeof(Snake));
win.Show();
}
void Begining()
{
//Init game
first = true;
gameSize = new Vector2(Mathf.Round(Screen.width / unit), Mathf.Round(Screen.height / unit));
head = new Vector2(Mathf.Round(gameSize.x / 2), Mathf.Round(gameSize.y / 2));
tail = new List<Vector2>();
tail.Add(head);
//Get start size
startSize = new Vector2(Screen.width, Screen.height);
//Gen food
foodPos = new Vector2(Mathf.Round(Random.Range(0, gameSize.x - 1)), Mathf.Round(Random.Range(0, gameSize.y - 1)));
//Generate Textures
headTex = new Texture2D(1, 1);
headTex.filterMode = FilterMode.Point;
headTex.SetPixel(0, 0, headCol);
headTex.Apply();
tailTex = new Texture2D(1, 1);
tailTex.filterMode = FilterMode.Point;
tailTex.SetPixel(0, 0, tailCol);
tailTex.Apply();
foodTex = new Texture2D(1, 1);
foodTex.filterMode = FilterMode.Point;
foodTex.SetPixel(0, 0, foodCol);
foodTex.Apply();
}
void OnGUI()
{
if (!first) Begining();
#region Input
//WASD
if (Event.current.keyCode == KeyCode.W) dir = 0;
if (Event.current.keyCode == KeyCode.D) dir = 1;
if (Event.current.keyCode == KeyCode.S) dir = 2;
if (Event.current.keyCode == KeyCode.A) dir = 3;
//ARROWS
if (Event.current.keyCode == KeyCode.UpArrow) dir = 0;
if (Event.current.keyCode == KeyCode.RightArrow) dir = 1;
if (Event.current.keyCode == KeyCode.DownArrow) dir = 2;
if (Event.current.keyCode == KeyCode.LeftArrow) dir = 3;
#endregion
if (EditorApplication.timeSinceStartup - lastTime > speed)
{
lastTime = (float) EditorApplication.timeSinceStartup;
#region Movement
if (dir == 0)
{
head = new Vector2(head.x, head.y - 1);
}
else if (dir == 1)
{
head = new Vector2(head.x + 1, head.y);
}
else if (dir == 2)
{
head = new Vector2(head.x, head.y + 1);
}
else if (dir == 3)
{
head = new Vector2(head.x - 1, head.y);
}
#endregion
//Check collision with sides
if (head.x < 0 || head.x > gameSize.x) Die();
if (head.y < 0 || head.y > gameSize.y) Die();
//Check collision with self
for (int i = 0; i < tail.Count; i++)
{
if (tail[i] == head && i != 0)
Die();
}
//Check food collision
if (head == foodPos) Eat();
//Shift tail
tail.RemoveAt(tail.Count - 1);
tail.Insert(0, head);
//Check updated size
if (startSize != new Vector2(Screen.width, Screen.height)) Die();
}
//Draw
///Food
GUI.DrawTexture(new Rect(foodPos.x * unit, foodPos.y * unit, unit, unit), foodTex);
///Food count
EditorGUI.LabelField(new Rect(Screen.width / 2, 20, 20, 20), score.ToString());
///Tail
foreach (Vector2 vec in tail)
GUI.DrawTexture(new Rect(vec.x * unit, vec.y * unit, unit, unit), tailTex);
///Head
GUI.DrawTexture(new Rect(head.x * unit, head.y * unit, unit, unit), headTex);
//Update
this.Repaint();
}
void Die()
{
gameSize = new Vector2(Screen.width / unit, Screen.height / unit);
startSize = new Vector2(Screen.width, Screen.height);
score = 0;
head = new Vector2(Mathf.Round(gameSize.x / 2), Mathf.Round(gameSize.y / 2));
tail = new List<Vector2>();
tail.Add(head);
dir = 4;
//Gen food
foodPos = new Vector2(Mathf.Round(Random.Range(0, gameSize.x - 1)), Mathf.Round(Random.Range(0, gameSize.y - 1)));
}
void Eat()
{
tail.Insert(tail.Count - 1, tail[tail.Count - 1]);
score++;
//Gen food
foodPos = new Vector2(Mathf.Round(Random.Range(0, gameSize.x - 1)), Mathf.Round(Random.Range(0, gameSize.y - 1)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment