Realization of greedy snake gradual movement effect _c language

Source: Internet
Author: User

The example of this article for everyone to share the realization of the Snake-C + + implementation of the gradual movement of the code, for your reference, the specific content as follows

Mainly uses the structure array and the structure pointer, then uses the Easyx Graph library to carry on the drawing, for discussion and the study

#include <conio.h>//Create a structure to store the snake body struct snack {int x;
 
int y;
};
Decision Direction int fangxiang=3;//w=0,s=1,a=2,d=3//key variable char anjian=0;
adjust coordinate relationship int Tiaozheng (struct snack *a);
moving function int Move (struct snack *a);
Print image int print (struct snack *a);
 
 
speed int v=10;  
 
  int main () {Initgraph (640, 480);
  struct snack na[10] ={0};
      while (anjian!= ' Q ') {if (Kbhit ()) {Anjian=getch ();
        Switch (Anjian) {//w=0,s=1,a=2,d=3 case ' W ': if (fangxiang!=1) fangxiang=0;
      Break
        Case ' s ': if (fangxiang!=0) fangxiang=1;
      Break
        Case ' a ': if (fangxiang!=3) fangxiang=2;
      Break
        Case ' d ': if (fangxiang!=2) fangxiang=3;
      Break
      Default:break;
      }//Clear screen function cleardevice ();
      Move (NA);
      Print (NA);
      Avoid the program running too Fast Sleep (60);
     
  Tiaozheng (NA);     
  } closegraph (); return 0; The int tiaozheng (struct snack *a) {//is passed as the last element, followed by a coordinate pass for (int i=9;i>0;i--) {a[i].x=a[i-
    1].x;  
    A[I].Y=A[I-1].Y;
 
return 0; int print (struct snack *a) {for (int i=0;i<10;i++) {//Drawing Putpixel (A[i].x,a[i].y,rgb
    -10*i));
  Circle (a[i].x,a[i].y,5);
return 0;
        int move (struct snack *a) {//w=0,s=1,a=2,d=3 switch (Fangxiang) {//Key response and reset case 0 for points outside the interface:
        A[0].y=a[0].y-v;
        if (a[0].y<0) a[0].y=480;
      Break
        Case 1:a[0].y=a[0].y+v;
        if (a[0].y>480) a[0].y=0;
      Break
        Case 2:a[0].x=a[0].x-v;
        if (a[0].x<0) a[0].x=640;
      Break
        Case 3:a[0].x=a[0].x+v;
        if (a[0].x>640) a[0].x=0;
      Break
  Default:break;
return 0;
 }

Due to limited time, did not consider the use of new to achieve the length of the snake body controllable, using only the initial value to achieve, followed by the use of global variables, the overall more messy, I hope you find other problems, welcome to point out!!!

The above is the entire content of this article, I hope to help you learn.

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.