Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* game.c ---------
- *
- * Filename: game.c
- * Description:
- * Author: Dmitrii Galantsev
- * Maintainer:
- * Created: May 12 11:28 2018
- * Last-Updated: June 4 11:00 2019
- * By: Matthew Fulford
- */ /* Commentary:
- *
- *
- */ /* Copyright (c) 2018 The Trustees of Indiana University and
- * Indiana University Research and Technology Corporation.
- *
- * All rights reserved.
- *
- * Additional copyrights may follow
- */
- #include <unistd.h>
- #include <ncurses.h>
- #include <stdlib.h>
- #include <time.h>
- #include "tile.h"
- #include "game.h"
- #include "room.h"
- #include "key.h"
- #include <stdio.h>
- #include <string.h>
- // stores value for how many floors have been created
- int floors = 0;
- //function the fill the array with tiles to make a floor
- void MakeFloor(tile* tiles[100][100], int width, int height, int screen_x_offset, int screen_y_offset){
- int i, j;
- int horizontal_doors = 0;
- for(i = 0; i < 100; i++){
- int vertical_doors = 0;
- for(j = 0; j < 100; j++){
- tiles[i][j] = create_tile(i, j, screen_x_offset + i%width, screen_y_offset + j%height, 0, 0, 0);
- if(j%height == 0 && j != 0 && rand()%height == 1 && horizontal_doors == 0){
- tiles[i][j]->door = 1;
- tiles[i][j-1]->door = 1;
- horizontal_doors++;
- }else if(i%width == 0 && i != 0 && rand()%width == 1 && vertical_doors == 0){
- tiles[i][j]->door = 1;
- tiles[i-1][j]->door = 1;
- vertical_doors++;
- }
- if(j%height == 0){
- vertical_doors = 0;
- }
- }
- if(i%width == 0){
- horizontal_doors = 0;
- }
- }
- //checks for how many floors have been created before making a new one
- if(floors < 5){
- tiles[rand()%100][rand()%100]->stair = 1;
- }
- tiles[width/2][height/2]->state[CURRENT] = PLAYER;
- floors += 1;
- }
- void init_game(void) {
- int x,y;
- }
- int game(void) {
- srand(time(0)); //Seed random numbers
- static int state = INIT;
- struct timespec tim = {0,1000000}; // Each execution of while(1) is approximately 1mS
- struct timespec tim_ret;
- int width = 50;
- int height = 25;
- int score = 0;
- int pHealth = 100;
- int save = 1;
- char fileName[20];
- tile * tiles[100][100]; //tiles that represent the floor
- tile * player; //pointer to keep track of the player
- room_t *r;
- int x_offset=0, y_offset=0;
- int screen_x_offset, screen_y_offset;
- int x_max, y_max;
- int arrow;
- int move_counter = 0;
- int move_timeout = BASE_TIMEOUT/10;
- int test = 0;
- while(1) {
- switch(state) {
- case INIT: // Initialize the game, only run one time
- initscr();
- nodelay(stdscr, TRUE); // Do not wait for characters using getch.
- noecho(); // Do not echo input characters
- getmaxyx(stdscr, y_max, x_max); // Get the screen dimensions
- curs_set(0);
- screen_x_offset = (x_max / 2) - ((width) / 2);
- screen_y_offset = (y_max / 2) - ((height) / 2);
- MakeFloor(tiles, width, height, screen_x_offset, screen_y_offset);
- tiles[width/2][height/2]->state[CURRENT] = PLAYER;
- player = tiles[width/2][height/2];
- r = init_room(screen_x_offset - 1, screen_y_offset - 1, width+1, height+1);
- draw_room(r);
- state = STEP;
- break;
- case STEP:
- if (move_counter > move_timeout) {
- // variables for char input, players locations and moving to locatrion
- int input;
- int original_x = player->x;
- int original_y = player->y;
- int new_x = original_x;
- int new_y = original_y;
- //checks which key was pressed
- int keypress = read_escape(&input);
- if(pHealth <= 0){
- state = EXIT;
- }
- if(keypress == UP){ //Up key move player up
- new_y--;
- }else if(keypress == DOWN){ //down key moves player down
- new_y++;
- }else if(keypress == LEFT){ //left key moves player left
- new_x--;
- }else if(keypress == RIGHT){ //right key moves player right
- new_x++;
- }else if(keypress == REGCHAR){ // if not arrow jey
- if(input == 'q' || input == 'Q'){ // q key quits game
- state = EXIT;
- break;
- }else if(input == 'p' || input == 'P'){ // p key pauses game
- while(1){
- char pause = getch();
- if(pause == 'p' || input == 'P'){
- break;
- }
- }
- }else if(input == 's' || input == 'S'){ // s key saves game
- FILE *f;
- if(save >= 10){
- save = 1;
- }else{
- save++;
- }
- switch(save){ // starts at save 1 and increments to 10 every time player saves
- case 1:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName, "./saves/save_1.game");
- break;
- case 2:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_2.game");
- break;
- case 3:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_3.game");
- break;
- case 4:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_4.game");
- break;
- case 5:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_5.game");
- break;
- case 6:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_6.game");
- break;
- case 7:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_7.game");
- break;
- case 8:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_8.game");
- break;
- case 9:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_9.game");
- break;
- case 10:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Saved game to save slot %02d", save);
- strcpy(fileName,"./saves/save_10.game");
- break;
- default:
- mvprintw(screen_y_offset + 28,screen_x_offset,"Could not save.");
- return;
- }
- f = fopen(fileName, "w");
- int i;
- for(i = 0; i < 100; i++){
- int j;
- for( j = 0; j < 100; j++){
- fprintf(f, "%d", tiles[i][j]->state[CURRENT]);
- }
- fprintf(f, "\n");
- }
- fclose(f);
- }
- }
- // moves player depending on what was entered
- int result = move_player(&player, new_x, new_y, width, height, tiles);
- // switch for what the player lands on
- switch(result){
- // enemy kills player and ends game
- case -1:
- pHealth -= 50;
- break;
- case -2:
- pHealth -= 100;
- break;
- case -3:
- pHealth -= 10;
- break;
- // player lands on gold and gains money
- case 1:
- score++;
- break;
- // player lands on empty space
- case 0:
- // if doot is at location allows player to move through wall to new room
- if(player->door == 1){
- switch(keypress){
- case UP:
- player->state[NEW] = EMPTY;
- player = tiles[player->x][player->y-1];
- player->state[NEW] = PLAYER;
- y_offset -= height;
- break;
- case DOWN:
- player->state[NEW] = EMPTY;
- player = tiles[player->x][player->y+1];
- player->state[NEW] = PLAYER;
- y_offset += height;
- break;
- case LEFT:
- player->state[NEW] = EMPTY;
- player = tiles[player->x-1][player->y];
- player->state[NEW] = PLAYER;
- x_offset -= width;
- break;
- case RIGHT:
- player->state[NEW] = EMPTY;
- player = tiles[player->x+1][player->y];
- player->state[NEW] = PLAYER;
- x_offset += width;
- break;
- default:
- break;
- }
- }
- break;
- }
- //if stairs, moves the player up 1 level
- if(player->stair == 1){
- undraw_room(r);
- MakeFloor(tiles, width, height, screen_x_offset, screen_y_offset);
- player = tiles[player->x][player->y];
- tiles[width/2][height/2]->state[CURRENT] = EMPTY;
- tiles[player->x][player->y]->state[CURRENT] = PLAYER;
- }
- switch(move_enemies(player, x_offset, y_offset, width, height, tiles)){
- case 1:
- pHealth -= 50;
- break;
- case 2:
- pHealth -= 100;
- break;
- case 3:
- pHealth -= 10;
- break;
- }
- //prints Score and which floor they are on
- mvprintw(screen_y_offset - 3,screen_x_offset,"Gold : %d\t\tFloor: %d\tHealth: %03d", score, floors, pHealth);
- draw_room(r);
- update_tiles(x_offset, y_offset, width, height, tiles);
- display_tiles(x_offset, y_offset, width, height, tiles);
- while(getch() != ERR); //prevents lag from having getch and sleep in a loop
- move_counter = 0;
- }
- move_counter++;
- break;
- case EXIT:
- endwin();
- return(0);
- break;
- }
- refresh();
- nanosleep(&tim,&tim_ret);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement