Example #1
0
        public SLSnake(SLLevel curLevel)
            : base(curLevel)
        {
            CurLevel = curLevel;

            Pos = new Point(CurLevel.SnakeStartPosX * CurLevel.TileSize, CurLevel.SnakeStartPosY * CurLevel.TileSize);

            //Check if the Snake is animated and set necessary animation initialization
            Animated = CurLevel.AnimatedSnake;

            if (Animated)
            {
                _initSnakeAnimation();
                _initCollisionAnimation();
                SnakeUtils.InitAnimationInDict(AnimationsDict, "win", CurLevel.Animation.FilenameWin, CurLevel.Animation.WinFrames, CurLevel.LevelCanvas);
            }
            else
            {
                SnakeImage = SnakeUtils.LoadImage(CurLevel.FilenameHead);
                CurLevel.LevelCanvas.Children.Add(SnakeImage);
            }

            SnakeImage.SetValue(Canvas.LeftProperty, Pos.X);
            SnakeImage.SetValue(Canvas.TopProperty, Pos.Y + CurLevel.SnakeOffsetY);
        }
Example #2
0
 public static void InitAnimationInDict(Dictionary <string, Image[]> AnimationsDict, string key, string filename, int frames, Canvas levelCanvas)
 {
     Image[] newImages = new Image[frames];
     for (int x = 0; x < frames; x++)
     {
         newImages[x]            = SnakeUtils.LoadImage(filename + "_" + (x + 1));
         newImages[x].Visibility = Visibility.Collapsed;
         levelCanvas.Children.Add(newImages[x]);
     }
     AnimationsDict.Add(key, newImages);
 }
Example #3
0
        private void RetireButton_Click(object sender, RoutedEventArgs e)
        {
            Buttons.Visibility      = Visibility.Collapsed;
            RetireButton.Visibility = Visibility.Collapsed;
            GameField.Children.Clear();
            Canvas retireCanvas = new Canvas();

            retireCanvas.Background = SnakeUtils.LoadBackgroundImage(SnakeUtils.RetireFile);
            retireCanvas.Width      = 200.0;
            retireCanvas.Height     = 200.0;
            GameField.Children.Add(retireCanvas);
        }
Example #4
0
        protected override void Shrink()
        {
            LinkedListNode <Tail>[] lastNodes = SnakeUtils.GetLastNodes(TailList, IsShrinking);

            for (int i = 0; i < lastNodes.Length; i++)
            {
                SLTail tail = (SLTail)lastNodes[i].Value;
                tail.TailImage.Visibility = Visibility.Collapsed;
            }

            base.Shrink();
        }
Example #5
0
        public override void OnShrinking(object sender, SnakeEventArgs e)
        {
            LinkedListNode <Tail>[] lastNodes = SnakeUtils.GetLastNodes(TailList, e.Value);

            for (int i = 0; i < lastNodes.Length; i++)
            {
                SLTail tail = (SLTail)lastNodes[i].Value;
                tail.IsShrinking = true;
            }

            base.OnShrinking(sender, e);
        }
Example #6
0
        public void _initCollisionAnimation()
        {
            _bufferedCollisionImage = new Image[CurLevel.Animation.CollisionFrames, 4];

            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < CurLevel.Animation.CollisionFrames; x++)
                {
                    _bufferedCollisionImage[x, y]            = SnakeUtils.LoadImage(CurLevel.Animation.FilenameCollision + "_" + (x + 1) + "_" + (y + 1));
                    _bufferedCollisionImage[x, y].Visibility = Visibility.Collapsed;
                    CurLevel.LevelCanvas.Children.Add(_bufferedCollisionImage[x, y]);
                }
            }
        }
Example #7
0
        protected override void ReadAppleTag(Level creationLevel_DC, XmlReader reader, Apple apple_DC)
        {
            SLLevel creationLevel = creationLevel_DC as SLLevel;
            SLApple apple         = apple_DC as SLApple;

            base.ReadAppleTag(creationLevel, reader, apple);

            reader.MoveToAttribute("img");
            string appleFilename = reader.Value;

            reader.MoveToAttribute("offsetY");
            apple.AppleOffsetY = Double.Parse(reader.Value);

            apple.AppleImage = SnakeUtils.LoadImage(appleFilename);
            creationLevel.LevelCanvas.Children.Add(apple.AppleImage);
        }
Example #8
0
        public void ShowWinner()
        {
            if (Animated)
            {
                _winAnimCount++;

                int frameNo = (_winAnimCount / CurLevel.Animation.WinDelay) % CurLevel.Animation.WinFrames;
                SnakeImage.Visibility = Visibility.Collapsed;
                SnakeImage            = AnimationsDict["win"][frameNo];
                SnakeImage.Visibility = Visibility.Visible;

                foreach (SLTail tail in TailList)
                {
                    tail.TailImage.Visibility = Visibility.Collapsed;
                    tail.TailImage            = tail.AnimationsDict["win"][frameNo];
                    tail.TailImage.Visibility = Visibility.Visible;
                }

                if (_winAnimCount >= CurLevel.TileSize)
                {
                    _winAnimCount = 0;
                }
            }
            else
            {
                if (_winAnimCount > 0)
                {
                    return;
                }

                SnakeImage.Visibility = Visibility.Collapsed;
                SnakeImage            = SnakeUtils.LoadImage(CurLevel.Animation.FilenameWin);
                CurLevel.LevelCanvas.Children.Add(SnakeImage);
                SnakeImage.Visibility = Visibility.Visible;

                foreach (SLTail tail in TailList)
                {
                    tail.TailImage.Visibility = Visibility.Collapsed;
                    tail.TailImage            = SnakeUtils.LoadImage(CurLevel.Animation.FilenameWin);
                    CurLevel.LevelCanvas.Children.Add(tail.TailImage);
                    tail.TailImage.Visibility = Visibility.Visible;
                }

                _winAnimCount++;
            }
        }
Example #9
0
        private void _initAnimation()
        {
            _bufferedImage = new Image[CurLevel.Animation.AnimationFrames, 4];

            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < CurLevel.Animation.AnimationFrames; x++)
                {
                    _bufferedImage[x, y]            = SnakeUtils.LoadImage(CurLevel.Animation.FilenameTail + "_" + (x + 1) + "_" + (y + 1));
                    _bufferedImage[x, y].Visibility = Visibility.Collapsed;
                    CurLevel.LevelCanvas.Children.Add(_bufferedImage[x, y]);
                }
            }

            TailImage            = BufferedImage[0, 0];
            TailImage.Visibility = Visibility.Visible;
        }
Example #10
0
        public override void SetToStart()
        {
            SnakeUtils.RemoveAnimationInDict(AnimationsDict, "win", CurLevel.LevelCanvas);
            SnakeUtils.RemoveAnimationInDict(AnimationsDict, "grow", CurLevel.LevelCanvas);
            SnakeUtils.RemoveAnimationInDict(AnimationsDict, "shrink", CurLevel.LevelCanvas);

            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < CurLevel.Animation.AnimationFrames; x++)
                {
                    _bufferedImage[x, y].Visibility = Visibility.Collapsed;
                    CurLevel.LevelCanvas.Children.Remove(_bufferedImage[x, y]);
                }
            }

            base.SetToStart();
        }
Example #11
0
        public void _initSnakeAnimation()
        {
            _bufferedImage = new Image[CurLevel.Animation.AnimationFrames, 4];

            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < CurLevel.Animation.AnimationFrames; x++)
                {
                    _bufferedImage[x, y]            = SnakeUtils.LoadImage(CurLevel.FilenameHead + "_" + (x + 1) + "_" + (y + 1));
                    _bufferedImage[x, y].Visibility = Visibility.Collapsed;
                    CurLevel.LevelCanvas.Children.Add(_bufferedImage[x, y]);
                }
            }

            /* Start with first animation frame */
            SnakeImage            = BufferedImage[0, 0];
            SnakeImage.Visibility = Visibility.Visible;
        }
Example #12
0
        protected override void ReadLevelTag(Level creationLevel_DC, XmlReader reader)
        {
            SLLevel creationLevel = creationLevel_DC as SLLevel;

            base.ReadLevelTag(creationLevel, reader);

            reader.MoveToAttribute("tileSize");
            creationLevel.TileSize = Int32.Parse(reader.Value);

            creationLevel.LevelCanvas.Width  = (double)(creationLevel.LevelWidth * creationLevel.TileSize);
            creationLevel.LevelCanvas.Height = (double)(creationLevel.LevelHeight * creationLevel.TileSize);

            reader.MoveToAttribute("background");
            creationLevel.LevelCanvas.Background = SnakeUtils.LoadBackgroundImage(reader.Value);

            reader.MoveToAttribute("borderBackground");
            creationLevel.BorderBackground = SnakeUtils.LoadBackgroundImage(reader.Value);
        }
Example #13
0
        protected override void ReadObjectTag(Level creationLevel_DC, XmlReader reader, LevelObject levObject_DC)
        {
            SLLevelObject levObject     = levObject_DC as SLLevelObject;
            SLLevel       creationLevel = creationLevel_DC as SLLevel;

            base.ReadObjectTag(creationLevel_DC, reader, levObject_DC);

            levObject.Pos = new Point((double)levObject.ObjectPosX * creationLevel.TileSize, (double)levObject.ObjectPosY * creationLevel.TileSize);

            reader.MoveToAttribute("offsetY");
            levObject.OffsetY = Double.Parse(reader.Value);

            reader.MoveToAttribute("filename");
            levObject.ObjectImage = SnakeUtils.LoadImage(reader.Value);

            levObject.ObjectImage.SetValue(Canvas.LeftProperty, levObject.Pos.X);
            levObject.ObjectImage.SetValue(Canvas.TopProperty, levObject.Pos.Y + levObject.OffsetY);
            creationLevel.LevelCanvas.Children.Add(levObject.ObjectImage);
        }
Example #14
0
        public override void SetToStart()
        {
            SnakeUtils.RemoveAnimationInDict(AnimationsDict, "win", CurLevel.LevelCanvas);
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < CurLevel.Animation.CollisionFrames; x++)
                {
                    _bufferedCollisionImage[x, y].Visibility = Visibility.Collapsed;
                    CurLevel.LevelCanvas.Children.Remove(_bufferedCollisionImage[x, y]);
                }
            }

            foreach (SLTail tail in TailList)
            {
                tail.SetToStart();
            }

            base.SetToStart();
        }
Example #15
0
        public SLTail(SnakeUtils.Directions direction, int tailPosX, int tailPosY, SLLevel curLevel)
            : base(direction, tailPosX, tailPosY, curLevel)
        {
            CurLevel = curLevel;
            _pos.X   = (double)(tailPosX * CurLevel.TileSize);
            _pos.Y   = (double)(tailPosY * CurLevel.TileSize);

            switch (direction)
            {
            case SnakeUtils.Directions.Up: _pos.Y += CurLevel.TileSize;
                break;

            case SnakeUtils.Directions.Right: _pos.X -= CurLevel.TileSize;
                break;

            case SnakeUtils.Directions.Down: _pos.Y -= CurLevel.TileSize;
                break;

            case SnakeUtils.Directions.Left: _pos.X += CurLevel.TileSize;
                break;
            }


            if (CurLevel.AnimatedSnake)
            {
                _initAnimation();
                SnakeUtils.InitAnimationInDict(AnimationsDict, "win", CurLevel.Animation.FilenameWin, CurLevel.Animation.WinFrames, CurLevel.LevelCanvas);
                SnakeUtils.InitAnimationInDict(AnimationsDict, "grow", CurLevel.Animation.FilenameGrow, CurLevel.Animation.GrowFrames, CurLevel.LevelCanvas);
                SnakeUtils.InitAnimationInDict(AnimationsDict, "shrink", CurLevel.Animation.FilenameShrink, CurLevel.Animation.ShrinkFrames, CurLevel.LevelCanvas);
            }
            else
            {
                TailImage = SnakeUtils.LoadImage(curLevel.Animation.FilenameTail);
                CurLevel.LevelCanvas.Children.Add(TailImage);
            }

            TailImage.SetValue(Canvas.LeftProperty, Pos.X);
            TailImage.SetValue(Canvas.TopProperty, Pos.Y + CurLevel.SnakeOffsetY);
        }
Example #16
0
        public void MoveSL()
        {
            if (NewDirection == SnakeUtils.Directions.Stop)
            {
                return;
            }

            if (_isNewTurn == true)
            {
                base.ChangeDirection();
                //if (!directionChanged && _keyBuffer.Count > 0)
                //    _keyBuffer.Clear();
                _UpdateKeyBuffer();
                _isNewTurn = false;

                ImminentCollision = base.CollisionImminent();
            }


            if (ImminentCollision != SnakeUtils.Collision.Wall && ImminentCollision != SnakeUtils.Collision.Object && ImminentCollision != SnakeUtils.Collision.Tail)
            {
                SnakeUtils.MoveSLPosition(OldDirection, ref _pos, 1.0);
                foreach (SLTail tail in TailList)
                {
                    tail.SLMoveTail(TailList);
                }
            }


            _tileCount++;

            if (_tileCount >= CurLevel.TileSize)
            {
                base.Move();
                SnakeUtils.SetZIndex(CurLevel, this, TailList);

                _isNewTurn = true;
                _tileCount = 0;
            }

            if (Animated)
            {
                int frameNo   = (_tileCount / CurLevel.Animation.AnimationDelay) % CurLevel.Animation.AnimationFrames;
                int frameColl = (_tileCount / CurLevel.Animation.CollisionDelay) % CurLevel.Animation.CollisionFrames;
                SnakeImage.Visibility = Visibility.Collapsed;
                if (ImminentCollision != SnakeUtils.Collision.Wall && ImminentCollision != SnakeUtils.Collision.Object && ImminentCollision != SnakeUtils.Collision.Tail)
                {
                    if (!CurLevel.IsControlInversed)
                    {
                        SnakeImage = BufferedImage[frameNo, ((int)this.OldDirection - 1)];
                    }
                    else
                    {
                        switch (this.OldDirection)
                        {
                        case SnakeUtils.Directions.Up: SnakeImage = BufferedImage[frameNo, 2]; break;

                        case SnakeUtils.Directions.Right: SnakeImage = BufferedImage[frameNo, 3]; break;

                        case SnakeUtils.Directions.Down: SnakeImage = BufferedImage[frameNo, 0]; break;

                        case SnakeUtils.Directions.Left: SnakeImage = BufferedImage[frameNo, 1]; break;
                        }
                    }


                    foreach (SLTail tail in TailList)
                    {
                        tail.Animate(_tileCount);
                    }
                }
                else
                {
                    //Add here Collision Animation, e.g. when hitting an object
                    SnakeImage = BufferedCollisionImage[frameColl, ((int)this.OldDirection - 1)];
                }
                SnakeImage.Visibility = Visibility.Visible;
            }
            else
            {
                if (ImminentCollision == SnakeUtils.Collision.Wall || ImminentCollision == SnakeUtils.Collision.Object || ImminentCollision == SnakeUtils.Collision.Tail)
                {
                    SnakeImage.Visibility = Visibility.Collapsed;
                    SnakeImage            = SnakeUtils.LoadImage("NutsTheme/collision_3_1");
                    CurLevel.LevelCanvas.Children.Add(SnakeImage);
                    SnakeImage.Visibility = Visibility.Visible;
                }
            }
        }
Example #17
0
 public void SLMoveTail(LinkedList <Tail> tailList)
 {
     SnakeUtils.MoveSLPosition(_oldDirection, ref _pos, 1.0);
 }