Linked list structure (snake games)

Source: Internet
Author: User

Implementation principle

Add script Bodyfollow to each node of the snake's body so that it can move and set its child nodes following the parent node

1  Public classBodyfollow:monobehaviour {2 3     //The next node.4      PublicBodyfollow Next;5 6     //the current position of this node.7 Vector3 originalpisition;8 9     //the method by which the node moves.Ten      Public voidFollow (Vector3 gotoposition) One     { A         //record the current position before moving. -Originalpisition = This. transform.position; -  the         //move the node to the location of the first node. -          This. transform.position =gotoposition; -  -         //if the next node is present, its move method is called recursively. +         if(Next) -         { + Next. Follow (originalpisition); A         } at     } -  -}

Game implementation Process

1, first add a cube as the head of the small snake, and then add a cube as the body of a small snake, drag the default body, change its color and size, so that it can be separated from the head, and then add a sphere and drag as a preset as food

2. Add the Bodyfollow script that was written before the small snake's body

3. Add Snakemove script to the head of the snake

1  Public classSnakemove1:monobehaviour {2 3     //gets the body's default body.4      PublicGameobject Bodyprefab;5 6     //the movement speed of the little snake.7      Public floatMovespeed;8 9     //time-register.Ten     Private floatTimer =0; One  A     //The current position. -     PrivateVector3 originalposition; -  the     //The next node. - Bodyfollow Next; -  -     //The last node. + Bodyfollow tail; -      +     voidStart () A     { at addbody (); -     } -      -      -     voidUpdate () { -  in Movecontroller (); -     } to      +     voidMovecontroller () -     { the         //move the method. *Timer + =Time.deltatime; $         if(Timer >1/movespeed)Panax Notoginseng         { -Originalposition =transform.position; the              This. Transform. Translate (Vector3.forward *0.8f); +             if(Next) A             { the Next. Follow (originalposition); +             } -Timer-=1/Movespeed; $         } $  -         //direction control. -         if(Input.getkeydown (KEYCODE.D) | |Input.getkeydown (keycode.rightarrow)) the         { -              This. Transform. Rotate (Vector3.up * -);Wuyi         } the         if(Input.getkeydown (keycode.a) | |Input.getkeydown (keycode.leftarrow)) -         { Wu              This. Transform. Rotate (Vector3.down * -); -         } About  $  -     } -  -     //add the body. A      Public voidaddbody () +     { theBodyfollow BODY = ((Gameobject) instantiate (Bodyprefab, Vector3.one *9999, quaternion.identity)). Getcomponent<bodyfollow>(); -  $         //If there is no next node, the next node is the newly generated body, and the tail is the newly generated body. the         if(!next) the         { theNext =body; theTail =Next; -         } in  the         //if present, the newly generated body is set to the next, and the tail is the newly generated body. the         Else About         { theTail.next =body; theTail =body; the         } +     } -  the}

The rest of the food to increase the body of the method is relatively simple, here do not write, simple list structure, if there is not well written place also hope that you have a lot of advice ha ~

The following is a more complete demo, press any key to start the game, use a, d or ←, → control the small snake steering

Web Version Preview

Download the PC version

Linked list structure (snake games)

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.