1976年,Gremlin平臺推出了一款經典街機遊戲Blockade。遊戲中,兩名玩家分別控制一個角色在螢幕上移動,所經之處砌起圍欄。角色只能向左、右方向90度轉彎,遊戲目標保證讓對方先撞上螢幕或圍欄。聽起來有點複雜,其實就是下面這個樣子:
基本上就是兩條每走一步都會長大的貪吃蛇比誰後完蛋,玩家要做的就是避免撞上障礙物和越來越長的身體。更多照片、影片可以看 GamesDBase 的介紹。
Blockade 很受歡迎,類似的遊戲先後出現在 Atari 2600、TRS-80、蘋果 2 等早期遊戲機、計算機上。但真正讓這種遊戲形式紅遍全球的還是21年後隨諾基亞手機走向世界的貪吃蛇遊戲——Snake。
using UnityEngine;
using System。Collections;
using System。Collections。Generic;
using System。Linq;
using UnityEngine。UI;
using System;
public class Snake : MonoBehaviour
{
// Current Movement Direction
// (by default it moves to the right)
Vector2 dir = Vector2。right;
List tail = new List();
// Did the snake eat something?
bool ate = false;
// Tail Prefab
public GameObject tailPrefab;
public Action OnLose;
// Use this for initialization
void Start()
{
// Move the Snake every 300ms
InvokeRepeating(“Move”, 0。3f, 0。3f);
}
void OnTriggerEnter2D(Collider2D coll)
{
// Food?
if (coll。name。StartsWith(“food”))
{
// Get longer in next Move call
ate = true;
// Remove the Food
Destroy(coll。gameObject);
}
// Collided with Tail or Border
else
{
OnLose();
}
}
void Update()
{
// Move in a new Direction?
if (Input。GetKey(KeyCode。RightArrow))
dir = Vector2。right;
else if (Input。GetKey(KeyCode。DownArrow))
dir = -Vector2。up; // ‘-up’ means ‘down’
else if (Input。GetKey(KeyCode。LeftArrow))
dir = -Vector2。right; // ‘-right’ means ‘left’
else if (Input。GetKey(KeyCode。UpArrow))
dir = Vector2。up;
}
void Move()
{
// Move head into new direction
Vector2 v = transform。position;
transform。Translate(dir);
if (ate)
{
// Load Prefab into the world
GameObject g = (GameObject)Instantiate(tailPrefab,
v,
Quaternion。identity);
// Keep track of it in our tail list
tail。Insert(0, g。transform);
// Reset the flag
ate = false;
}
// Do we have a Tail?
else if (tail。Count > 0)
{
// Move last Tail Element to where the Head was
tail。Last()。position = v;
// Add to front of list, remove from the back
tail。Insert(0, tail。Last());
tail。RemoveAt(tail。Count - 1);
}
}
}