Beispiel #1
0
        public void Update(object source, ElapsedEventArgs e)
        {
            if (GameState == State.RUNNING)
            {
                // Check collison between snake and apple
                CordinatePair snakeHead = Snake.Head;
                if (snakeHead.X == Apple.X && snakeHead.Y == Apple.Y)
                {
                    Score += 1;
                    InitApple();

                    Snake.GrowBigger();
                }

                Snake.Update();

                // Check collision between head and tail
                List <CordinatePair> tail = Snake.Tail;
                foreach (CordinatePair snakeBit in tail)
                {
                    if (snakeHead.X == snakeBit.X && snakeHead.Y == snakeBit.Y)
                    {
                        GameOver();
                    }
                }

                // Check collision between snake and walls
                if (map.GetTileAt(Snake.Head.X, Snake.Head.Y) == 1)
                {
                    GameOver();
                }
            }
            Render();
        }
Beispiel #2
0
 public Snake(int x, int y)
 {
     direction = Direction.RIGHT;
     Tail      = new List <CordinatePair>();
     Head      = new CordinatePair {
         X = x, Y = y
     };
 }
Beispiel #3
0
        public SnakeGame(PictureBox pictureBox)
        {
            map          = new Map();
            Apple        = new CordinatePair();
            gameRenderer = new GameRenderer(pictureBox, this);
            InitNewSnake();
            InitApple();

            GameState      = State.NOT_STARTED;
            timer          = new System.Timers.Timer(120);
            timer.Elapsed += new ElapsedEventHandler(Update);
            timer.Start();
        }
Beispiel #4
0
        public void GrowBigger()
        {
            CordinatePair lastSnakeBit = numberOfTailElements != 0 ? Tail.Last() : Head;

            CordinatePair snakeBit = new CordinatePair
            {
                X = lastSnakeBit.X, Y = lastSnakeBit.Y
            };

            Tail.Add(snakeBit);

            numberOfTailElements += 1;
        }
Beispiel #5
0
        private void Move(int x, int y)
        {
            for (var i = numberOfTailElements - 1; i > -1; i -= 1)
            {
                CordinatePair tailElement = Tail[i];
                CordinatePair leadingTail = i == 0 ? Head : Tail[i - 1];

                tailElement.X = leadingTail.X;
                tailElement.Y = leadingTail.Y;
                // tailElement.Movable = true;
            }

            Head.X += x;
            Head.Y += y;
        }
Beispiel #6
0
        public void Render()
        {
            Font font = new Font("Arial", 14);

            // Clear screen
            backBufferGfx.DrawImage(wallsBitmap, 0, 0);

            // Render the snake
            if (snakeGame.GameState == SnakeGame.State.RUNNING)
            {
                Snake         snake = snakeGame.Snake;
                CordinatePair head  = snake.Head;
                backBufferGfx.FillRectangle(Brushes.Yellow, head.X * tileWidth,
                                            head.Y * tileHeight, tileWidth, tileHeight);
                var snakeBits = snake.Tail;
                foreach (CordinatePair snakeBit in snakeBits)
                {
                    backBufferGfx.FillRectangle(Brushes.Yellow, snakeBit.X * tileWidth,
                                                snakeBit.Y * tileHeight, tileWidth, tileHeight);
                }

                // Render the apple
                CordinatePair apple = snakeGame.Apple;
                backBufferGfx.FillEllipse(Brushes.Green, apple.X * tileWidth,
                                          apple.Y * tileHeight, tileWidth, tileHeight);
            }

            // Draw score
            backBufferGfx.DrawString("SCORE: " + snakeGame.Score, font, Brushes.Black, 2, 2);

            // Draw Game-over string, if we are game over
            if (snakeGame.GameState == SnakeGame.State.GAMEOVER)
            {
                backBufferGfx.DrawString("GAME OVER", font, Brushes.Red, 240, 200);
                backBufferGfx.DrawString("PRESS SPACE TO RESTART", font, Brushes.Red, 170, 260);
            }
            else if (snakeGame.GameState == SnakeGame.State.NOT_STARTED)
            {
                backBufferGfx.DrawString("PRESS SPACE TO START", font, Brushes.Green, 180, 225);
            }


            // Render to screen
            view.Image = backBufferBitmap;
        }