Snake-C-drawing program based on easyx graphics library (on): basic control function, easyx

Source: Internet
Author: User

Snake-C-drawing program based on easyx graphics library (on): basic control function, easyx

Since I learned the C language, I have always wanted to make a game. Today I am paying for it. The first time I wrote it, I wrote a lot of bugs. Today, I read several greedy snakes on the Internet and re-wrote them once. The effect is good.

 

The following is a detailed construction process. Due to time constraints, this section first posts an important control function implementation and uses it to create a very simple and interesting drawing program.

 

First, you should have a general understanding of the structure of the Snake. A control system should control the movement of the snake up and down, and cannot move in the opposite direction. There should be a food production system, where the food appears randomly; after eating food, the snake will become longer; there will be a death determination system ......

To sum up, it is like this:

 

 

The structure I wrote is a bit messy, but this is basically the case ~

 

The failure of the first attempt tells me that I want to implement a complete greedy snake. It is difficult for me who haven't touched c for a long time. So this time, I am going to start with a small game that can move squares in the window.

Specifically, there is only a "snake" in the control module.

A snake has four moving directions. We can use vectors to represent it.

Because the screen coordinate system is like this

Therefore, the vector and direction correspond to the following:

Left (-1, 0)

Right (1, 0)

Upper (0,-1)

Lower (0, 1)

 

We use "w a s d" as the control key to obtain the control function:

1 void command () // obtain the Keyboard command 2 {3 if (_ kbhit () // if there is a Keyboard Message 4 switch (_ getch ()) /* getchar () */5 {6 case 'A': 7 array. x =-1; 8 array. y = 0; 9 break; 10 case 'D': 11 array. x = 1; 12 array. y = 0; 13 break; 14 case 'W': 15 array. x = 0; 16 array. y =-1; 17 break; 18 case's ': 19 array. x = 0; 20 array. y = 1; 21 break; 22} 23}

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

Then our mobile function can be written as follows:

1 void move () // modify the coordinates of the header node to move 2 {3 snake. x + = array. x * 10; // move 10 pix4 snake each time. y + = array. y * 10; 5 6 setcolor (BLUE); // set the snake color 7 // draw the snake 8 rectangle (snake. x-5, snake. y-5, snake. x + 5, snake. y + 5); 9}

Then write a driver. 

1 # include <graphics. h> 
2 # include <conio. h>
3 4 typedef struct Position // coordinate structure 5 {6 int x; 7 int y; 8} Pos; 9 10 Pos snake; 11 Pos array; 12 13 int main () 14 {15 snake. x = 300; snake. y = 300; 16 array. x = 1; array. y = 0;
Initgraph (640,480); // initialize the graphical interface 17 while (true) 18 {19 command (); 20 move ();
Sleep (100); 21} 22 return 0; 23}

This small program is like a drawing program, and because there is no boundary judgment, he will keep running somewhere we can't see ~~ The effect is as follows: (garbled ~)

 

The complete code is as follows:

1 # include <graphics. h> 2 # include <conio. h> 3 4 typedef struct Position // coordinate structure 5 {6 int x; 7 int y; 8} Pos; 9 10 Pos snake; 11 Pos array; 12 13 void command () // obtain the Keyboard Command 14 {15 if (_ kbhit () // if there is a Keyboard Message 16 switch (_ getch ()/* getchar () cannot be used here () */17 {18 case 'A': 19 array. x =-1; 20 array. y = 0; 21 break; 22 case 'D': 23 array. x = 1; 24 array. y = 0; 25 break; 26 case 'W': 27 array. x = 0; 28 array. y =-1; 29 break; 30 case's ': 31 array. x = 0; 32 array. y = 1; 33 break; 34} 35} 36 37 void move () // modify the coordinates of the header node to move 38 {39 snake. x + = array. x * 10; // move 10 pix40 snake each time. y + = array. y * 10; 41 42 setcolor (BLUE); // set the snake color 43 // draw the snake 44 rectangle (snake. x-5, snake. y-5, snake. x + 5, snake. y + 5); 45} 46 47 int main () 48 {49 snake. x = 300; snake. y = 300; 50 array. x = 1; array. y = 0; 51 initgraph (640,480); // initialize the graphic interface 52 while (true) 53 {54 command (); 55 move (); 56 Sleep (100 ); 57} 58 return 0; 59}Plotting Program

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

Let's get started with it. Let's get there today.

I have attached my DRDs jellyfish to the end ~~

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.