Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.22 KB | None | 0 0
  1. /* game.c ---------
  2. *
  3. * Filename: game.c
  4. * Description:
  5. * Author: Dmitrii Galantsev
  6. * Maintainer:
  7. * Created: May 12 11:28 2018
  8. * Last-Updated: June 4 11:00 2019
  9. * By: Matthew Fulford
  10. */ /* Commentary:
  11. *
  12. *
  13. */ /* Copyright (c) 2018 The Trustees of Indiana University and
  14. * Indiana University Research and Technology Corporation.
  15. *
  16. * All rights reserved.
  17. *
  18. * Additional copyrights may follow
  19. */
  20.  
  21. #include <unistd.h>
  22. #include <ncurses.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #include "tile.h"
  26. #include "game.h"
  27. #include "room.h"
  28. #include "key.h"
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. // stores value for how many floors have been created
  33. int floors = 0;
  34.  
  35. //function the fill the array with tiles to make a floor
  36.  
  37.  
  38. void MakeFloor(tile* tiles[100][100], int width, int height, int screen_x_offset, int screen_y_offset){
  39. int i, j;
  40. int horizontal_doors = 0;
  41. for(i = 0; i < 100; i++){
  42. int vertical_doors = 0;
  43. for(j = 0; j < 100; j++){
  44. tiles[i][j] = create_tile(i, j, screen_x_offset + i%width, screen_y_offset + j%height, 0, 0, 0);
  45. if(j%height == 0 && j != 0 && rand()%height == 1 && horizontal_doors == 0){
  46. tiles[i][j]->door = 1;
  47. tiles[i][j-1]->door = 1;
  48. horizontal_doors++;
  49. }else if(i%width == 0 && i != 0 && rand()%width == 1 && vertical_doors == 0){
  50. tiles[i][j]->door = 1;
  51. tiles[i-1][j]->door = 1;
  52. vertical_doors++;
  53. }
  54. if(j%height == 0){
  55. vertical_doors = 0;
  56. }
  57. }
  58. if(i%width == 0){
  59. horizontal_doors = 0;
  60. }
  61. }
  62. //checks for how many floors have been created before making a new one
  63. if(floors < 5){
  64. tiles[rand()%100][rand()%100]->stair = 1;
  65. }
  66. tiles[width/2][height/2]->state[CURRENT] = PLAYER;
  67. floors += 1;
  68. }
  69. void init_game(void) {
  70. int x,y;
  71. }
  72. int game(void) {
  73. srand(time(0)); //Seed random numbers
  74. static int state = INIT;
  75. struct timespec tim = {0,1000000}; // Each execution of while(1) is approximately 1mS
  76. struct timespec tim_ret;
  77. int width = 50;
  78. int height = 25;
  79. int score = 0;
  80. int pHealth = 100;
  81. int save = 1;
  82. char fileName[20];
  83.  
  84. tile * tiles[100][100]; //tiles that represent the floor
  85. tile * player; //pointer to keep track of the player
  86. room_t *r;
  87. int x_offset=0, y_offset=0;
  88. int screen_x_offset, screen_y_offset;
  89. int x_max, y_max;
  90. int arrow;
  91. int move_counter = 0;
  92. int move_timeout = BASE_TIMEOUT/10;
  93. int test = 0;
  94. while(1) {
  95. switch(state) {
  96. case INIT: // Initialize the game, only run one time
  97. initscr();
  98. nodelay(stdscr, TRUE); // Do not wait for characters using getch.
  99. noecho(); // Do not echo input characters
  100. getmaxyx(stdscr, y_max, x_max); // Get the screen dimensions
  101.  
  102. curs_set(0);
  103. screen_x_offset = (x_max / 2) - ((width) / 2);
  104. screen_y_offset = (y_max / 2) - ((height) / 2);
  105. MakeFloor(tiles, width, height, screen_x_offset, screen_y_offset);
  106. tiles[width/2][height/2]->state[CURRENT] = PLAYER;
  107. player = tiles[width/2][height/2];
  108. r = init_room(screen_x_offset - 1, screen_y_offset - 1, width+1, height+1);
  109. draw_room(r);
  110. state = STEP;
  111. break;
  112.  
  113. case STEP:
  114.  
  115. if (move_counter > move_timeout) {
  116.  
  117. // variables for char input, players locations and moving to locatrion
  118. int input;
  119. int original_x = player->x;
  120. int original_y = player->y;
  121. int new_x = original_x;
  122. int new_y = original_y;
  123.  
  124. //checks which key was pressed
  125. int keypress = read_escape(&input);
  126.  
  127. if(pHealth <= 0){
  128. state = EXIT;
  129. }
  130. if(keypress == UP){ //Up key move player up
  131. new_y--;
  132. }else if(keypress == DOWN){ //down key moves player down
  133. new_y++;
  134. }else if(keypress == LEFT){ //left key moves player left
  135. new_x--;
  136. }else if(keypress == RIGHT){ //right key moves player right
  137. new_x++;
  138. }else if(keypress == REGCHAR){ // if not arrow jey
  139. if(input == 'q' || input == 'Q'){ // q key quits game
  140. state = EXIT;
  141. break;
  142. }else if(input == 'p' || input == 'P'){ // p key pauses game
  143. while(1){
  144. char pause = getch();
  145. if(pause == 'p' || input == 'P'){
  146. break;
  147. }
  148. }
  149. }else if(input == 's' || input == 'S'){ // s key saves game
  150. FILE *f;
  151. if(save >= 10){
  152. save = 1;
  153. }else{
  154. save++;
  155. }
  156. switch(save){ // starts at save 1 and increments to 10 every time player saves
  157. case 1:
  158. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  159. strcpy(fileName, "./saves/save_1.game");
  160. break;
  161. case 2:
  162. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  163. strcpy(fileName,"./saves/save_2.game");
  164. break;
  165. case 3:
  166. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  167. strcpy(fileName,"./saves/save_3.game");
  168. break;
  169. case 4:
  170. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  171. strcpy(fileName,"./saves/save_4.game");
  172. break;
  173. case 5:
  174. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  175. strcpy(fileName,"./saves/save_5.game");
  176. break;
  177. case 6:
  178. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  179. strcpy(fileName,"./saves/save_6.game");
  180. break;
  181. case 7:
  182. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  183. strcpy(fileName,"./saves/save_7.game");
  184. break;
  185. case 8:
  186. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  187. strcpy(fileName,"./saves/save_8.game");
  188. break;
  189. case 9:
  190. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  191. strcpy(fileName,"./saves/save_9.game");
  192. break;
  193. case 10:
  194. mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
  195. strcpy(fileName,"./saves/save_10.game");
  196. break;
  197. default:
  198. mvprintw(screen_y_offset + 28,screen_x_offset,"Could not save.");
  199. return;
  200. }
  201. f = fopen(fileName, "w");
  202. int i;
  203. for(i = 0; i < 100; i++){
  204. int j;
  205. for( j = 0; j < 100; j++){
  206. fprintf(f, "%d", tiles[i][j]->state[CURRENT]);
  207. }
  208. fprintf(f, "\n");
  209. }
  210. fclose(f);
  211. }
  212. }
  213. // moves player depending on what was entered
  214. int result = move_player(&player, new_x, new_y, width, height, tiles);
  215.  
  216. // switch for what the player lands on
  217. switch(result){
  218. // enemy kills player and ends game
  219. case -1:
  220. pHealth -= 50;
  221. break;
  222. case -2:
  223. pHealth -= 100;
  224. break;
  225. case -3:
  226. pHealth -= 10;
  227. break;
  228. // player lands on gold and gains money
  229. case 1:
  230. score++;
  231. break;
  232. // player lands on empty space
  233. case 0:
  234. // if doot is at location allows player to move through wall to new room
  235. if(player->door == 1){
  236. switch(keypress){
  237. case UP:
  238. player->state[NEW] = EMPTY;
  239. player = tiles[player->x][player->y-1];
  240. player->state[NEW] = PLAYER;
  241. y_offset -= height;
  242. break;
  243. case DOWN:
  244. player->state[NEW] = EMPTY;
  245. player = tiles[player->x][player->y+1];
  246. player->state[NEW] = PLAYER;
  247. y_offset += height;
  248. break;
  249. case LEFT:
  250. player->state[NEW] = EMPTY;
  251. player = tiles[player->x-1][player->y];
  252. player->state[NEW] = PLAYER;
  253. x_offset -= width;
  254. break;
  255. case RIGHT:
  256. player->state[NEW] = EMPTY;
  257. player = tiles[player->x+1][player->y];
  258. player->state[NEW] = PLAYER;
  259. x_offset += width;
  260. break;
  261. default:
  262. break;
  263. }
  264.  
  265. }
  266. break;
  267. }
  268.  
  269. //if stairs, moves the player up 1 level
  270. if(player->stair == 1){
  271. undraw_room(r);
  272. MakeFloor(tiles, width, height, screen_x_offset, screen_y_offset);
  273. player = tiles[player->x][player->y];
  274. tiles[width/2][height/2]->state[CURRENT] = EMPTY;
  275. tiles[player->x][player->y]->state[CURRENT] = PLAYER;
  276. }
  277.  
  278.  
  279. switch(move_enemies(player, x_offset, y_offset, width, height, tiles)){
  280. case 1:
  281. pHealth -= 50;
  282. break;
  283. case 2:
  284. pHealth -= 100;
  285. break;
  286. case 3:
  287. pHealth -= 10;
  288. break;
  289. }
  290.  
  291. //prints Score and which floor they are on
  292. mvprintw(screen_y_offset - 3,screen_x_offset,"Gold : %d\t\tFloor: %d\tHealth: %03d", score, floors, pHealth);
  293. draw_room(r);
  294. update_tiles(x_offset, y_offset, width, height, tiles);
  295. display_tiles(x_offset, y_offset, width, height, tiles);
  296.  
  297. while(getch() != ERR); //prevents lag from having getch and sleep in a loop
  298. move_counter = 0;
  299. }
  300. move_counter++;
  301. break;
  302. case EXIT:
  303. endwin();
  304. return(0);
  305. break;
  306. }
  307. refresh();
  308. nanosleep(&tim,&tim_ret);
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement