unity贪吃蛇c 语言代码,给新手发一段贪吃蛇的代码

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

public class Tanchishe1 : MonoBehaviour {

She she;

Dirction dirction;

Food food;

void Start () {

she=new She();

InvokeRepeating("Move",2,GameConfig.speed);

dirction=Dirction.right;

food=new Food();

}

void Move()

{

she.Pa(dirction);

}

void Update () {

string input=Input.inputString;

if(input.Length>0)

{

if(input=="w"&&dirction!=Dirction.down)

{

dirction=Dirction.up;

}

if(input=="s"&&dirction!=Dirction.up)

{

dirction=Dirction.down;

}

if(input=="a"&&dirction!=Dirction.right)

{

dirction=Dirction.left;

}

if(input=="d"&&dirction!=Dirction.left)

{

dirction=Dirction.right;

}

}

she.Eat(food);

}

void OnGUI()

{

she.Show();

Map.Box();

food.Show();

}

}

class She

{

public List body=new List();

public She()

{

int temp=5;

for(int i=0;i<4;i++)

{

body.Add(new Position(temp--,1));

}

}

public void Show()

{

for(int i=0;i

{

GUI.Button(body[i].Torect(),"");

}

}

public void Pa(Dirction dirction)

{

for(int i=body.Count-1;i>0;i--)

{

body[i].x=body[i-1].x;

body[i].y=body[i-1].y;

}

Position head=body[0];

switch(dirction)

{

case Dirction.up:

head.y--;

break;

case Dirction.down:

head.y++;

break;

case Dirction.left:

head.x--;

break;

case Dirction.right:

head.x++;

break;

}

}

public void Eat(Food food)

{

Position foodposition=food.position;

Position headposition=body[0];

if(foodposition.x==headposition.x&&foodposition.y==headposition.y)

{

this.AddLength();

food.Change();

}

}

public void AddLength()

{

Position headposition=body[0];

Position temp= new Position(headposition.x,headposition.y);

body.Insert(1,temp);

}

}

class Position

{

public int x;

public int y;

public Position()

{

}

public Position(int x,int y)

{

this.x=x;

this.y=y;

}

public Rect Torect()

{

return(new Rect(x*GameConfig.boxsize,y*Gameconfig.boxsize,GameConfig.boxsize,GameConfig.boxsize));

}

}

class Map

{

public static void Box()

{

for(int i=0;i

{

for(int j=0;j

{

Rect temp=new Rect(i*GameConfig.boxsize,j*GameConfig.boxsize,GameConfig.boxsize,GameConfig.boxsize);

if(i==GameConfig.width-1||j==GameConfig.heigth-1||i==0||j==0)

{

GUI.Button(temp,"");

}

}

}

}

}

class GameConfig

{

public static int boxsize=30;

public static float speed=0.3f;

public static int width=20;

public static int heigth=20;

}

class Food

{

public Position position=new Position();

public Food()

{

position.x=Random.Range(1,GameConfig.width-1);

position.y=Random.Range(1,GameConfig.heigth-1);

}

public void Change()

{

position.x=Random.Range(1,GameConfig.width-1);

position.y=Random.Range(1,GameConfig.heigth-1);

}

public void Show()

{

GUI.Button(position.Torect(),"");

}

}

enum Direction{up,down,left,right}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值