Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. // Generates a map and allows the play to move through it.
  2. // Potential difficulties: realtime player input, rougelike style
  3. // Done by 1:30 pm
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #define NORTH 0
  8. #define SOUTH 1
  9. #define EAST 2
  10. #define WEST 3
  11. #define HEIGHT 100
  12. #define WIDTH 100
  13.  
  14. void print_dir(int dir)
  15. {
  16. char *string;
  17. if(dir == NORTH){
  18. string = "North";
  19. } else if(dir == SOUTH){
  20. string = "South";
  21. } else if(dir == WEST){
  22. string = "West";
  23. } else if(dir == EAST){
  24. string = "East";
  25. };
  26. printf("%s",string);
  27. return;
  28. }
  29.  
  30. void print_map(int map[HEIGHT][WIDTH], int playerx, int playery)
  31. {
  32. int ix,iy;
  33. for(iy = 0;iy < HEIGHT;iy++){
  34. for(ix = 0;ix < WIDTH;ix++){
  35. if((iy == playery) && (ix == playerx)){
  36. printf("@");
  37. } else if(map[iy][ix] == 1){
  38. printf("0");
  39. } else {
  40. printf(" ");
  41. };
  42. // printf("%d",map[iy][ix]);
  43. }
  44. printf("\n");
  45. }
  46. return;
  47. }
  48.  
  49. int main(int argc, char **argv)
  50. {
  51. srand(time(NULL));
  52. int i,x,y,pathx,pathy,newx,newy;
  53. x = WIDTH/2;
  54. y = HEIGHT/2;
  55. pathx = newx = x;
  56. pathy = newy = y;
  57. char input = ' ';
  58. int dir = 0;
  59. int map[HEIGHT][WIDTH] = {{0}};
  60. print_map(map,x,y);
  61. printf("\n");
  62. int counter = 100;
  63. while(counter--){
  64. //generate up to two new random paths
  65. map[y][x] = 1;
  66. for(i = 0;i < 2;i++){
  67. pathx = x;
  68. pathy = y;
  69. dir = rand()%4;
  70. int counter2 = (rand()%10);
  71. while(counter2--){
  72. if(dir == NORTH){
  73. pathy++;
  74. } else if(dir == SOUTH){
  75. pathy--;
  76. } else if(dir == WEST){
  77. pathx--;
  78. } else if(dir == EAST){
  79. pathx++;
  80. };
  81. map[pathy][pathx] = 1;
  82. };
  83. };
  84.  
  85. //Pick a path
  86. /*
  87. do {
  88. newx = x + ((rand()%3) - 1);
  89. newy = y + ((rand()%3) - 1);
  90. //printf("newx = %d, newy = %d\n",newx,newy);
  91. } while(map[newy][newx] != 1);
  92. */
  93. print_map(map,x,y);
  94. newy = y;
  95. newx = x;
  96. input = getchar();
  97. if(input == 'w'){
  98. newy++;
  99. } else if(input == 's'){
  100. newy--;
  101. } else if(input == 'a'){
  102. newx--;
  103. } else if(input == 'd'){
  104. newx++;
  105. };
  106. // Follow the path if valid
  107. if(map[newy][newx] == 1) {
  108. x = newx;
  109. y = newy;
  110. };
  111. };
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement