Architecture Exercises: C language Realization Snake (four): Receive button change the direction of the snake

Source: Internet
Author: User

Create a thread, receive keystrokes, change the direction of the snake:

Depending on the direction of the keypad value:

' 2 ': The Snake is descending

' 4 ': Snake to the left.

' 6 ': Snake right.

' 8 ': The Serpent is ascending


Source:

#include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include < time.h> #include <curses.h> #include <curses.h> #include <stdio.h> #include <stdlib.h># Include <unistd.h> #include <string.h> #include <ncurses.h> #include <unistd.h> #include < time.h> #include <string.h> #include <stdlib.h> #include <pthread.h> #include <errno.h># Define Game_width 50#define game_higth 25int targetgenerate (int *x_site, int *y_site) {int seconds= time ((time_t*) NULL); * Y_site = ABS (Seconds*random ())%game_higth;*x_site = ABS (Seconds*random ())%game_width;return 0;} typedef unsigned char uchar;typedef struct snakeinfo{int numparts;/* How many parts, snake body divided by number of segments */int lenparts[game_width];/ * The length of each segment of the snake body */int xpartshead[game_width];/* The x-coordinate of paragraph I of the snake body, the initial value is 1 */int ypartshead[game_width];/* snake body the y-coordinate of paragraph I, the initial value is 1 */uchar The direction;/* snake is currently moving in the direction of, 2: Next, 4: Left, 6: Right, 8: Upper */int (*movedown) (window *win), int (*moveup) (window *win);NT (*moveleft) (window *win); int (*moveright) (window *win);} snakest;static char chardir = 6;static Snakest snake = {1,{1,},{game_width/2,},{game_higth/2,}, 6};/* initial value The snake body is 1 segments long, the first paragraph is 1, Initial right movement, the initial position of the snake in the middle */int snakemove (window *win); int snakemovedown (window *win) {snake.ypartshead[0] + = 1;return 0;} int Snakemoveup (WINDOW *win) {snake.ypartshead[0]-= 1;return 0;} int Snakemoveleft (WINDOW *win) {snake.xpartshead[0]-= 1;return 0;} int Snakemoveright (WINDOW *win) {snake.xpartshead[0] + = 1;return 0;} int Snakeinit (WINDOW *win) {int i;for (i=0; i<snake.lenparts[0];i++) {MVWPRINTW (win,snake.ypartshead[0], SNAKE.XPARTSHEAD[0], "*");} Snake.movedown = Snakemovedown;snake.moveup = Snakemoveup;snake.moveleft = snakemoveleft;snake.moveright= Snakemoveright;snake.direction = 6;return 0;} int snakemove (Window *win) {snake.direction = chardir;printf ("snake.direction:%d\n", snake.direction); switch ( snake.direction) {case 2:snake.movedown (win); Break;case 4:snake.moveleft (Win); Break;case 6:snake.moveright (Win); Break;case 8:snake.moveup (Win); break;default:printf ("Invalid direct!\n"); return-1;}} int framecreate (WINDOW *win,int x_site, int y_site) {int I, j;for (j=0;j<game_higth;j++) {for (i=0;i<game_width;i++ {if (i = = Snake.xpartshead[0] && j = = Snake.ypartshead[0]) {MVWPRINTW (win,j,i, "+");} else if (i = = X_site && j = = Y_site) {mvwprintw (Win,j,i, "*");} else if (j = = | | J ==0) {mvwprintw (Win,j,i, "-");} else if (1 = = i) {mvwprintw (win,j,i, "|");} else if (i== (game_width-1)) {MVWPRINTW (win,j,i, "|");} else {MVWPRINTW (win,j,i, "");}}} return 0;} static int need_new_target = 1;static int x_site, Y_site;int snakecreate (WINDOW *win) {int I, j;if (need_new_target = = 1) {TA Rgetgenerate (&x_site, &y_site); need_new_target = 0;} Snakeinit (Win); Snakemove (Win); Framecreate (Win, X_site, Y_site); return 0;} /*resource ncurses_newwin (int rows, int cols, int y, int x), Ncurses_newwin () Creates a new window to draw elements in. W Indows can be positioned using X, Y, rows and cols. When creating additional windows, reMember to use NCURSES_GETMAXYX () to the check for available space, as terminal size was individual and may vary. The return value is a resource ID used to differ between multiple windows.*/void* get_new_direction (void* unused) {while ( 1) {Chardir = GetChar ();p rintf ("chardir:%c", Chardir), switch (chardir) {case ' 2 ': Chardir = 2;break;case ' 4 ': Chardir = 4; Break;case ' 6 ': Chardir = 6;break;case ' 8 ': Chardir = 8;break;default:break;} Sleep (1);} return NULL;}   int main (int argc, char *argv[]) {int x, y;   time_t t;pthread_t thread_id;pthread_create (&thread_id, NULL, &get_new_direction, NULL);   WINDOW *win;             INITSCR ();           Curs_set (0);           NoEcho ();    Win=newwin (25,50,0,0); Getmaxyx (win,y,x);   printf ("a:%d, b:%d\n", X, y);             Refresh ();      Wrefresh (Win);  while (1) {snakecreate (win); Wrefresh (Win); Sleep (1);} Endwin (); return 0;}


Architecture Exercises: C language Realization Snake (four): Receive button change the direction of the snake

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.