// Use this for initialization void Start() { Head = new SnakeElement(); Head.Element = HeadElement; Head.Direction = Direction; Head.Speed = Speed; Head.TargetPoint = Head.Element.transform.localPosition+Direction*Speed*TimeBetweenChangeDirection; Body = new List<SnakeElement>(); foreach(var t in Elements) { var element = new SnakeElement { Direction = Direction, Element = t, Speed = Speed }; Body.Add(element); } Body[0].TargetPoint =Head.Element.transform.localPosition; for(int i=1;i<Body.Count;i++) { Body[i].TargetPoint = Body[i-1].Element.transform.position; } LastUpdateTime = Time.time; HeadDirection = Direction; }
private void CheckColisionWitchSelf() { SnakeElement snakeHead = GetSnakeHead(); bool hasCollision = false; if (snakeHead != null) { foreach (var snakeElement in snakeElements) { if (!snakeElement.IsHead) { if (snakeElement.X == snakeHead.X && snakeElement.Y == snakeHead.Y) { hasCollision = true; break; } } } } if (hasCollision) { if (points > 1 && points < 5) { MessageBox.Show("Wąż uderzył głową w samego siebie, koniec gry. Zdobyto " + points + " punkty. Kliknij OK, by zagrać ponownie."); } else { MessageBox.Show("Wąż uderzył głową w samego siebie, koniec gry. Zdobyto " + points + " punktów. Kliknij OK, by zagrać ponownie."); } ResetGame(); InitializeGame(); } }
private void CheckColisionWitchWorldItems() { if (apple == null) { return; } SnakeElement head = snakeElements[0]; if (head.X == apple.X && head.Y == apple.Y) { pictureNumer = RandomNumber.Next(0, 4); while (pictureNumer == oldPictureNumer) { pictureNumer = RandomNumber.Next(0, 4); } oldPictureNumer = pictureNumer; BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = new Uri(listUrl[pictureNumer], UriKind.Absolute); src.EndInit(); SnakePicture.Source = src; GameWorld.Children.Remove(apple.UIElement); GrowSnake(); apple = null; MakeGameFaster(); points++; } }
public void ChangeDirectionThrowsArgumentOutOfRangeExceptionWhenInvalidEnumValueTest() { // arrange var snakeElement = new SnakeElement(1, 1, DirectionOfMove.Up); // act // assert Assert.Throws <ArgumentOutOfRangeException>(() => snakeElement.ChangeDirection((DirectionOfMove)100)); }
void SmoothMovement() { transform.Translate(0, 0, 2 / movementSmoothing); for (int i = 1; i < tailList.Count; i++) { SnakeElement temp = tailList[i]; temp.segment.transform.Translate((tailList[i - 1].lastPosition - temp.lastPosition) / movementSmoothing); } }
// Start is called before the first frame update void Start() { // Add the first existing element to our snake queue //snake.Add(new SnakeElement(firstSnakeElement, DIRECTION.UP)); SnakeElement snakeElement = firstSnakeElement.GetComponent <SnakeElement>(); snakeElement.direction = DIRECTION.UP; snake.Add(snakeElement); ChangeDirection(DIRECTION.RIGHT); }
private bool CollisionWithApple() { if (Apple == null || Snake == null || Snake.Head == null) { return(false); } SnakeElement head = Snake.Head; return(head.X == Apple.X && head.Y == Apple.Y); }
// Update is called once per frame /*<summary> * Update is responsible for movement, ar for instantiation of special food. * </summary> */ void Update() { //if game is over don't want snake to move if (GameManager.instance.gameOver) { return; } //to be sure if game could be continued if for some reason food was not created if (GameObject.FindGameObjectsWithTag("NormalFood").Length == 0) { foodPrefab.GetComponent <Food>().AddNewOne(); } //counting time to instantiate new special food object timeToNewSpecialFood -= Time.deltaTime; if (timeToNewSpecialFood <= 0) // if all time has passed { //create new special food object SpecialFood sf = specialFoodPrefab.GetComponent <SpecialFood>(); sf.AddNewOne(); //random new time to create new special food object //range is selected to not create new object before old one is not expired timeToNewSpecialFood = Random.Range(2 * sf.timeToDestroy, 2 * sf.timeToDestroy + maxTimeToNewSpecialFood); } //if there is not a time for a next move if (Time.time < nextFrameTime) { return; } nextFrameTime += interval; // set up a new move time SnakeElement.MoveTail(snake); //firstly move a snake tail SnakeHead head = snake[0] as SnakeHead; // head will always be firs member //check if rotation is set up if (isTurningLeft) { isTurningLeft = false; head.RotateHeadCounterClockwise(); } else if (isTurningRight) { isTurningRight = false; head.RotateHeadClockwise(); } SnakeElement.MoveHead(snake); // move head including rotation }
public void AddElement(GameObject obj) { var element = new SnakeElement { Direction = Direction, Element = obj, Speed = Speed }; element.Direction = Body[Body.Count-1].Direction; element.TargetPoint = Body[Body.Count-1].Element.transform.localPosition; Body.Add(element); }
public void MoveLeftTest() { // arrange const int expected = 0; var snakeElement = new SnakeElement(1, 1, DirectionOfMove.Left); // act snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DistanceFromLeft); }
public void MoveDownTest() { // arrange const int expected = 1; var snakeElement = new SnakeElement(1, 0, DirectionOfMove.Down); // act snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DistanceFromTop); }
public void ChangeDirectionToLeftWhenCurrentIsRightTest() { // arrange const DirectionOfMove expected = DirectionOfMove.Right; var snakeElement = new SnakeElement(1, 1, DirectionOfMove.Right); // act snakeElement.ChangeDirection(DirectionOfMove.Left); snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DirectionOfMove); }
public void ChangeDirectionToUpValidConditionsTest() { // arrange const DirectionOfMove expected = DirectionOfMove.Up; var snakeElement = new SnakeElement(1, 1, DirectionOfMove.Right); // act snakeElement.ChangeDirection(expected); snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DirectionOfMove); }
public void ChangeDirectionToDownWhenCurrentIsUpTest() { // arrange const DirectionOfMove expected = DirectionOfMove.Up; var snakeElement = new SnakeElement(1, 1, DirectionOfMove.Up); // act snakeElement.ChangeDirection(DirectionOfMove.Down); snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DirectionOfMove); }
public void AddElement(GameObject obj) { var element = new SnakeElement { Direction = Direction, Element = obj, Speed = Speed }; element.Direction = Body[Body.Count - 1].Direction; element.TargetPoint = Body[Body.Count - 1].Element.transform.localPosition; Body.Add(element); }
public void ChangeDirectionToDownValidConditionsTest() { // arrange const DirectionOfMove expected = DirectionOfMove.Down; var snakeElement = new SnakeElement(0, 0, DirectionOfMove.Left); // act snakeElement.ChangeDirection(expected); snakeElement.Move(); // assert Assert.AreEqual(expected, snakeElement.DirectionOfMove); }
private SnakeElement GetSnakeHead() { SnakeElement snakeHead = null; foreach (var snakeElement in snakeElements) { if (snakeElement.IsHead) { snakeHead = snakeElement; break; } } return(snakeHead); }
void UpdateSnake() { transform.rotation = currDirection; for (int i = 0; i < tailList.Count; i++) { SnakeElement temp = tailList[i]; temp.lastPosition = temp.segment.transform.position; tailList[i] = temp; } if (food > 0) { tailList.Add(new SnakeElement(Instantiate(tailPrefab, tailList[tailList.Count - 1].segment.transform.position, Quaternion.identity, snakeParent.transform), tailList[tailList.Count - 1].segment.transform.position)); food--; } }
public SnakeViewModel() { snakeHead = new SnakeElement(0, 0, 20, 20); collisionManager = new CollisionManager(); Timer = new Timer(); Timer.dispatcherTimer.Start(); Locator = new LocatorViewModel(); MovementManager = new MovementManager(this, Locator.Food); leftCommand = new RelayCommand(LeftAction, () => true); rightCommand = new RelayCommand(RightAction, () => true); upCommand = new RelayCommand(UpAction, () => true); downCommand = new RelayCommand(DownAction, () => true); GuiTail = new ObservableCollection <SnakeElement>(); }
public void CreateSnakeElementBehindDirectionIsDownTest() { // arrange const DirectionOfMove expectedDirection = DirectionOfMove.Down; const int expectedDistanceFromLeft = 1; const int expectedDistanceFromTop = 1; var snakeElement = new SnakeElement(expectedDistanceFromLeft, expectedDistanceFromTop + 1, expectedDirection); // act var newSnakeElement = snakeElement.CreateSnakeElementBehind(); // assert Assert.Multiple(() => { Assert.AreEqual(expectedDistanceFromLeft, newSnakeElement.DistanceFromLeft); Assert.AreEqual(expectedDistanceFromTop, newSnakeElement.DistanceFromTop); Assert.AreEqual(expectedDirection, newSnakeElement.DirectionOfMove); }); }
// Start is called before the first frame update /*<summary> * Starts creating a new gamefield, and creating first snake. * Then it's adjusting an game speed interval, and special food time. * </summary> */ void Start() { //to be sure if it's set up GameManager.instance.gameOver = false; //interval from options interval = GameManager.instance.GameSpeed; nextFrameTime = Time.time + interval; timeToNewSpecialFood = Random.Range(0f, maxTimeToNewSpecialFood); //generating time to instantiate a new special food object //generate GameField Field field = fieldPrefab.gameObject.GetComponent <Field>(); field.CreateField(); //create wall; Wall wall = wallPrefab.gameObject.GetComponent <Wall>(); wall.BuildWall(field); //snake initialization snake = new List <SnakeElement>(); SnakeElement head = headPrefab.gameObject.GetComponent <SnakeHead>(); head.AddElement(snake); for (int i = 0; i < 4; i++) { SnakeElement tail = tailPrefab.gameObject.GetComponent <SnakeTail>(); tail.AddElement(snake); SnakeElement.MoveTail(snake); SnakeElement.MoveHead(snake); } //food creation Food food = foodPrefab.gameObject.GetComponent <Food>(); food.AddNewOne(); }
void ResetGame() { if (gameLoopTimer != null) { gameLoopTimer.Stop(); gameLoopTimer.Tick -= MainGameLoop; gameLoopTimer = null; } if (GameWorld != null) { GameWorld.Children.Clear(); } apple = null; if (snakeElements != null) { snakeElements.Clear(); snakeElements = null; } tailBackup = null; points = 0; }
private void MoveSnake() { SnakeElement head = snakeElements[0]; SnakeElement tail = snakeElements[snakeElements.Count - 1]; tailBackup = new SnakeElement(elementSize) { X = tail.X, Y = tail.Y }; head.IsHead = false; tail.IsHead = true; tail.X = head.X; tail.Y = head.Y; switch (currentDirection) { case Direction.Right: tail.X += elementSize; break; case Direction.Left: tail.X -= elementSize; break; case Direction.Up: tail.Y -= elementSize; break; case Direction.Down: tail.Y += elementSize; break; default: break; } snakeElements.RemoveAt(snakeElements.Count - 1); snakeElements.Insert(0, tail); }
public Snake(Position SendPosition) { Head = new SnakeElement(SendPosition); Direction = Direction.Up; }
// First initializing of the snake static void InitializeSnake() { var headPositionRow = snake.ElementAt(0).Row; var headPositionCol = snake.ElementAt(0).Col; for (int i = 1; i < 5; i++) { var part = new SnakeElement(new Position(headPositionRow + i, headPositionCol), '*'); snake.Enqueue(part); } }