Architecture Exercises: C language Realization Snake (ii): Using the LIBNCURSES.A Library to implement maps and mushrooms do not change page refresh

Source: Internet
Author: User

Now has implemented maps and mushrooms, or the game interface does not change the page refresh,

This needs to be implemented using the LIBNCURSES.A library, as shown in the code below.

Next step: Realize the movement of snakes.


Source:

[email protected]:/mnt/shared/appbox/snake# cat snake.c#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 middle_        Line_create (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 = = X_site && j = = Y_site) {//printf ("*");                        MVWPRINTW (Win,j,i, "*");         } else if (j = = | | J ==0)               {MVWPRINTW (Win,j,i, "-");                                } else if (1 = = i) {//printf ("|");                        MVWPRINTW (win,j,i, "|");}                                else if (i== (game_width-1)) {//printf ("|\n");                        MVWPRINTW (win,j,i, "|");}                                else {//printf ("");                        MVWPRINTW (Win,j,i, ""); }}} return 0;}         int target_create (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;} static int need_new_target = 1;static int x_site, Y_site;int frame_create (WINDOW *win) {int I, J;                if (Need_new_target = = 1) {target_create (&x_site, &y_site);        Need_new_target = 0;        }//printf ("x:%d, y:%d\n", X_site, Y_site);        Middle_line_create (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 and may 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 ();        Initialize standard window, must drop Curs_set (0);           Hide cursor dynamic noecho ();        Input no echo win=newwin (25,50,0,0);           Getmaxyx (WIN,Y,X);        printf ("a:%d, b:%d\n", X, y); ReFresh ();      Refresh the standard window, or you will not be able to display the newly created window Wrefresh (win);                Refresh the new window while (1) {frame_create (win);                  Wrefresh (Win);        Sleep (1);        } endwin (); return 0;}  [email protected]:/mnt/shared/appbox/snake#


Architecture Exercises: C language Realization Snake (ii): Using the LIBNCURSES.A Library to implement maps and mushrooms do not change page refresh

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.