Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Simple ncurses program
- #include <ncurses.h>
- #include <time.h>
- #include <stdlib.h>
- bool end=false;
- int s=0;
- int age=18;
- int points=0;
- int x=12;
- int y=5;
- int rock_x=25;
- int rock_y=1;
- int rock_dir=4;
- int key_press;
- int p_dir=0;
- void start_ncurses()
- {
- initscr();
- srand(time(NULL));
- noecho();
- curs_set(0);
- timeout(180);
- }
- void add_color()
- {
- start_color();
- bkgd(COLOR_PAIR(1));
- init_pair(1,COLOR_WHITE,COLOR_BLACK);
- }
- void pause(){
- timeout(981314);
- }
- void resume(){
- timeout(180);
- }
- void game_over()
- {
- endwin();
- refresh();
- getch();
- timeout(-1);
- }
- int main()
- {
- start_ncurses();
- add_color();
- while (!end)
- {
- key_press=getch();
- switch(key_press){
- case 'a': case 's': case 'd':
- p_dir = 1;
- break;
- case 'j': case 'k': case 'l':
- p_dir = 2;
- break;
- case 'r': case 't': case 'y': case 'u':
- p_dir = 3;
- break;
- case 'c': case 'v': case 'b': case 'n': case ' ': p_dir= 4;
- break;
- case 'g': p_dir=0; break;
- case '0': pause(); break;
- case 'p': resume(); break;
- }
- s++;
- attron(A_BOLD);
- if (s>30){
- s=0; age++;
- }
- if (age>100) end=true;
- switch (p_dir)
- {
- case 1: x--; break;
- case 2: x++; break;
- case 3: y--; break;
- case 4: y++; break;
- }
- switch (rock_dir)
- {
- case 1: rock_x--; break;
- case 2: rock_x++; break;
- case 3: rock_y--; break;
- case 4: rock_y++; break;
- }
- erase();
- if (x<0) x=30;
- if (x>31) x=0;
- if (x>=rock_x && x<=rock_x+2 && y==rock_y || y<3 || y>11) end=true;
- if (rock_y>11) {
- rock_y=2+rand()%1;
- rock_x=rand()%32;
- points=points+25;
- }
- mvprintw(0, 0, "Points: %i ", points);
- mvprintw(0, 20, "Age: %i ", age);
- mvprintw(2, 0, "******************************");
- mvprintw(12, 0, "******************************");
- mvprintw(y,x,"o");
- mvprintw(rock_y,rock_x,"[=]");
- }
- mvprintw(6,9,"Game Over ");
- mvprintw(8,9,"Score: %i", points);
- game_over();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement