Home > other >  How to realize the picture of the snake game to change
How to realize the picture of the snake game to change

Time:09-28

How to realize the turn of the images change
Turned to the turning point of the neck and bottom after follow changes and head of the
Haven't started 'new a

CodePudding user response:

1. The location of the record by pressing the button head and direction, when the end of the body to move and rotate
2. Suppose you have head, body, tail, tail first use to judge the direction of the body, with a body to determine the direction of the head
The reason is that the first seconds when you left from up
Head will change first position, the tail should be to body at this moment, in the end, which in turn will become operational at the same time

CodePudding user response:

 
//class
Public class Dir
{
Public Vector3 position;
Public Vector3 dir.
}


 
//greedy snake class
Public class Head
{
Public readonly List Dirs=new List (a);
Private Vector3 currentDir=Vector3. Zero;

Public float Speed {get; The set; }=3.0 f;

Private void the Start ()
{
CurrentDir=tranform. Forword;
}

Private void the Update ()
{
If (Input. KeyDown (KeyCode. UP))
CurrentDir=Vector3. UP;
Else if (Input KeyDown (KeyCode. Down))
CurrentDir=Vector3. Down;
Else if (Input KeyDown (KeyCode. Left))
CurrentDir=Vector3. Left;
Else if (Input KeyDown (KeyCode. Right))
CurrentDir=Vector3. Right;

If (currentDir!=tranform. Forword)
{
Tranform. Forword=currentDir;
Dirs. Add (new Dir (tranform. The position and tranform. Forword));
}

Tranform. Position +=tranform. Speed forword * * Time. The delta;
}
}

 
//snake body
Public class Body
{
Protected the Head Head=null;

Private void Awake ()
{
The head=GameObject. Find (a);
}

Private void the Update ()
{
Dir Dir=head. Dirs [head. Dirs. Length - 1);
If ((dir. Position - tranform. The position). The magnitude & lt; 0.001 f)
{
Tranform. Forword=dir. Dir.
OnForwordChanged ();
}
Tranform. Position +=tranform. Forword * head Speed * Time. The delta;
}

Protected virtual void OnForwordChanged ()
{
}
}

 
//tail, inherited from the body
Public class Tail: Body
{
Protected override void OnForwordChanged ()
{
Head. Dirs. RemoveAt (head) Dirs) Length - 1);
}
}


The code above, respectively, on the head, body and tail of the precast body, and processed food to the time-varying long, is to instantiate a prefabricated body body, additional to the front of the tail of the

This code is to provide train of thought, the body of the code can also change again, to follow the previous body, make out of the snake chain table structure ,,, so much the better,
  • Related