Advertisement
Guest User

Simple ncurses program

a guest
Jan 18th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. //Simple ncurses program
  2.  
  3. #include <ncurses.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6.  
  7.  
  8.  
  9.  
  10. bool end=false;
  11. int s=0;
  12. int age=18;
  13. int points=0;
  14. int x=12;
  15. int y=5;
  16. int rock_x=25;
  17. int rock_y=1;
  18. int rock_dir=4;
  19.  
  20. int key_press;
  21. int p_dir=0;
  22.  
  23.        
  24. void    start_ncurses()
  25. {
  26.     initscr();
  27.     srand(time(NULL));
  28.     noecho();
  29.     curs_set(0);
  30.     timeout(180);
  31. }
  32.    
  33.    
  34.         void add_color()
  35. {
  36.    
  37.  
  38. start_color();
  39.  
  40. bkgd(COLOR_PAIR(1));
  41.  
  42. init_pair(1,COLOR_WHITE,COLOR_BLACK);    
  43.  
  44. }
  45.    
  46.    
  47.     void pause(){
  48.         timeout(981314);
  49.     }
  50.     void resume(){
  51.         timeout(180);
  52.     }
  53.    
  54.    
  55. void game_over()
  56. {
  57.    
  58.    
  59. endwin();
  60. refresh();
  61. getch();
  62. timeout(-1);
  63.  
  64. }
  65.  
  66. int main()
  67. {
  68.    
  69.     start_ncurses();
  70.    
  71. add_color();
  72.  
  73.    
  74.     while (!end)
  75.     {
  76.    
  77.    
  78.     key_press=getch();
  79.    
  80.    
  81.     switch(key_press){
  82.        
  83.        
  84.         case 'a': case 's': case 'd':
  85. p_dir = 1;
  86. break;
  87. case 'j': case 'k': case 'l':
  88. p_dir = 2;
  89. break;
  90. case 'r': case 't': case 'y': case 'u':
  91. p_dir = 3;
  92. break;
  93. case 'c': case 'v': case 'b': case 'n': case ' ': p_dir= 4;
  94. break;
  95. case 'g': p_dir=0; break;
  96.  
  97. case '0': pause(); break;
  98.  
  99. case 'p': resume(); break;
  100.     }
  101.    
  102. s++;
  103.  
  104. attron(A_BOLD);
  105.    
  106.    
  107.    
  108.     if (s>30){
  109.         s=0; age++;
  110.     }
  111.    
  112.    
  113.     if (age>100) end=true;
  114.    
  115.    
  116.    
  117.     switch (p_dir)
  118.     {
  119.         case 1: x--; break;
  120.         case 2: x++; break;
  121.         case 3: y--; break;
  122.         case 4: y++; break;
  123.     }
  124.    
  125.     switch (rock_dir)
  126.     {
  127.         case 1: rock_x--; break;
  128.         case 2: rock_x++; break;
  129.         case 3: rock_y--; break;
  130.         case 4: rock_y++; break;
  131.     }
  132.        
  133.  
  134.     erase();
  135.    
  136.    
  137.     if (x<0) x=30;
  138.     if (x>31) x=0;
  139.     if (x>=rock_x && x<=rock_x+2 && y==rock_y || y<3 || y>11) end=true;
  140.    
  141.     if (rock_y>11) {
  142.        
  143.         rock_y=2+rand()%1;
  144.         rock_x=rand()%32;
  145.         points=points+25;
  146.     }
  147.    
  148.     mvprintw(0, 0, "Points: %i ", points);
  149.    
  150.     mvprintw(0, 20, "Age: %i ", age);
  151.  
  152.     mvprintw(2, 0, "******************************");
  153.     mvprintw(12, 0, "******************************");
  154.    
  155.    
  156.     mvprintw(y,x,"o");
  157.    
  158.     mvprintw(rock_y,rock_x,"[=]");
  159.    
  160.     }
  161.    
  162. mvprintw(6,9,"Game Over ");
  163. mvprintw(8,9,"Score: %i", points);
  164.    
  165.    
  166.     game_over();
  167.    
  168.  
  169.  
  170.    
  171.    
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement