Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generates a map and allows the play to move through it.
- // Potential difficulties: realtime player input, rougelike style
- // Done by 1:30 pm
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #define NORTH 0
- #define SOUTH 1
- #define EAST 2
- #define WEST 3
- #define HEIGHT 100
- #define WIDTH 100
- void print_dir(int dir)
- {
- char *string;
- if(dir == NORTH){
- string = "North";
- } else if(dir == SOUTH){
- string = "South";
- } else if(dir == WEST){
- string = "West";
- } else if(dir == EAST){
- string = "East";
- };
- printf("%s",string);
- return;
- }
- void print_map(int map[HEIGHT][WIDTH], int playerx, int playery)
- {
- int ix,iy;
- for(iy = 0;iy < HEIGHT;iy++){
- for(ix = 0;ix < WIDTH;ix++){
- if((iy == playery) && (ix == playerx)){
- printf("@");
- } else if(map[iy][ix] == 1){
- printf("0");
- } else {
- printf(" ");
- };
- // printf("%d",map[iy][ix]);
- }
- printf("\n");
- }
- return;
- }
- int main(int argc, char **argv)
- {
- srand(time(NULL));
- int i,x,y,pathx,pathy,newx,newy;
- x = WIDTH/2;
- y = HEIGHT/2;
- pathx = newx = x;
- pathy = newy = y;
- char input = ' ';
- int dir = 0;
- int map[HEIGHT][WIDTH] = {{0}};
- print_map(map,x,y);
- printf("\n");
- int counter = 100;
- while(counter--){
- //generate up to two new random paths
- map[y][x] = 1;
- for(i = 0;i < 2;i++){
- pathx = x;
- pathy = y;
- dir = rand()%4;
- int counter2 = (rand()%10);
- while(counter2--){
- if(dir == NORTH){
- pathy++;
- } else if(dir == SOUTH){
- pathy--;
- } else if(dir == WEST){
- pathx--;
- } else if(dir == EAST){
- pathx++;
- };
- map[pathy][pathx] = 1;
- };
- };
- //Pick a path
- /*
- do {
- newx = x + ((rand()%3) - 1);
- newy = y + ((rand()%3) - 1);
- //printf("newx = %d, newy = %d\n",newx,newy);
- } while(map[newy][newx] != 1);
- */
- print_map(map,x,y);
- newy = y;
- newx = x;
- input = getchar();
- if(input == 'w'){
- newy++;
- } else if(input == 's'){
- newy--;
- } else if(input == 'a'){
- newx--;
- } else if(input == 'd'){
- newx++;
- };
- // Follow the path if valid
- if(map[newy][newx] == 1) {
- x = newx;
- y = newy;
- };
- };
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement