SHARE
TWEET

ncurses game: "Galaxy_X"

Wilson_p_2037 Jan 25th, 2016 (edited) 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. // "Galaxy_X"
  4. // wperez274@gmail.com
  5.  
  6. #include <ncurses.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9.  
  10. class Player
  11. {
  12.  
  13.   public:
  14.  
  15. bool end;
  16.  
  17. int getkey, dir, lives, energy, age, points, x, y, traveled, collision;
  18. };
  19.  
  20. Player p = {
  21. 0,
  22. 0,
  23. 0,
  24. 3,
  25. 30,
  26. 18,
  27. 0,
  28. 5,
  29. 10,
  30. 0,
  31. 0
  32. };
  33.  
  34. class Food
  35. {
  36.  
  37.   public:
  38.  
  39. int clock, amount, x, y;
  40. };
  41.  
  42. Food bread = {
  43. 0,
  44. 0,
  45. 35,
  46. 0
  47. };
  48.  
  49. class Liquid
  50. {
  51.   public:
  52.  
  53. int clock, amount, x, y;
  54. };
  55.  
  56. Liquid water = {
  57. 0,
  58. 10,
  59. 26,
  60. 6
  61. };
  62.  
  63. class Money
  64. {
  65. public:
  66.  
  67. int clock, amount, x, y;
  68. };
  69.  
  70. Money coin={
  71. 0,
  72. 0,
  73. 16,
  74. 6
  75. };
  76.  
  77. class Animal
  78. {
  79.   public:
  80.  
  81. int dir, x, y;
  82. };
  83.  
  84. Animal bird = {
  85. 1,
  86. 22,
  87. 6
  88. };
  89.  
  90. class Machine
  91. {
  92.   public:
  93. int clock, dir, x, y;
  94. };
  95.  
  96. Machine ship = {
  97. 0,
  98. 0,
  99. 24,
  100. 1
  101. };
  102.  
  103. class room_1
  104. {
  105.  
  106. public:
  107. int ceiling,
  108.     ground;
  109. };
  110.  
  111. room_1 rm1=
  112. {
  113. 2,
  114. 14
  115. };
  116.  
  117. enum Move
  118. {
  119. NONE,
  120. LEFT,
  121. RIGHT,
  122. UP,
  123. DOWN
  124. };
  125.  
  126.  
  127. enum Border
  128. {
  129. MIN_X = -1,
  130. MAX_X = 33,
  131. MIN_Y = 3,
  132. MAX_Y = 13
  133. };
  134.  
  135. enum Colors
  136. {
  137. BK = 0,
  138. RD = 1,
  139. GN = 2,
  140. YW = 3,
  141. BE = 4,
  142. MA = 5,
  143. CN = 6,
  144. WE = 7
  145. };
  146.  
  147. void start_ncurses()
  148. {
  149. initscr();
  150. srand(time(NULL));
  151. noecho();
  152. curs_set(0);
  153. timeout(180);
  154. start_color();
  155. bkgd(COLOR_PAIR(1));
  156. init_pair(1, WE, BK);
  157. init_pair(2, MA, BK);
  158. init_pair(3, GN, BK);
  159. init_pair(4, CN, BK);
  160. init_pair(5, WE, BE);
  161. }
  162.  
  163. void draw_player()
  164. {
  165. attrset(COLOR_PAIR(1));
  166. mvprintw(p.y, p.x, "o");
  167. }
  168.  
  169. void draw_food(){
  170. attrset(COLOR_PAIR(2));
  171. mvprintw(bread.y, bread.x, "@");
  172. }
  173.  
  174. void draw_water(){
  175. attrset(COLOR_PAIR(5));
  176. mvprintw(water.y, water.x, "w");
  177. }
  178.  
  179. void draw_ship()
  180. {
  181. attrset(COLOR_PAIR(4));
  182. mvprintw(ship.y, ship.x, "[===]");
  183. }
  184.  
  185. void draw_bird()
  186. {
  187. attrset(COLOR_PAIR(4));
  188. mvprintw(bird.y, bird.x, "<");
  189. }
  190.  
  191. void draw_coin(){
  192. attrset(COLOR_PAIR(3));
  193. mvprintw(coin.y, coin.x, "$");
  194. }
  195.  
  196. void draw_room1(){
  197.  
  198. attrset(COLOR_PAIR(1));
  199. mvprintw(rm1.ceiling, 0, "OOOOooOoooOOOooOOoooooOOooOOOOOooo");
  200. mvprintw(rm1.ground, 0, "oOOoooOooOooOoOooooOoooooOooooOoOO");
  201.  
  202.  
  203. }
  204.  
  205. void room1_conditions()
  206. {
  207.  
  208. if (bird.x < 0)
  209. {
  210. bird.x = p.x + 4 + rand() % 4;
  211. bird.y = p.y;
  212. }
  213.  
  214. if (ship.clock > 5 + rand() % 10)
  215. ship.dir = DOWN;
  216.  
  217. if (ship.y > 13)
  218. {
  219. ship.y = 1 + rand() % 3;
  220. ship.x = rand() % 30;
  221. ship.clock = 0 - rand() % 10;
  222. ship.dir = 0;
  223. }
  224. }
  225.  
  226. void global_display()
  227. {
  228. attrset(COLOR_PAIR(1));
  229. mvprintw(0, 0, "Lives: %i ", p.lives);
  230. mvprintw(0, 20, "Money:$ %i ", coin.amount);
  231. mvprintw(16, 0, "Energy: %i ", p.energy);
  232.  
  233. mvprintw(16, 20, "Travel: %i ", p.traveled);
  234.  
  235. }
  236.  
  237. void game_over()
  238. {
  239.     erase();
  240. p.end = true;
  241. attrset(COLOR_PAIR(1));
  242. mvprintw(4, 9, "GAME OVER");
  243. mvprintw(6, 9, "Score: %i", p.points);
  244. mvprintw(8, 9, "Traveled: %i Bytes", p.traveled);
  245. if (water.amount<p.energy)
  246. mvprintw(10, 9, "Cause of death: Thirst.");
  247. if (p.energy<water.amount)
  248. mvprintw(10, 9, "Cause of death: Crashed.");
  249.  
  250. getch();
  251. timeout(-1);
  252. refresh();
  253. endwin();
  254. }
  255.  
  256. void room_1()
  257. {
  258. room1_conditions();
  259. ship.clock++;
  260. bread.clock++;
  261. water.clock++;
  262. coin.clock++;
  263. global_display();
  264. draw_player();
  265. draw_food();
  266. draw_water();
  267. draw_ship();
  268. draw_bird();
  269. draw_coin();
  270. draw_room1();
  271. }
  272.  
  273. void User_input()
  274. {
  275. switch (p.getkey)
  276. {
  277. case 'q':
  278. case 'w':
  279. case 'a':
  280. case 's':
  281. case 'z':
  282. case 'x':
  283. p.dir = LEFT;
  284. break;
  285. case 'o':
  286. case 'p':
  287. case 'k':
  288. case 'l':
  289. case 'n':
  290. case 'm':
  291. p.dir = RIGHT;
  292. break;
  293. case 'r':
  294. case 't':
  295. case 'y':
  296. case 'u':
  297. p.dir = UP;
  298. break;
  299. case 'c':
  300. case 'v':
  301. case 'b':
  302. case ' ':
  303. p.dir = DOWN;
  304. break;
  305. case 'g':
  306. p.dir = 0;
  307. break;
  308. // pause
  309. case 10: case ',': case '.':
  310. mvprintw(7, 12, "PAUSED");
  311. refresh();
  312. while (1)
  313. {
  314. p.getkey = getch();
  315. if (p.getkey == 10 || p.getkey == ',' || p.getkey == '.')
  316. {
  317. break;
  318. }
  319. }
  320. break;
  321. }
  322. }
  323.  
  324. void player_nav()
  325. {
  326.  
  327. switch (p.dir)
  328. {
  329. case LEFT:
  330. p.x--; water.clock++;
  331. p.traveled++;
  332. break;
  333. case RIGHT:
  334. p.x++; water.clock++;
  335. p.traveled++;
  336. break;
  337. case UP:
  338. p.y--; water.clock++;
  339. p.traveled++;
  340. break;
  341. case DOWN:
  342. p.y++; water.clock++;
  343. p.traveled++;
  344. break;
  345. }
  346.  
  347. }
  348.  
  349. void log_nav()
  350. {
  351. switch (ship.dir)
  352. {
  353. case DOWN:
  354. ship.y++;
  355. break;
  356. }
  357.  
  358. }
  359.  
  360. void bird_nav()
  361. {
  362.  
  363. switch (bird.dir)
  364. {
  365. case LEFT:
  366. bird.x--;
  367. break;
  368. }
  369. }
  370.  
  371. void global_cond()
  372. {
  373. p.getkey = getch();
  374. User_input();
  375. ship.clock++;
  376.  
  377. if (p.x < MIN_X)
  378. p.x = MAX_X;
  379.  
  380. if (p.x > MAX_X)
  381. p.x = MIN_X;
  382.  
  383. if (p.y < MIN_Y){
  384. p.dir = DOWN;
  385. p.energy-=5;
  386. }
  387. if (p.y > MAX_Y){
  388. p.dir = UP;
  389. p.energy-=5;
  390. }
  391.  
  392. if (p.x >= ship.x && p.x <= ship.x + 4 && p.y == ship.y || p.x == bird.x && p.y == bird.y ||
  393. p.y < 3 || p.y > 13){
  394.     p.energy -= 2 + rand() % 3;
  395.     p.collision++;
  396. };
  397.   if (p.x==bread.x && p.y==bread.y){
  398.   bread.x=rand()%34;
  399.   bread.y=2+rand()%12;
  400.   p.energy+=2+rand()%3;
  401.   };
  402.    if (p.x==water.x && p.y==water.y){
  403.   water.x=rand()%40;
  404.   water.y=rand()%40;
  405.   p.energy+=1+rand()%2;
  406. water.amount+=5;
  407.   };
  408.   if (bread.clock>15+rand()%10){
  409.   bread.x=rand()%34;
  410.   bread.y=2+rand()%12;
  411.   bread.clock=0;
  412.  
  413.   };
  414.  
  415.  
  416.   if (p.x==coin.x && p.y==coin.y){
  417.   coin.x=rand()%34;
  418.   coin.y=2+rand()%14;
  419. coin.amount+=50+rand()%100; p.points+=100+rand()%200;
  420.   };
  421.  
  422.   if (coin.clock>15+rand()%10){   coin.x=rand()%36;
  423.   coin.y=2+rand()%15;
  424.   coin.clock=0;
  425.   };
  426.  
  427.   if (water.clock>45+rand()%15)
  428.   {water.x=rand()%40;
  429.   water.y=rand()%40;
  430.   water.clock=0;
  431.   water.amount-=rand()%2;
  432.   };
  433.   if (p.energy<0)
  434.  {
  435.     p.energy=30;
  436.     p.lives--;
  437.  };
  438.  
  439.   if (p.lives < 1 || water.amount<-10)
  440. game_over();
  441. }
  442.  
  443. int main()
  444. {
  445. start_ncurses();
  446. while (!p.end)
  447. {
  448. global_cond();
  449. player_nav();
  450. bird_nav();
  451. log_nav();
  452. erase();
  453. global_display();
  454. room_1();
  455. }
  456.  
  457. game_over();
  458.  
  459. }
RAW Paste Data
Top