Automatic Snake Game (C)

Source: Internet
Author: User

I believe most of my friends do not have much difficulty in writing a Snake game, but it takes some time to realize the function of automatically eating beans for snakes.

Two obvious errors in the first version of my automatic game will lead to a dead end for the snake, or you cannot get out of the dead end.

Next we will introduce the Implementation ideas,

This isProgramOverall architecture:

This program can be manually or automatically switched

Frmgame is the game's main window. When the window is initialized, it fills up the control array of lsf-: list <label> In the window. To display snakes and beans, you only need to set the Enable attribute of the label control at the corresponding position to true, and set the other to false to display the label control. This method is easy to display in exchange for memory consumption.

Controlsnake is a class that controls snakes. It includes the width, height, bean position, target, snake body, and snags: List <point> of the game venue, and the direction of the snake: direction, and the game status gamestate. The methods include game initialization, determining whether a snake hits itself or hits a wall.

The function implemented by the AutoPlay class is the automatic game of snakes. The automatic bean eating method is as follows:

1. First, judge whether there is a wall or yourself in front of the Snake. If there is no wall in front of the snake, then the bean-eating action will be taken in the direction of the bean.

2. If you have a wall or yourself, you can avoid the avoiddead () operation. The so-called avoidance means turning in the direction of the snake, which may lead to a dead end. To prevent it from entering a dead end, this program temporarily uses the number of spaces enclosed by the body and wall of the snake on both sides of the side, the snake goes to the side with many spaces.

Before proceeding to the next step, please watch two videos. This is a problem in the game.

First question


Second question


The first problem is that when the snake moves forward, it may also enter a dead end. The improved method is to determine whether the previous step is a dead end when the snake goes forward.Code, If yes, it turns

The second problem is that I only have the approximate method to implement: When a snake cannot eat beans, it will change to a walking method (when we are playing a manual game, it is easy to think of using this method ). However, some problems need to be considered when to implement the program. The first is when to adopt a special route, the second is when to exit this special route, and the third is how to describe this route in the program.

I believe that if the second problem is solved, the snake can become longer, but as the length of the snake increases, it will certainly encounter new problems in the future, I hope to make this snake grow !!

This is the source code of my program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.