C + + based on the console implementation of the greedy Snake games _c language

Source: Internet
Author: User
Tags rand

This example describes the C + + based on the console implementation of the greedy snake games. Share to everyone for your reference. The implementation method is as follows:

#include <windows.h> #include <time.h> #include <stdio.h> #define MAX #define UP 1 #define DOW
N 2 #define LEFT 3 #define RIGHT 4 #define MOVING 5 #define STOP 0 HANDLE hmain_out = NULL;
HANDLE hmain_in = NULL;
 struct Pos {int x;
int y;
};
 struct body {int state;
 int Len;
int Direction;
int ZZZZ;
 int HHHH;
Pos Pos[max];
};
Pos Newpos[max];
Pos Food;
Small_rect Wall;
int count = 0;
int grade = 0;
int level = 1;
int amount = 0;
int speed = 200;
void Init (body &b);
void Print (const body &b);
void Print (int x,int y);
void Move (body &b);
void clean (int x,int y);
void clean (the Const body &b);
void Showinfo ();
int getdirection (body &b);
void turnround (int direction,body &b);
void Poscopy (Body &b,pos newpos[]);
void Movebody (body &b);
void Hidecursor ();
void Createwall ();
void Createfood ();
BOOL Isknock_food (const body &b);
BOOL Isknock_wall (const body &b);

void Addbody (body &b);
 int main () {body B; IniT (b);
 Print (b);
 Hidecursor ();
  while (TRUE) {sleep (speed);
  Move (b);
 Getdirection (b);
return 0;
 } void Init (Body &b) {B.len = 3;
 B.direction = right;
 B.state = STOP;
 b.pos[0].x = 2;
 B.POS[0].Y = 1;
 b.pos[1].x = 4;
 B.POS[1].Y = 1;
 b.pos[2].x = 6;
 B.POS[2].Y = 1;
 Hmain_out = GetStdHandle (Std_output_handle);
 hmain_in = GetStdHandle (Std_input_handle);
 Createwall ();
 Createfood ();
Showinfo ();
 } void Print (const body &b) {COORD COORD; for (int ix = B.len-1;ix >= 0;--ix) {coord.
  X = b.pos[ix].x; Coord.
  Y = B.POS[IX].Y;
  SetConsoleCursorPosition (Hmain_out,coord);
 printf ("");
 } void Move (Body &b) {showinfo ();
  if (Isknock_wall (b)) {MessageBox (NULL, "You Are dead!", "Oh my God", 0);
 Exit (0);
   } if (Isknock_food (b)) {if (Amount > 5) {++level;
   Amount = 0;
  speed-= 50;
  } addbody (b);
  Grade + 10;
  ++amount;
  Clean (FOOD.X,FOOD.Y);
 Createfood (); } if (STOP = = b.state) {if (right = = B.direction) {for (int ix = 0;ix < B.len;++ix) {clean (B.POS[IX].X,B.POS[IX].Y);
   b.pos[ix].x+=2;
    } if (up = = B.direction) {for (int ix = 0;ix < B.len;++ix) {clean (B.POS[IX].X,B.POS[IX].Y);
   b.pos[ix].y--;
    } if (down = = B.direction) {for (int ix = 0;ix < B.len;++ix) {clean (B.POS[IX].X,B.POS[IX].Y);
   b.pos[ix].y++;
    } if (left = = b.direction) {for (int ix = 0;ix < B.len;++ix) {clean (B.POS[IX].X,B.POS[IX].Y);
   b.pos[ix].x-=2;
  }} if (moving = b.state) {poscopy (b,newpos);
    if (up = = B.direction) {if (B.len = = count) {b.state = STOP;
    B.direction = up;
   Count = 0;
    } if (Count < B.len && moving = = b.state) {b.pos[b.len-1].y--;
    Clean (B.POS[0].X,B.POS[0].Y);
    Movebody (b);
   Print (b);
    } if (down = = b.direction) {if (B.len = count) {b.state = STOP;
    B.direction = down;
   Count = 0; } if (Count < B.len && moving = = B.STate) {b.pos[b.len-1].y++;
    Clean (B.POS[0].X,B.POS[0].Y);
    Movebody (b);
   Print (b);
    } if (left = = b.direction) {if (B.len = = count) {b.state = STOP;
    B.direction = left;
   Count = 0;
    } if (Count < B.len && moving = = b.state) {b.pos[b.len-1].x-=2;
    Clean (B.POS[0].X,B.POS[0].Y);
    Movebody (b);
   Print (b);
    } if (right = = B.direction) {if (B.len = = count) {b.state = STOP;
    B.direction = right;
   Count = 0;
    } if (Count < B.len && moving = = b.state) {b.pos[b.len-1].x+=2;
    Clean (B.POS[0].X,B.POS[0].Y);
    Movebody (b);
   Print (b);
}} Print (b);
 } void Clean (int x,int y) {COORD C;
 c.x = X;
 C.y = Y;
 SetConsoleCursorPosition (HMAIN_OUT,C);
printf ("");}
 void Clean (const body &b) {for (int ix = 0;ix < B.len;++ix) {clean (B.POS[IX].X,B.POS[IX].Y);
  } int getdirection (body &b) {if (Getasynckeystate (vk_up)) {count = 0; Turnround(UP,B);
  } if (Getasynckeystate (Vk_down)) {count = 0;
 Turnround (DOWN,B);
  } if (Getasynckeystate (Vk_left)) {count = 0;
 Turnround (LEFT,B);
  } if (Getasynckeystate (vk_right)) {count = 0;
 Turnround (RIGHT,B);
return 0; } void turnround (int d,body &b) {switch (d) {case Up:if (right = = B.direction | |
   left = = b.direction) {poscopy (b,newpos);
   --B.POS[B.LEN-1].Y;
   Clean (B.POS[0].X,B.POS[0].Y);
   Movebody (b);
   Print (b);
   B.direction = D;
  B.state = moving;
 } break; Case Down:if (right = = B.direction | |
   left = = b.direction) {poscopy (b,newpos);
   ++B.POS[B.LEN-1].Y;
   Clean (B.POS[0].X,B.POS[0].Y);
   Movebody (b);
   Print (b);
   B.direction = D;
  B.state = moving;
 } break; Case Left:if (up = = B.direction | |
   down = = b.direction) {poscopy (b,newpos);
   b.pos[b.len-1].x-=2;
   Clean (B.POS[0].X,B.POS[0].Y);
   Movebody (b);
   Print (b);
   B.direction = D;
  B.state = moving;
 } break; Case Right:if (up = = B.direction | |
   down = = b.direction) {poscopy (b,newpos);
   b.pos[b.len-1].x+=2;
   Clean (B.POS[0].X,B.POS[0].Y);
   Movebody (b);
   Print (b);
   B.direction = D;
  B.state = moving;
 } break;
 Default:break;
  } void Poscopy (Body &b,pos newpos[]) {for (int ix = 0;ix < B.len;++ix) {newpos[ix].x=0;
 newpos[ix].y=0;
 for (int ix = 0;ix <b.len;++ix) {Newpos[ix] = B.pos[ix];
 } void Movebody (Body &b) {for (int ix = B.LEN-1;IX > 0;--ix) {b.pos[ix-1] = Newpos[ix];
 } ++count;
Poscopy (B,newpos);
 } void Hidecursor () {Console_cursor_info INFO;
 Getconsolecursorinfo (Hmain_out,&info);
 info.bvisible = FALSE;
Setconsolecursorinfo (Hmain_out,&info);
 } void Createwall () {Console_screen_buffer_info INFO;
 Getconsolescreenbufferinfo (Hmain_out,&info);
 info.srwindow.right-=19;
 info.srwindow.bottom-=5;
 Wall = Info.srwindow;
  for (int i = 0;i <= info.srwindow.right;i+=2) {Print (i,info.srwindow.top); Print (I,info.srwindow.bottom);
  for (int y = 0;y <= info.srwindow.bottom;++y) {Print (0,y);
 Print (Info.srwindow.right,y);
 } void Print (int x,int y) {COORD C;
 c.x = X;
 C.y = Y;
 SetConsoleCursorPosition (HMAIN_OUT,C);
printf ("");
 } void Createfood () {srand (unsigned (NULL));
 unsigned x_t = rand_max/wall.right;
 unsigned y_t = Rand_max/wall.bottom;
  while (true) {int x = rand ()/x_t;
  int y = rand ()/y_t;
  Food.x = x-4;
  Food.y = y-4;
   if ((0 = food.x% 2) && (0 = food.y% 2)) {if (Food.x < 5) {food.x+=8;
   } if (food.y<5) {food.y+=8;
   } Print (FOOD.X,FOOD.Y);
  Break 
  } bool Isknock_food (const body &b) {if (b.pos[b.len-1].x = = food.x && b.pos[b.len-1].y== food.y) {
 return true;
 else {return false; } bool Isknock_wall (const body &b) {if (0 = b.pos[b.len-1].x | | 0 = = B.POS[B.LEN-1].Y | | Wall.right = = B.pos[b.len-1].x | |
 Wall.bottom = = b.pos[b.len-1].y) {return true; } Pos head = b.pos[b.len-1]; for (int ix = 0;ix <= b.len-3;++ix) {if (head.x = = b.pos[ix].x && Head.y = = b.pos[ix].y) {return true
  ;
return false;
 } void Showinfo () {COORD C;
 c.x = Wall.right + 2;
 C.Y = 3;
 SetConsoleCursorPosition (HMAIN_OUT,C);
 printf ("Score:%d", grade);
 c.y+=10;
 SetConsoleCursorPosition (HMAIN_OUT,C);

printf ("Degree of difficulty:%d", level);  } void Addbody (&b) {if (B.len < MAX) {if (up = = b.direction) {b.pos[b.len].y = B.pos[b.len-1].y-
   1;
   b.pos[b.len].x = b.pos[b.len-1].x;
  ++b.len;
   } if (down = = b.direction) {b.pos[b.len].y = b.pos[b.len-1].y + 1;
   b.pos[b.len].x = b.pos[b.len-1].x;
  ++b.len;
   } if (left = = b.direction) {b.pos[b.len].x = b.pos[b.len-1].x-2;
   B.pos[b.len].y = B.POS[B.LEN-1].Y;
  ++b.len;
   } if (right = = b.direction) {b.pos[b.len].x = b.pos[b.len-1].x + 2;
   B.pos[b.len].y = B.POS[B.LEN-1].Y;
  ++b.len; }
 }
}

The effect diagram looks like this:

I hope this article will help you with the C + + program design.

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.