// Update is called once per frame void Update() { time++; time2++; if (gameMode == 1 && time >= maxTime) { GameObject apple = Instantiate(ApplePref) as GameObject; Apples.Add(apple); time = 0; } if (gameMode == 1 && time2 >= maxTime * 3) { GameObject apple = Apples[0]; if (apple == null) { Apples.Remove(apple); apple = Apples[0]; } Destroy(apple); Apples.Remove(apple); time2 = 0; } if (SnakeObj != null) { xx = 0; yy = 0; if (Input.GetAxis("Horizontal") > 0) { xx = 1; } if (Input.GetAxis("Vertical") > 0) { yy = 1; } if (Input.GetAxis("Horizontal") < 0) { xx = -1; } if (Input.GetAxis("Vertical") < 0) { yy = -1; } if (xx != 0 || yy != 0) { SnakeLife s = SnakeObj.GetComponent <SnakeLife>(); if (xx != 0) { s.direction = new Vector2(xx, 0); } if (yy != 0) { s.direction = new Vector2(0, yy); } } } }
private void OnCollisionEnter(Collision collision) { SnakeLife S = collision.gameObject.GetComponent <SnakeLife>(); if (S != null) { S.SnakeDestroy(); } }
// Update is called once per frame void Update() { if (SnakeObj != null) { //обнуляем направление движения XX = 0; YY = 0; if (Input.GetAxis("Horizontal") > 0) { XX = 1; } if (Input.GetAxis("Horizontal") < 0) { XX = -1; } if (Input.GetAxis("Vertical") > 0) { YY = 1; } if (Input.GetAxis("Vertical") < 0) { YY = -1; } if ((XX != 0) || (YY != 0)) { //получаем компонент управления змейкой SnakeLife S = SnakeObj.GetComponent <SnakeLife>(); //указываем змейке куда ей двигаться if (XX != 0) { S.DirecionHod = new Vector2(XX, 0); } if (YY != 0) { S.DirecionHod = new Vector2(0, YY); } } } else { GameMode = 0; } if (GameMode > 0) { Buff++; if (Buff > TimeSpeed) { AddEat(); Buff = 0; } } }
private void OnCollisionEnter(Collision collision) { SnakeLife s = collision.gameObject.GetComponent <SnakeLife>(); if (s != null) { // Destroy(collision.gameObject); s.DestroySnake(); } }
private void OnCollisionEnter(Collision collision) { SnakeLife s = collision.gameObject.GetComponent <SnakeLife>(); if (s != null) { s.addChank(); s.SnakeScore++; Destroy(gameObject); } }
void OnCollisionEnter(Collision collision) { SnakeLife S = collision.gameObject.GetComponent <SnakeLife>(); if (S != null) { S.ScoreSnake += 3;//add score S.kolvo++; DestroyObject(this.gameObject);//kill poison } }
void OnCollisionEnter(Collision collision) { SnakeLife S = collision.gameObject.GetComponent <SnakeLife>(); if (S != null) { S.AddChank5(); //add elem S.ScoreSnake += 5; //add score S.kolvo++; DestroyObject(this.gameObject); //kill apple } }
private void OnCollisionEnter(Collision collision) { SnakeLife S = collision.gameObject.GetComponent <SnakeLife>(); if (S != null) { S.AddChank(); S.ScoreSnake++; DestroyObject(this.gameObject); } }
void OnGUI() { int posX = Screen.width / 2; int posY = Screen.height / -50; switch (gameMode) { case 0: Boolean stateBtn1 = GUI.Button(new Rect( new Vector2(posX - 100, posY), new Vector2(200, 30) ), "старт игры"); Boolean stateBtn2 = GUI.Button(new Rect( new Vector2(posX - 100, posY + 50), new Vector2(200, 30) ), "Выход"); if (stateBtn1) { createSnake(); } { createSnake(); } if (stateBtn2) { Application.Quit(); } { Application.Quit(); } break; case 1: SnakeLife snakeL = SnakeObj.GetComponent <SnakeLife>(); if (snakeL != null) { score = snakeL.snakeScore; Rect rect = new Rect( new Vector2(posX - 20, 0), new Vector2(200, 30) ); GUI.Label(rect, "Score:" + score); } break; } }
void OnCollisionEnter(Collision collision) { SnakeLife S = collision.gameObject.GetComponent <SnakeLife>(); if (S != null) { S.SnakeDestroy(); } Oleg O = collision.gameObject.GetComponent <Oleg>(); if (O != null) { O.OlegDestroy(); } }
/// <summary> /// 贪吃蛇游戏初始化 /// </summary> public void SnakeGameRenew() { SnakeBody _body; _SnakeList.Clear();//清空所有数据 //初始化游戏运行数据 _Direction = SnakeDirection.Right; _Life = SnakeLife.Live; _Score = 0; //初始化蛇头 _body = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Head); _SnakeList.Add(_body); //创建初始蛇身 int initLength = 3; if (initLength > 1) { int dx = 0, dy = 0; int x = _SnakeList[0].x, y = _SnakeList[0].y; _GetVariation(_Direction, out dy, out dy); for (int i = 1; i < initLength; i++) { x -= (dx * _BodySize); y -= (dy * _BodySize); _body = new SnakeBody(x, y, _SColor, true, SnakeProperty.Body); _SnakeList.Add(_body); } } //初始化食物 int fx = 0, fy = 0; _Food.color = _SColor; _Food.exist = true; _Food.proty = SnakeProperty.Food; _SnakeGetRandomXY(out fx, out fy); _Food.x = fx; _Food.y = fy; //重绘游戏画面 _SnakeClearWindow(); _SnakeReDrawGame(); }
//покажем кнопку старта void OnGUI() { int posaY = Screen.height / 2; int posaX = Screen.width / 2; switch (GameMode) { case 0: if (GUI.Button(new Rect(new Vector2(posaX - 100, posaY), new Vector2(200, 30)), "Старт игры")) { CreateSnake(); } if (GUI.Button(new Rect(new Vector2(posaX - 100, posaY + 40), new Vector2(200, 30)), "Выход")) { Application.Quit(); } break; case 1: //получаем компонент управления змейкой SnakeLife S = SnakeObj.GetComponent <SnakeLife>(); int Score = 0; if (S != null) { Score = S.ScoreSnake; } //показываем очки на экране GUI.Label(new Rect(new Vector2(posaX, 0), new Vector2(200, 30)), "Набрано яблок" + Score); break; } }
// Update is called once per frame void Update() { if (SnakeObj != null) { //obnulit napravlenie XX = 0; YY = 0; if (Input.GetAxis("Horizontal") > 0) { XX = 1; } if (Input.GetAxis("Horizontal") < 0) { XX = -1; } if (Input.GetAxis("Vertical") > 0) { YY = 1; } if (Input.GetAxis("Vertical") < 0) { YY = -1; } if ((XX != 0) || (YY != 0)) { //get component to moderete snake SnakeLife S = SnakeObj.GetComponent <SnakeLife>(); if (XX != 0) { S.DirectionHod = new Vector2(XX, 0); } if (YY != 0) { S.DirectionHod = new Vector2(0, YY); } Oleg O = OlegObj.GetComponent <Oleg>(); //kuda dvigatsa if (XX != 0) { O.DirectionHod = new Vector2(XX, 0); } if (YY != 0) { O.DirectionHod = new Vector2(0, YY); } } } else if (OlegObj != null) { //obnulit napravlenie XX2 = 0; YY2 = 0; if (Input.GetAxis("Horizontal") > 0) { XX2 = 1; } if (Input.GetAxis("Horizontal") < 0) { XX2 = -1; } if (Input.GetAxis("Vertical") > 0) { YY2 = 1; } if (Input.GetAxis("Vertical") < 0) { YY2 = -1; } if ((XX2 != 0) || (YY2 != 0)) { //get component to moderete snake Oleg O = OlegObj.GetComponent <Oleg>(); //kuda dvigatsa if (XX2 != 0) { O.DirectionHod = new Vector2(XX2, 0); } if (YY2 != 0) { O.DirectionHod = new Vector2(0, YY2); } } } else { GameMode = 0; } if (GameMode > 0) { Buff++; if (Buff > TimeSpeed)//create new apple and buff==0 { AddFive(); AddFour(); AddThree(); AddAuto(); Buff = 0; } } }
void OnGUI() { GUI.skin = MainMenu; int posaY = Screen.height / 2; int posaX = Screen.width / 2; switch (GameMode) { case 0: GUI.Label(new Rect(new Vector2(posaX - 150, posaY - 60), new Vector2(400, 60)), "<color=white><size=40>CHOOSE GAME</size></color>"); if (GUI.Button(new Rect(new Vector2(posaX - 100, posaY), new Vector2(200, 30)), "Opahen")) { CreateSnake1(); } if (GUI.Button(new Rect(new Vector2(posaX - 100, posaY + 20), new Vector2(200, 30)), "Oleg")) { CreateSnake2(); } if (GUI.Button(new Rect(new Vector2(posaX - 100, posaY + 40), new Vector2(200, 30)), "EXIT")) { Application.Quit(); } break; case 1: //get component to mode snake SnakeLife S = SnakeObj.GetComponent <SnakeLife>(); int Score = 0; int Kol = 0; int Sr = 0; if (S != null) { Score = S.ScoreSnake; } //show score if (S != null) { Kol = S.kolvo; } Sr = S.ScoreSnake / S.kolvo; GUI.Label(new Rect(new Vector2(posaX - 50, 0), new Vector2(200, 80)), "KKOl:" + Kol); GUI.Label(new Rect(new Vector2(posaX, 0), new Vector2(200, 30)), "Apples:" + Score); GUI.Label(new Rect(new Vector2(posaX - 100, 0), new Vector2(200, 80)), "SRES:" + Sr); break; case 2: //get component to mode snake Oleg O = OlegObj.GetComponent <Oleg>(); Score = 0; Kol = 0; Sr = 0; if (O != null) { Score = O.ScoreOleg; } //show score if (O != null) { Kol = O.kolvo; } Sr = O.ScoreOleg / O.kolvo; GUI.Label(new Rect(new Vector2(posaX - 50, 0), new Vector2(200, 80)), "KKOl:" + Kol); GUI.Label(new Rect(new Vector2(posaX, 0), new Vector2(200, 30)), "Apples:" + Score); GUI.Label(new Rect(new Vector2(posaX - 100, 0), new Vector2(200, 80)), "SRES:" + Sr); break; } }
/// <summary> /// 开始运行游戏 /// </summary> public void SnakeGameRun() { _Life = SnakeMoveStep(_Direction); }
/// <summary> /// 蛇身前进一步 /// </summary> /// <param name="dir">前进方向</param> /// <returns>蛇生命值</returns> private SnakeLife SnakeMoveStep(SnakeDirection dir) { SnakeBody tail = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Body); tail.x = _SnakeList[_SnakeList.Count - 1].x;//保存蛇尾坐标 tail.y = _SnakeList[_SnakeList.Count - 1].y; SnakeBody body_t = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Body); for (int i = (_SnakeList.Count - 1); i > 0; i--) {//更新蛇身坐标 body_t.x = _SnakeList[i - 1].x; body_t.y = _SnakeList[i - 1].y; _SnakeList[i] = body_t; } int dx = 0, dy = 0; _GetVariation(dir, out dx, out dy); //根据移动方向获取移动xy轴上的增量 SnakeBody body_h = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Head); //更新蛇头坐标 body_h.x = _SnakeList[0].x + dx; body_h.y = _SnakeList[0].y + dy; //穿墙处理 int _maxX = _Windows.Size.Width / _BodySize; int _maxY = _Windows.Size.Height / _BodySize; if (_CrossWall == false) { if ((body_h.x >= _maxX) || (body_h.x < 0) || (body_h.y >= _maxY) || (body_h.y < 0)) { _Life = SnakeLife.Die; } } if (body_h.x >= _maxX) { body_h.x = 0; } if (body_h.x < 0) { body_h.x = _maxX - 1; } if (body_h.y >= _maxY) { body_h.y = 0; } if (body_h.y < 0) { body_h.y = _maxY - 1; } _SnakeList[0] = body_h;//更新蛇头坐标 //判断是否吃到食物或咬到自己(在未更新坐标之前预判) int foodx = 0, foody = 0; SnakeProperty p = _SnakeGetPointProperty(_SnakeList[0].x, _SnakeList[0].y); switch (p) { case SnakeProperty.Food: Console.WriteLine("SnakeProperty: " + p.ToString() + " SnakeLife: " + _Life.ToString()); //吃到食物后产生一个食物,蛇身长度增加 if (_SnakeGetRandomXY(out foodx, out foody)) //创建食物 { _Food.x = foodx; _Food.y = foody; _SnakeList.Add(tail); //增加蛇身长度 _Score++; //得分增加 _Life = SnakeLife.Live; } else { _Life = SnakeLife.Win; //return _Life; } break; case SnakeProperty.Body: case SnakeProperty.Wall: Console.WriteLine("SnakeProperty: " + p.ToString() + " SnakeLife: " + _Life.ToString()); _Life = SnakeLife.Die; //死亡 //return _Life; break; case SnakeProperty.Head: case SnakeProperty.Null: default: _Life = SnakeLife.Live; break; } _SnakeClearPoint(tail); //清除蛇尾 _SnakeReDrawGame(); //更新显示蛇身 return(_Life); }