Recently learned ncurses, with the snake training
Idea: Do not construct the linked list, the snake head to the direction of the forward point, the tail of the snake disappears, the formation of movement.
Need to record the direction of the snake head, the direction of the snake tail, and the list to mimic the queue, add inflection point information (space than the list of each node open space saving a lot)
The idea felt clearer, and it was written in a few hours.
Programming Environment ubuntu12.04 Installation Ncurses:sudo Apt-get install ncurses compile: g++ xx.cpp-lncurses
#include <iostream> #include <list> #include <algorithm> #include <ncurses.h> #include < signal.h> #include <sys/time.h> #include <cstdio> #include <cstdlib>using namespace std;/******** Define the direction ****************/#define UP 1#define the 2#define left 3#define right 4#define random (x) (rand ()%x+1)// Used to generate a random number struct snode//node {int x;int y;//snode *next;} fruit;//definition fruit struct snake{snode front; SNode back;list<snode> turn;//here with a list to mimic a queue list<int> TURN_DIRECTION;//2 a list is mainly used to save inflection point information, convenient to judge int Front_ direction;//the direction of the head of the serpent int back_direction;//The vanishing direction of the Serpent tail int len; Mysnake;int ch,eat,i;list<snode>::iterator turn_iter;void Show (int signumber); void Conctroller (void); void draw _node (SNode node, char paint), void end_game (), BOOL Crash (), int main () {struct Itimerval value;value.it_value.tv_sec=0; Value.it_value.tv_usec=200000;value.it_interval.tv_sec=0;value.it_interval.tv_usec=200000;signal (SIGALRM,& Show); Setitimer (Itimer_real,&value,null); INITSCR ();//Initialize Virtual screen RAW ();//Disable line buffer NoEcho ();//Turn off keyboard echo keypad (stdscr,true);//Turn on function keyboard for (int i=0;i<40;i++) {mvaddch (0,i, '-'); MVADDCH (21,i ,'-');} for (int i=0;i<21;i++) {mvaddch (i,0, ' | '); MVADDCH (i,41, ' | ');} Mysnake.front.x=2;mysnake.front.y=1;mysnake.back.x=1;mysnake.back.y=1;mysnake.len=2;fruit.x=random (FRUIT.Y); =random ();d raw_node (Mysnake.front, ' * ');d raw_node (mysnake.back, ' * ');d raw_node (Fruit, ' # '); Mysnake.front_ DIRECTION=RIGHT;MYSNAKE.BACK_DIRECTION=RIGHT;MVPRINTW (22,0, "/******game:snake len:%d by Plss******/", Mysnake.len) ; Refresh (); Getch ();//wait to receive a null character, start the game Setitimer (Itimer_real,&value,null); Turn on the timer while (ch! = Key_f (2)) {Conctroller ();} Endwin (); return 0;} void Conctroller (void) {ch=getch (); switch (CH) {case Key_up:if (mysnake.front_direction!=down) {Mysnake.front_ Direction=up;mysnake.turn_direction.push_back (mysnake.front_direction); Mysnake.turn.push_back (Mysnake.front); Sleep (100);} Break;case Key_down:if (mysnake.front_direction!=up) {Mysnake.front_direction=down;mysnake.turn_direction.push_ Back (Mysnake.front_direction); Mysnake.turn.push_back (Mysnake.front); sleep (100);} Break;case Key_left:if (mysnake.front_direction!=right) {Mysnake.front_direction=left;mysnake.turn_direction.push _back (mysnake.front_direction); Mysnake.turn.push_back (Mysnake.front); sleep (100);} Break;case Key_right:if (mysnake.front_direction!=left) {Mysnake.front_direction=right;mysnake.turn_ Direction.push_back (mysnake.front_direction); Mysnake.turn.push_back (Mysnake.front); sleep (100);} Break;}} void Show (int signumber) {if (SIGNUMBER==SIGALRM) {Eat=0;draw_node (Mysnake.back, "); if (mysnake.front.x==fruit.x && mysnake.front.y==fruit.y) eat=1;for (int i=0;i<=eat;i++) {switch (mysnake.front_direction) {case up: Mysnake.front.y--;break;case Down:mysnake.front.y++;break;case Left:mysnake.front.x--;break;case Right: Mysnake.front.x++;break;} Draw_node (Mysnake.front, ' * ');} Switch (mysnake.back_direction) {case Up:mysnake.back.y--;break;case Down:mysnake.back.y++;break;case left: Mysnake.back.x--;break;case Right:mysnake.back.x++;break;} If(Mysnake.turn_direction.size () && (Mysnake.back.x==mysnake.turn.front (). x && mysnake.back.y== Mysnake.turn.front (). Y) {mysnake.back_direction=mysnake.turn_direction.front (); mysnake.turn_direction.pop_ Front (); Mysnake.turn.pop_front ();} if (Crash ()) end_game (); if (eat) {MYSNAKE.LEN++;MVPRINTW (22,0, "/******game:snake len:%d by plss******/", Mysnake.len); Fruit.x=random (fruit.y=random);d raw_node (Fruit, ' # ');} Refresh ();}} void Draw_node (SNode node,char paint) {mvaddch (node.y,node.x,paint);} void End_game () {struct Itimerval value;value.it_value.tv_sec=0;value.it_value.tv_usec=0;value.it_interval.tv_sec= 0;value.it_interval.tv_usec=0;setitimer (Itimer_real,&value,null); MVPRINTW (10,18, "Game_over");} BOOL Crash () {int max,min; SNode tmp;if (mysnake.front.x>40 | | mysnake.front.x<=0 | | mysnake.front.y<=0 | | MYSNAKE.FRONT.Y>20) return true;//wall/* Determine if you hit yourself */if (!mysnake.turn.empty ()) {i=mysnake.turn.size () -1;turn_iter = Mysnake.turn.end (); while (i--) {Tmp=*turn_iter;turn_iteR--;if ((*turn_iter). x==tmp.x && (*turn_iter). X==mysnake.front.x) {Max=max ((*turn_iter). y,tmp.y); Min=min ((*turn_iter). y,tmp.y); if (mysnake.front.y>=min && Mysnake.front.y<=max) return true;} else if ((*turn_iter). Y==tmp.y && (*turn_iter). Y==mysnake.front.y) {Max=max ((*turn_iter). x,tmp.x); Min=min ((*turn_iter). x,tmp.x); if (mysnake.front.x>=min && Mysnake.front.x<=max) return true;}} Turn_iter = Mysnake.turn.begin (); if ((*turn_iter). x==mysnake.back.x && (*turn_iter). x==mysnake.front.x) {Max =max ((*turn_iter). y,mysnake.back.y); Min=min ((*turn_iter). y,mysnake.back.y); if (mysnake.front.y>=min && Mysnake.front.y<=max) return true;} else if ((*turn_iter). Y==mysnake.back.y && (*turn_iter). Y==mysnake.front.y) {Max=max ((*turn_iter). x, mysnake.back.x); Min=min ((*turn_iter). x,mysnake.back.x); if (mysnake.front.x>=min && Mysnake.front.x<=max) return true;}} return false;}
Greedy snake C + + ncurses