private void DoCollisionCheck() { SnakePiece snakeHead = snakePieces[snakePieces.Count - 1]; if ((snakeHead.Position.X == Canvas.GetLeft(snakeFood)) && (snakeHead.Position.Y == Canvas.GetTop(snakeFood))) { EatSnakeFood(); return; } if (wallCollisions == true) { if ((snakeHead.Position.Y < 0) || (snakeHead.Position.Y >= Board.ActualHeight) || (snakeHead.Position.X < 0) || (snakeHead.Position.X >= Board.ActualWidth)) { EndGame(); } } //else //{ //PassToOtherSide(); //} foreach (SnakePiece snakeBodyPart in snakePieces.Take(snakePieces.Count - 1)) { if ((snakeHead.Position.X == snakeBodyPart.Position.X) && (snakeHead.Position.Y == snakeBodyPart.Position.Y)) { EndGame(); } } }
public void MoveSnake() { //make sure that for loop procedes from first (closest to end of snake). for (int i = 0; i < pieces.Count - 1; i++) { pieces[i].MoveNext(); } SnakePiece temp = pieces[pieces.Count - 1]; if (dir == 0) { temp.y++; } if (dir == 1) { temp.x++; } if (dir == 2) { temp.y--; } if (dir == 3) { temp.x--; } pdir = dir; pieces[pieces.Count - 1].transform.GetChild(0).eulerAngles = new Vector3(0, 0, -dir * 90); dead = 0; }
private SnakePiece SpawnChildBody() { SnakePiece obj = Instantiate(snakePiece_prefab, lastPiece.transform.position, lastPiece.transform.rotation); lastPiece.child_obj = obj; // adicionar como filho lastPiece = obj; //atualizar last piece return(obj); }
public SnakePiece(SnakePiece parent, Position position) { Parent = parent; Direction = parent == null ? Orientation.None : parent.Direction; Child = null; Location = position == null ? new Position(0, 0) : position; Character = SNAKE_EMOJI; }
// Use this for initialization void Awake() { m_SnakePiece = GetComponent <SnakePiece>(); m_Controller = GetComponent <Controller>(); m_CollectableParent = GameObject.Find("Collectables").transform; AddPiece(2); SnakeManager.snakeList.Add(this); color = m_Color; }
private void MoveTimer(object sender, EventArgs e) { int x = _snake[_front].Location.X, y = _snake[_front].Location.Y; // If still - NOP if (_dx == 0 && _dy == 0) { return; } // If over board - game over if (IsOverBoard(x + _dx, y + _dy)) { timer.Stop(); MessageBox.Show("Game over"); return; } // If Collision if (CollisionFood(x + _dx, y + _dy)) { // TODO : Can we collide body on food area ? if (HitsBody((y + _dy) / SnakePiece.SidePixelSize, (x + _dx) / SnakePiece.SidePixelSize)) { return; } // Body growing var head = new SnakePiece(x + _dx, y + _dy); _front = (_front - 1 + 1250) % 1250; _snake[_front] = head; _visit[head.Location.Y / SnakePiece.SidePixelSize, head.Location.X / SnakePiece.SidePixelSize] = true; Controls.Add(head); RandomFood(); // Refresh control this.Invalidate(); } // No collision else { if (HitsBody((y + _dy) / SnakePiece.SidePixelSize, (x + _dx) / SnakePiece.SidePixelSize)) { return; } // Move body _visit[_snake[_back].Location.Y / SnakePiece.SidePixelSize, _snake[_back].Location.X / SnakePiece.SidePixelSize] = false; _front = (_front - 1 + 1250) % 1250; _snake[_front] = _snake[_back]; _snake[_front].Location = new Point(x + _dx, y + _dy); _back = (_back - 1 + 1250) % 1250; _visit[(y + _dy) / SnakePiece.SidePixelSize, (x + _dx) / SnakePiece.SidePixelSize] = true; } }
public void NewPosition(SnakePiece pos) { //Posiciona la piece local en la posición de la piece de pos. if (piece == null) { Debug.Log("Ñoc"); } piece.position = pos.piece.position; }
/// <summary> /// Draw a snake piece on the PictureBox /// </summary> /// <param name="e"></param> /// <param name="snakePiece"></param> private static void DrawSnakePiece(PaintEventArgs e, SnakePiece snakePiece, Settings settings) { // Create solid brush. SolidBrush blueBrush = new SolidBrush(snakePiece.GetSnakePieceColor()); // Create rectangle. Rectangle rect = new Rectangle(snakePiece.X, snakePiece.Y, settings.PieceSize, settings.PieceSize); // Fill rectangle to screen. e.Graphics.FillRectangle(blueBrush, rect); }
public void AddPiece() { //Se crea un objeto de SnakePiece y se añadirá a la Lista body GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube); go.transform.position = body[body.Count - 1].piece.position - direction; SnakePiece sp = new SnakePiece(go.transform); body.Add(sp); }
private SnakePiece SpawnFirstChildBody() { SnakePiece obj = Instantiate(snakePiece_prefab, transform.position, transform.rotation); if (child_obj == null) { child_obj = obj; } lastPiece = obj;//atualizar last piece return(obj); }
private void MoveSnake() { // Remove the last part of the snake, in preparation of the new part added below while (snakePieces.Count >= snakeLength) { Board.Children.Remove(snakePieces[0].UiElement); snakePieces.RemoveAt(0); } // Next up, we'll add a new element to the snake, which will be the (new) head foreach (SnakePiece snakePart in snakePieces) { // Therefore, we mark all existing parts as non-head (body) elements and then // we make sure that they use the body brush (snakePart.UiElement as Rectangle).Fill = snakeBodyBrush; snakePart.IsHead = false; } // Determine in which direction to expand the snake, based on the current direction SnakePiece snakeHead = snakePieces[snakePieces.Count - 1]; double nextX = snakeHead.Position.X; double nextY = snakeHead.Position.Y; switch (snakeDirection) { case SnakeDirection.Left: nextX -= SnakeSquareSize; break; case SnakeDirection.Right: nextX += SnakeSquareSize; break; case SnakeDirection.Up: nextY -= SnakeSquareSize; break; case SnakeDirection.Down: nextY += SnakeSquareSize; break; } // Now add the new head part to our list of snake parts... snakePieces.Add(new SnakePiece() { Position = new Point(nextX, nextY), IsHead = true }); // And then have it drawn! DrawSnake(); DoCollisionCheck(); }
private void tryEat(GamePiece gamePiece, XYPointer nextCorindate, ObservableCollection <BasePresenterItem> items) { var goingToBeEatten = items.Where((m) => { //get the item that answers "next cordinate". if (m.CurrentGamePiece.XPosition == nextCorindate.XProperty && m.CurrentGamePiece.YPosition == nextCorindate.YProperty) { return(true); } return(false); }); var firstOrDeafult = goingToBeEatten.FirstOrDefault(); if (firstOrDeafult != null) { if (firstOrDeafult is GrassPresenter) { return; } if (firstOrDeafult is SnakePiecePresenter) { //end game DebugHelper.WriteLog("game eneded... bummer", "new snakes game manager"); return; } int index = items.IndexOf(firstOrDeafult); SnakePiecePresenter whoToFollow; if (snakeParts.Count > 0) { whoToFollow = snakeParts.Last(); } else { whoToFollow = GetHeadItems(SnakePresenter.ArrayOfItems); } var snakePiece = new SnakePiece(nextCorindate.XProperty, nextCorindate.YProperty); SnakePiecePresenter newSnakepresetner = new SnakePiecePresenter(snakePiece, whoToFollow); snakeParts.Add(newSnakepresetner); items[index] = newSnakepresetner; } DebugHelper.WriteLog(string.Format("eatted {0},{1}", nextCorindate.XProperty, nextCorindate.YProperty), "new snakes game manager"); }
void Start() { _speed = speed; if (head) { pos = transform.position; } SnakePiece headPiece = new SnakePiece(true, head); body.Add(headPiece); Time.fixedDeltaTime = 0.05f; }
private void setNextPiece() { if (transform.parent.childCount > transform.GetSiblingIndex() + 1) { Transform next = transform.parent.GetChild(transform.GetSiblingIndex() + 1); if (next) { nextPiece = next.GetComponent <SnakePiece>(); } } else { nextPiece = null; } }
void CheckForCollisions() { for (int i = 0; i < SnakeManager.snakeList.Count; ++i) { if (SnakeManager.snakeList[i] != this) { SnakePiece piece = SnakeManager.snakeList[i].CheckForCollision(transform.position); if (piece) { if (chargeCounter > 0.0f) { piece.DestroyPiece(); } } } } }
private void MoveTimer(object sender, EventArgs e) { int x = snake[front].Location.X, y = snake[front].Location.Y; if (dx == 0 && dy == 0) { return; } if (GameOver(x + dx, y + dy)) { timer.Stop(); MessageBox.Show("Game over"); return; } if (CollisionFood(x + dx, y + dy, out int scoreValue)) { score += scoreValue; labelScore.Text = "Score: " + score.ToString(); if (Hits((y + dy) / SnakePiece.SideSize, (x + dx) / SnakePiece.SideSize)) { return; } SnakePiece head = new SnakePiece(x + dx, y + dy); front = (front - 1 + 1250) % 1250; snake[front] = head; visit[head.Location.Y / SnakePiece.SideSize, head.Location.X / SnakePiece.SideSize] = true; Controls.Add(head); RandomFood(); this.Invalidate(); } else { if (Hits((y + dy) / SnakePiece.SideSize, (x + dx) / SnakePiece.SideSize)) { return; } visit[snake[back].Location.Y / SnakePiece.SideSize, snake[back].Location.X / SnakePiece.SideSize] = false; front = (front - 1 + 1250) % 1250; snake[front] = snake[back]; snake[front].Location = new Point(x + dx, y + dy); back = (back - 1 + 1250) % 1250; visit[(y + dy) / SnakePiece.SideSize, (x + dx) / SnakePiece.SideSize] = true; } }
// Update is called once per frame void Update() { transform.Translate(m_MoveDirection * 0.5f); if (transform.position.y > Snake.maxY || transform.position.y < Snake.minY || transform.position.x > Snake.maxX || transform.position.x < Snake.minX) { Destroy(gameObject); return; } for (int i = 0; i < SnakeManager.snakeList.Count; ++i) { SnakePiece piece = SnakeManager.snakeList[i].CheckForCollision(transform.position); if (piece) { piece.DestroyPiece(); Destroy(gameObject); } } }
private void Initialize() { _visit = new bool[_rows, _columns]; var head = new SnakePiece((rand.Next() % _columns) * SnakePiece.SidePixelSize, (rand.Next() % _rows) * SnakePiece.SidePixelSize); for (int i = 0; i < _rows; i++) { for (int j = 0; j < _columns; j++) { _visit[i, j] = false; _available.Add(i * _columns + j); } } RandomFood(); _visit[head.Location.Y / SnakePiece.SidePixelSize, head.Location.X / SnakePiece.SidePixelSize] = true; _available.Remove(head.Location.Y / SnakePiece.SidePixelSize * _columns + head.Location.X / SnakePiece.SidePixelSize); Controls.Add(head); _snake[_front] = head; }
private void Initialize() { visit = new bool[rows, cols]; SnakePiece head = new SnakePiece((rand.Next() % cols) * SnakePiece.SideSize, (rand.Next() % rows) * SnakePiece.SideSize); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { visit[i, j] = false; available.Add(i * cols + j); } } RandomFood(); visit[head.Location.Y / SnakePiece.SideSize, head.Location.X / SnakePiece.SideSize] = true; available.Remove(head.Location.Y / SnakePiece.SideSize * cols + head.Location.X / SnakePiece.SideSize); Controls.Add(head); snake[front] = head; }
public Game(int size) { if (size < 2) { throw new NotImplementedException("Game board must be at least 2 x 2."); } Size = size; SnakeLength = 1; IsOver = false; board = new Piece[size, size]; random = new Random(); int foodRow = random.Next(size); int foodCol = random.Next(size); food = new Food(new Position(foodRow, foodCol)); int snakeRow, snakeCol; do { snakeRow = random.Next(size); snakeCol = random.Next(size); } while (foodRow == snakeRow && foodCol == snakeCol); Orientation[] vals = Enum.GetValues(typeof(Orientation)) .OfType <Orientation>() .Where(o => o != Orientation.None) .ToArray(); Orientation direction = vals[random.Next(vals.Length)]; head = new SnakePiece { Direction = direction, Location = new Position(snakeRow, snakeCol) }; tail = head; ResetBoard(); }
void Movement() { foreach (ServerClient snake_player in client) { if (!snake_player.dead && snake_player.jumped) { Vector2 nextPos = new Vector2(0, 0); Vector2 tmp = snake_player.nextpos; switch (snake_player.direction) { case 0: nextPos = new Vector2(tmp.x, tmp.y + float.Parse(movmentSpeed.ToString())); break; case 1: nextPos = new Vector2(tmp.x + float.Parse(movmentSpeed.ToString()), tmp.y); break; case 2: nextPos = new Vector2(tmp.x, tmp.y - float.Parse(movmentSpeed.ToString())); break; case 3: nextPos = new Vector2(tmp.x - float.Parse(movmentSpeed.ToString()), tmp.y); break; } if (snake_player.clength == 0) { snake_player.pos = new List <SnakePiece>(); } if (snake_player.length != snake_player.clength) { SnakePiece newpiece = new SnakePiece(); newpiece.pos = nextPos; newpiece.nr = snake_player.clength; snake_player.pos.Add(newpiece); snake_player.clength++; } List <SnakePiece> cache = new List <SnakePiece>(); SnakePiece cache2 = new SnakePiece(); cache2.pos = nextPos; cache2.nr = 0; int x = 1; foreach (SnakePiece v2 in snake_player.pos) { SnakePiece temp = v2; cache.Add(cache2); cache2 = temp; cache2.nr = x; x++; } snake_player.pos = cache; snake_player.nextpos = nextPos; Update_Client.Add(snake_player); BroadcastToAll("PlayerMovement|" + snake_player.id + "|" + snake_player.nextpos.x + "|" + snake_player.nextpos.y); if (food_list.Count < 1000) { SpawnFood(); } } } startPlayerUpdate(); if (!hightickrate) { foreach (ServerClient snake_player in client) { if (!snake_player.dead && snake_player.spawned) { foreach (ServerClient snake_player2 in client) { if (!snake_player2.dead && snake_player.spawned) { try { foreach (SnakePiece sp in snake_player.pos) { foreach (SnakePiece sp2 in snake_player2.pos) { if (snake_player.id == snake_player2.id) { if (sp.nr != sp2.nr) { if (sp.pos == sp2.pos) { setPlayerDead(snake_player.id); } } } else { if (sp.pos == sp2.pos) { if (sp.nr < sp2.nr) { setPlayerDead(snake_player.id); } else { setPlayerDead(snake_player2.id); } } } } } } catch (System.Exception) { } } } } } } startPlayerUpdate(); }
private void UpdateGame() { while (true) { lock (board) { Position directionPosition = Position.FromOrientation(head.Direction); Position newSnakePosition = Position.Mod(head.Location, directionPosition, Size); Piece piece = board[newSnakePosition.Row, newSnakePosition.Col]; if (piece is SnakePiece) { // the snake has crashed !!! IsOver = true; } else if (piece is Food) { SnakePiece newTail = new SnakePiece( tail, Position.Mod(tail.Location, Position.FromReverseOrientation(tail.Direction), Size) ); tail.Child = newTail; tail = newTail; SnakeLength++; } ResetBoard(); board[food.Location.Row, food.Location.Col] = food; SnakePiece curr = head; Position oldPosition = null; Orientation oldDirection = Orientation.None; while (curr != null) { if (curr.Parent == null) { oldPosition = curr.Location; oldDirection = curr.Direction; curr.Location = newSnakePosition; } else { Position tempPosition = curr.Location; Orientation tempDirection = curr.Direction; curr.Direction = oldDirection; curr.Location = oldPosition; oldPosition = tempPosition; oldDirection = tempDirection; } board[curr.Location.Row, curr.Location.Col] = curr; curr = curr.Child; } if (piece is Food) { int row, col; do { row = random.Next(Size); col = random.Next(Size); } while (!(board[row, col] is Blank)); food.Location = new Position(row, col); board[row, col] = food; } PrintBoard(); } if (IsOver) { break; } Thread.Sleep(SLEEP); } }
public bool IsSamePositionAs(SnakePiece other) { return(other != null && X == other.X && Y == other.Y); }