Architecture Exercises: C language Realization Snake (iii): The method of moving the encapsulated snake

Source: Internet
Author: User

Current progress:

How to move the package snake:

typedef struct SNAKEINFO{INT numparts;/* How many parts, snake body divided by how many segments */int lenparts[game_width];/* snake Body length of each paragraph */int xpartshead[ game_width];/* The x-coordinate of paragraph I of the body of the snake, the initial value is 1 */int the y-coordinate of paragraph I of the ypartshead[game_width];/* snake body, the initial value is 1 */uchar direction;/* the snake is currently moving in the direction of, 2: under , 4: Left, 6: Right, 8: */int (*movedown) (window *win), int (*moveup) (window *win), int (*moveleft) (window *win), int (*moveright) ( WINDOW *win);} Snakest;static Snakest snake = {1,{1,},{GAME_WIDTH/2,},{GAME_HIGTH/2,}, 6};

Complete the initialization of the snake:

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;}

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 Snakest snake = {1,{1,},{game_width/2,},{game_higth/2,}, 6};/* initial value The snake body is 1 segments long, the first paragraph length is 1, the 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) {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 check for available space, as terminal size is IND Ividual andMay vary.   The return value is a resource ID used to differ between multiple windows.*/int main (int argc, char *argv[]) {int x, y;   time_t T;   pthread_t thread;   WINDOW *win;          INITSCR ();   Arrival Workspace åœ errno indent credential bang 獥 å è¡#̈亞? Jian 呴 errno a hooker?        Curs_set (0);   闅 Refer bookmark Å corporation indent animals ㄦ€?           NoEcho ();    Fork name Mitsu ã ã wucun display? Win=newwin (25,50,0,0); Getmaxyx (win,y,x);   printf ("a:%d, b:%d\n", X, y);          Refresh ();   Arrival Hiiragiya Ryokan Juan € phone indent credential bang 獥 å è¡#̈亞? trickle terrain filler ã result 硶 display drama identify ã board slow fermium archives 獥 å è¡#̈亞?      Wrefresh (Win);  Arrival Feng Hiiragiya ryokan Juan € phone Hiiragiya ryokan antigen Rainbow Crypto 楀 Hani while (1) {snakecreate (win); Wrefresh (Win); Sleep (1);} Endwin (); return 0;}


Architecture Exercises: C language Realization Snake (iii): The method of moving the encapsulated 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.