Snake-c-based on Easyx Graphics library (1): Basic control function for drawing program

Source: Internet
Author: User

Since learning C language, has always wanted to play a game, today will pay the action, the first write special rotten, all kinds of bug; Today on the Internet to see several greedy snake, re-write once, the effect can also be done.

Here is a detailed build process, this section because of time constraints, first put out the more important control function implementation, and use it to make a very simple very simple very interesting drawing program.

First of all, there is a general understanding of the structure of the snake, to have a control system to control the snake move up and down, and can not move in the opposite direction, to have food production system, food appears randomly, the snake will become longer after eating food, to have a death judgment system ...

To sum up, that's it:

The structure I wrote was a bit messy, but that's the way it is.

The failure of the first attempt tells me that I want to achieve a complete snake for a long time without touching c I, it is more difficult. So I'm going to do it slowly, I'm going to do a little game where I can move blocks in the window.

Specifically, only the control module "Snake".

Snakes have four moving directions, and we can use vectors to represent them.

Because the coordinate coordinate system of the screen is like this

So the vectors correspond to the directions as follows

Left ( -1,0)

Right (1,0)

On (0,-1)

Under (0,1)

We use "W a s D" as the control key, so we get the control function:

1 voidCommand ()//get keyboard Commands2 {3     if(_kbhit ())//If you have a keyboard message4         Switch(_getch ())/*There is no use of GetChar ()*/5         {6          Case 'a':7array.x =-1;8ARRAY.Y =0;9              Break;Ten          Case 'D': Onearray.x =1; AARRAY.Y =0; -              Break; -          Case 'W': thearray.x =0; -ARRAY.Y =-1; -              Break; -          Case 's': +array.x =0; -ARRAY.Y =1; +              Break; A         } at}

For simplicity, we assume that our snake has only one block and can move freely in four directions.

Then our move function can be written like this:

1 voidMove ()//Modify the head node coordinates to achieve the purpose of moving2 {3Snake.x + = array.x*Ten;//10pix per move4Snake.y + = array.y*Ten;5 6SetColor (BLUE);//Set Snake color7         //Draw a snake8Rectangle (snake.x-5, Snake.y-5, Snake.x +5, Snake.y +5); 9}

and write a driver .

1 #include <graphics.h>
2 #include <conio.h>
34typedefstructPosition//Coordinate structure5 {6 intx;7 inty;8 }pos;9 Ten Pos Snake; One Pos Array; A - intMain () - { thesnake.x = -; snake.y = -; -array.x =1; array.y =0;
Initgraph (640,480); Initializing the graphical interface - while(true) - { + command (); - move ();
Sleep (100); + } A return 0; at}

This small program is like a drawing program, and because there is no boundary to determine, he will always run to the place we do not see ~ ~ effect is as follows: (Doodle ~)

Well, I feel like I made a fun sandbox game ~ ~ ~

Let's get to know each other and come here today.

At the end of the attached I took the Begonia ~ ~

Snake-c-based on Easyx Graphics library (1): Basic control function for drawing program

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.