Practiced hand WPF (four) - Snake game easy to achieve (at)

Original: practice hand WPF (four) - A simple and easy Snake game (next)

Eight, to generate a new single snake
with us first talk about the principles of good little game, after the game is running, continue to generate new class SnakeNode single snake through timer events, added to the List in the 0 position, the original smugglers into a sect. The coordinates of the new section snakehead snakehead Direct judged by the forward direction, i.e., if the original snakehead left running, the new left snakehead snakehead original position to generate a grid, so the other direction. Finally, add the section to SnakeNodes list and add the appropriate graphics to the game area.

private void GenNewSnakeNode()
{
    SnakeNode snakeNode = null;
    switch (Direct)
    {
        case Direction.UP:
            snakeNode = new SnakeNode(new Point(SnakeNodes[SnakeHead]._pos.X,
                SnakeNodes[SnakeHead]._pos.Y - 1));
            break;

        case Direction.DOWN:
            snakeNode = new SnakeNode(new Point(SnakeNodes[SnakeHead]._pos.X,
                SnakeNodes[SnakeHead]._pos.Y + 1));
            break;

        case Direction.LEFT:
            snakeNode = new SnakeNode(new Point(SnakeNodes[SnakeHead]._pos.X - 1,
                SnakeNodes[SnakeHead]._pos.Y));
            break;

        case Direction.RIGHT:
            snakeNode = new SnakeNode(new Point(SnakeNodes[SnakeHead]._pos.X + 1,
                SnakeNodes[SnakeHead]._pos.Y));
            break;
    }

    if (snakeNode != null)
    {
        SnakeNodes.Insert(0, snakeNode);
        myCanvas.Children.Add(SnakeNodes[0]._rect);
    }
}

 

Nine, fruits random position
Note the new location must not overlap with any of a snake, if it overlaps another to generate a new position, until it succeeds.

private Point SetFruitToRandomPos()
{
    bool flag = true;
    Point pos = new Point();
    while (flag)
    {
        flag = false;
        pos = new Point(rnd.Next(0, CellWidth - 1), rnd.Next(0, CellHeight - 1));
        foreach (var node in SnakeNodes)
        {
            if (pos.X == node._pos.X && pos.Y == node._pos.Y)
            {
                flag = true;
                break;
            }
        }
    }

    return pos;
}

 

Ten, collision detection (snakeheads and fruit)
because only the smugglers will be the first to touch the fruit, so just judge smugglers coordinate with fruit coordinates are the same as can be. If you hit the fruit, the fruit will be randomly generates a new position; if not met, remove the tail section (since then will generate a new one by smugglers timer, this will save the snake length, feeling a forward position grid).

private void CheckCollision()
{
    if (SnakeNodes[SnakeHead]._pos.X == fruit._pos.X && SnakeNodes[SnakeHead]._pos.Y == fruit._pos.Y)
    {
        fruit.SetPostion(SetFruitToRandomPos());
    }
    else
    {
        if (myCanvas.Children.Contains(SnakeNodes[SnakeNodes.Count - 1]._rect))
            myCanvas.Children.Remove(SnakeNodes[SnakeNodes.Count - 1]._rect);

        SnakeNodes.RemoveAt(SnakeNodes.Count - 1);
    }
}

 

Eleven, to determine whether the end of the game
is very simple, to see whether any touch a snake head snake or four sides of the playing field, and if it returns true.
Note that the snake is from the beginning of the cycle, starting from 0 if you know what would happen.

        private bool IsGameOver()
        {
            if (SnakeNodes[SnakeHead]._pos.X == -1 || SnakeNodes[SnakeHead]._pos.X == CellWidth
                || SnakeNodes[SnakeHead]._pos.Y == -1 || SnakeNodes[SnakeHead]._pos.Y == CellHeight)
            {
                return true;
            }

            foreach (var node in SnakeNodes)
            {
                if (node == SnakeNodes[SnakeHead])
                    continue;

                if (node._pos.X == SnakeNodes[SnakeHead]._pos.X && node._pos.Y == SnakeNodes[SnakeHead]._pos.Y)
                {
                    return true;
                }
            }

            return false;
        }

 

XIII, delete all the games area snake festival
that will be used to re-start the game when:

private void RemoveSnakeNodeAll()
{
    for (int i = 0; i < SnakeNodes.Count; i++)
    {
        if (myCanvas.Children.Contains(SnakeNodes[i]._rect))
        {
            myCanvas.Children.Remove(SnakeNodes[i]._rect);
        }
    }
}

 

Incidentally come out of the game area to delete fruits:

private void RemoveFruit()
{
    if (fruit == null) 
    {
        return;
    }

    if (myCanvas.Children.Contains(fruit._ellipse))
    {
        myCanvas.Children.Remove(fruit._ellipse);
    }
}

 

Fourth, a key operation
by the direction keys provided Direct value.

private void MyCanvas_PreviewKeyDown(object sender, KeyEventArgs e)
{
    switch (e.Key)
    {
        case Key.Left:
            if (Direct != Direction.RIGHT)
            {
                Direct = Direction.LEFT;
            }
            break;

        case Key.Right:
            if (Direct != Direction.LEFT)
            {
                Direct = Direction.RIGHT;
            }
            break;

        case Key.Up:
            if (Direct != Direction.DOWN)
            {
                Direct = Direction.UP;
            }
            break;

        case Key.Down:
            if (Direct != Direction.UP)
            {
                Direct = Direction.DOWN;
            }
            break;

        case Key.Escape:
            Application.Current.Shutdown();
            break;

        case Key.Space:
            if (CurrGameState == GameState.NONE)
                return;

            if (CurrGameState == GameState.PAUSE)
            {
                CurrGameState = GameState.GAMEING;
                timer.Start ();
                MenuControl_Pause.Header = "暂停";
            }
            else if (CurrGameState == GameState.GAMEING)
            {
                CurrGameState = GameState.PAUSE;
                timer.Stop ();
                MenuControl_Pause.Header = "继续";
            }
            break;
    }
}

 

Fourth, start the game defined method
to delete the interface and snake fruit (reserved concealed bottom), generating a random position and three snake fruit.

private  void Start Game ()
{
    RemoveSnakeNodeAll();
    RemoveFruit();

    int startX = rnd.Next(5, CellWidth - 6);
    int startY = rnd.Next(5, CellHeight - 6);
    Direct = Direction.RIGHT;

    fruit = new Fruit(SetFruitToRandomPos(), myCanvas);

    SnakeNodes = new List<SnakeNode>();
    SnakeNodes.Add(new SnakeNode(new Point(startX, startY)));
    GenNewSnakeNode ();
    GenNewSnakeNode ();
}

 

Fifth, to start the game menu

Call StartGame () method, and set the timer starts.

private void MenuFile_NewGame_Click(object sender, RoutedEventArgs e)
{
    Start Game ();
    timer.Start ();
    CurrGameState = GameState.GAMEING;
    MenuControl_Pause.Header = "暂停";
}

 

Sixteen, pause and continue
directly on the code, not to say.

private void MenuControl_Pause_Click(object sender, RoutedEventArgs e)
{
    if (CurrGameState == GameState.GAMEING)
    {
        CurrGameState = GameState.PAUSE;
        timer.Stop ();
        MenuControl_Pause.Header = "继续";
    }
    else if (CurrGameState == GameState.PAUSE)
    {
        CurrGameState = GameState.GAMEING;
        timer.Start ();
        MenuControl_Pause.Header = "暂停";
    }
}

 

XVII quit the game

private void MenuFile_Exit_Click(object sender, RoutedEventArgs e)
{
    Application.Current.Shutdown();
}

 

Eighteenth and other menu functions
to achieve their own good

private void MenuHelp_About_Click(object sender, RoutedEventArgs e)
{
}

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11019997.html