#include <string> #include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int main() { start: srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(175); bool quit = false; int ch; int life=30; int points=0; int player_direct=0; int x=12; int y=6; int food_x=2+rand()%20; int food_y=2+rand()%9; int rock_direct=0; int rock_x=rand()%20; int rock_y=rand()%2; int bird_direct=0; int bird_x=16+rand()%9; int bird_y=1+rand()%10; int clock_1=0; int clock_2=0; //initiate color function start_color(); bkgd(COLOR_PAIR(1)); init_pair(1, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_RED, COLOR_BLACK); init_pair(3, COLOR_MAGENTA, COLOR_BLACK); init_pair(4, COLOR_WHITE, COLOR_BLACK); init_pair(5, COLOR_WHITE, COLOR_BLACK); init_pair(6, COLOR_RED, COLOR_WHITE); while (!quit) { ch = getch(); switch (ch) { case 'q': player_direct = 1; break; case 'w': player_direct = 1; break; case 'a': player_direct = 1; break; case 's': player_direct = 1; break; case 'z': player_direct = 1; break; case 'x': player_direct = 1; break; case 'o': player_direct = 2; break; case 'k': player_direct = 2; break; case 'l': player_direct = 2; break; case 'n': player_direct = 2; break; case 'm': player_direct = 2; break; case 'r': player_direct = 3; break; case 't': player_direct = 3; break; case 'y': player_direct = 3; break; case 'u': player_direct = 3; break; case 'c': player_direct = 4; break; case 'v': player_direct = 4; break; case 'b': player_direct = 4; break; case ' ': player_direct = 4; break; case '1': quit = true; break; case '0': timeout(555555); break; case 'p': timeout(175); break; } clock_1++; clock_2++; if (clock_1>10+rand()%5) rock_direct=4; if (clock_1>20+rand()%5) bird_direct=1; if (clock_2>=25+rand()%15){ food_x=rand()%31; food_y=1+rand()%11; clock_2=rand()%3; life=life-2; } //this erase function keep the moving characters from leaving a trace on the screen erase(); bool collision=false; bool collision_2=false; //player gets hit in certain conditions if (x>=rock_x && x<=rock_x+10 && rock_y==y || x==bird_x && y==bird_y){ life=life-3; collision=true; } if (x==food_x && y==food_y){ collision_2=true; food_x=rand()%31; food_y=1+rand()%11; life=life+5; points=points+50; } if (collision) { bkgd(COLOR_PAIR(6)); attrset(COLOR_PAIR(4)); mvprintw(1,0,":-("); mvprintw(y-1,x,"x"); mvprintw(y+1,x,"x"); mvprintw(y,x-1,"x"); mvprintw(y,x+1,"x"); } else bkgd(COLOR_PAIR(1)); if (collision_2) { attrset(COLOR_PAIR(4)); mvprintw(1,0,":-)"); mvprintw(y-1,x,"o"); mvprintw(y+1,x,"o"); mvprintw(y,x-1,"o"); mvprintw(y,x+1,"o"); } //keys or buttons pressed switch (player_direct){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } switch (rock_direct){ case 1: rock_x--; break; case 2: rock_x++; break; case 3: rock_y--; break; case 4: rock_y++; break; } switch (bird_direct){ case 1: bird_x--; break; case 2: bird_x++; break; case 3: bird_y--; break; case 4: bird_y++; break; } if (rock_x<0) rock_x=29; if (rock_x>29) rock_x=0; if (rock_y>14){ rock_y=rand()%2; rock_x=rand()%22; } //this code makes bird more likely to hit player if (bird_x<0 && x<=14){ bird_x=4+rand()%10; bird_y=y;} if (bird_x<0 && x>=15){ bird_x=19+rand()%10; bird_y=y;} if (x<0) x=32; if (x>32) x=0; if (y<0) player_direct=4; if (y>12) player_direct=3; //game over if (life<=0) quit=true; //player attrset(COLOR_PAIR(2)); mvprintw(food_y, food_x, "@"); attrset(COLOR_PAIR(3)); mvprintw(y,x,"O"); attrset(COLOR_PAIR(1)); mvprintw(bird_y,bird_x,"]"); attrset(COLOR_PAIR(5)); mvprintw(rock_y,rock_x,"oxoxoxoxoxo"); attrset(COLOR_PAIR(4)); mvprintw(0,1,"life:"); mvprintw(0,6,"%i", life); mvprintw(0,17,"SCORE:"); mvprintw(0,23,"%i", points); }//while (!quit) bracket. Do not delete this bracket //The area below here is for when quit=true, which stops the program. timeout(-1); erase(); mvprintw(5, 9, "GAME OVER"); mvprintw(7,9,"SCORE: $"); mvprintw(7,16,"%i", points); refresh(); getch(); endwin(); goto start; // restart game }//Closing bracket of main function
2 Comments
Braden Best
10/16/2015 08:47:59 pm
Okay, you know what? Your code is spaghetti.
Reply
Wilson Euler
10/17/2015 05:44:59 am
Thanks for taking the time to even look at my basic code. Also, thanks for the linkI will look at the video today.
Reply
Leave a Reply. |
Archives
March 2016
Categories |