C # greedy Snake source

Source: Internet
Author: User

Using Unityengine;

Using System.Collections;
Using System.Diagnostics;
Using Unityengine.scenemanagement;
Using System.Collections.Generic;
Using System.Linq;
Using Unityengine.ui;


public class Snakemove:monobehaviour {
List<transform> BODY = new list<transform> ();//The array body that holds the Transform data type
Public Gameobject bodyobject;//define a game object snake
Public Gameobject sfood;////defines a game object food


UpDown Y-Axis left-down is the x-axis forward back is the z-axis
Vector3 postion = vector3.up; Abbreviation of Vector3.up Vertor3 (0,1,0)
private bool S = false;
Use this for initialization
public float speed=0.1f;
public float time = 0;
public float TIME0 = 1;
public float xlimit = 25.5f;
public float ylimit = 25.5f;
public int xlimit = 25;
public int ylimit = 25;


Damage value
public int Value;
Target Location
Private Vector3 Mtarget;
Screen coordinates
Private Vector3 Mscreen;
Text width
public float contentwidth = 100;
Text height
public float contentheight = 50;
GUI coordinates
Private Vector2 Mpoint;
Cool fonts
Guistyle Frontstyle = new Guistyle ();

public text text;
Vector3 vpostion;

Public Gameobject body1;
Public ArrayList list = new ArrayList ();
private bool iseated = false;
void Start () {

time.timescale=1;
Time = Time.time + time;
Invokerepeating is called every four seconds starting from the first second
Invokerepeating ("Food", 1, 4); Call food every 4 seconds after 1 seconds
Invokerepeating ("Move", speed, speed);

Get Target Location
Mtarget =transform.position;
Get screen coordinates
Mscreen =camera.main.worldtoscreenpoint (Mtarget);
Convert screen coordinates to GUI coordinates
Mpoint = new Vector2 (MSCREEN.X,SCREEN.HEIGHT-MSCREEN.Y);
//
Value = 0;
}

Update is called once per frame
void Update () {

if (Input.getmousebuttondown (0))
time.timescale=1;
if (input.getkeydown//(get key pressed) (KEYCODE.D) &&postion!=vector3.left)

{
postion = Vector3.right;
}
if (Input.getkeydown (KEYCODE.A) &&postion!=vector3.right)
{
postion = Vector3.left;
}
if (Input.getkeydown (KEYCODE.S) &&postion!=vector3.up)
{
postion = Vector3.down;
}
if (Input.getkeydown (KEYCODE.W) &&postion!=vector3.down)
{
postion = Vector3.up;
}
Time.tiem system Time
if (time<=time.time)
//        {
Transform. Translate (postion);
Time + = 1;
The smaller the time, the faster it gets.
//        }

Random r = new Random ();
Ontriggerenter ();

}
void Move ()
{
Transform. Translate (postion);

Vpostion = transform.position;
Transform. Translate (postion); Transform.translate how far to move an object in a direction
if (iseated)
{
Gameobject g = (gameobject) instantiate (body1,vpostion,quaternion.identity);
G.getcomponent<meshrenderer> (). Material.color = new Color (random.range (0, 1.0f), Random.range (0, 1.0f), Random. Range (0, 1.0f));
List. Insert (0, g);//Inserts an item into the ilist< at the specified index (of < (t>) >).
Inserts an element into the ArrayList at the specified index. can be inserted in any position.
iseated = false;
}
else if (list. COUNT&GT;0)
{
The position of the last element is assigned to a new position
The last element is inserted at the address of the first element
Delete last Element
((Gameobject) list[list. Count-1]). transform.position = Vpostion;
List. Insert (0, List [list]. COUNT-1]);//Insert a in 0 position
List. RemoveAt (list. COUNT-1);//Removes the element at the specified index of ArrayList.
}
}
void food ()
{
System.Random r = new System.Random ();
float x = R.next (-xlimit,xlimit);
Float y = r.next (-ylimit,ylimit);

float x = Random.range (-xlimit,xlimit);
Float y = random.range (-ylimit, ylimit);
Instantiate (Sfood, New Vector2 (x, y), quaternion.identity);
}
void Ontriggerenter (Collider other)
{

if (Other.gameObject.CompareTag ("Food"))
{
if (!iseated)
value++;
Iseated = true;
Destroy (Other.gameobject);
}
Else
{


time.timescale=0;

Scenemanager.loadscene (0);
}
Text.text = "Score:" + Value;
}
void Ongui ()
{
Ensure the target is in front of the camera
if (mscreen.z > 0)
{
Gui.color = Color.Blue;
Use GUI coordinates to draw internally
frontstyle.fontsize=40;
FrontStyle.normal.background = null;//Set Background fill
FrontStyle.normal.textColor = new Color (100, 0, 128);//Set Font color
Gui. Label (New Rect (30,0,contentwidth,contentheight), "fractional" +value.tostring (), Frontstyle);
Mpoint.x,mpoint.y
}
}
}

C # greedy Snake source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.