Advertisement
StormWingDelta

BaseMazeTextGame

Jul 17th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.18 KB | None | 0 0
  1. // BaseMaze2point0.cpp : Defines the entry point for the console application.
  2. // Meant to be used for learning and this isn't exactly the best one to learn from.
  3. // This version of basemaze was made by Christopher Rogers, online name StormWing or StormWingDelta.
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <cmath>
  11. #include <ymath.h>
  12. #include <time.h>
  13. using namespace std;
  14.  
  15. struct Position
  16. {
  17.  
  18.     int x;
  19.     int y;
  20.  
  21. };
  22.  
  23. Position player; // Allows for the player's position to be set
  24. Position target; // Allows for the target's position to be set
  25. Position enemy1; // Allows for enemy1's position to be set
  26. Position enemy2; // Allows for enemy2's position to be set
  27. Position enemy3;
  28. Position enemy4;
  29. Position enemy5;
  30. //Position enemy6;
  31.  
  32. int random();
  33. char direction1 = 1; //Global Variable for tracking direction
  34. char direction2 = random();
  35. char direction3 = 1; //Global Variable for tracking direction
  36. char direction4 = 1;
  37. char direction5 = 1; //Global Variable for tracking direction
  38. //char direction6 = 1;
  39.  
  40.  
  41. void HandleEnemy(); // Function declaration
  42. int Modulo(int nb, int mod);
  43. void HandlePlayer(char input);
  44. void DrawMap();
  45.  
  46.  
  47. int main()
  48. {
  49.     srand ( time(NULL) );
  50.     player.x = 2, player.y = 10; //Location setting
  51.     target.x = 5, target.y = 5;
  52.     enemy1.x = 2, enemy1.y = 2;
  53.     enemy2.x = random(), enemy2.y = random();
  54.     enemy3.x = 2, enemy3.y = 3;
  55.     enemy4.x = 6, enemy4.y = 8;
  56.     enemy5.x = 5, enemy5.y = 1;
  57. //  enemy6.x = 7, enemy6.y = 2;
  58.  
  59.  
  60.     //GameLoop
  61.     while (1)
  62.     {
  63.         DrawMap();
  64.         char input;
  65.         printf("Enter a choice: A(left), D(right), W(up), S(down), and e(exit)\n");
  66.         scanf(" %c", &input);
  67.         if (input == 'e')
  68.         {
  69.  
  70.         goto Exit;
  71.         }
  72.         else
  73.         {
  74.             HandlePlayer(input);
  75.             HandleEnemy();
  76.  
  77.             system("cls");
  78.             //Colliding with an enemy
  79.             if ((player.x == enemy1.x && player.y == enemy1.y) || (player.x == enemy2.x && player.y == enemy2.y) || (player.x == enemy3.x && player.y == enemy3.y)|| (player.x == enemy4.x && player.y == enemy4.y) || (player.x == enemy5.x && player.y == enemy5.y)) //Detects when an enemy collides player
  80.             {
  81.                 printf("Sorry, YOU LOSE \n");
  82.                 goto Exit;
  83.             }
  84.             //Goal reached
  85.             else if (player.x == target.x && player.y == target.y) //Detects when the player gets to the goal.
  86.             {
  87.                 printf("YOU WIN THIS TIME :) ");
  88.                 goto Exit;
  89.             }
  90.  
  91.         }
  92.  
  93.     }
  94.     Exit: //Label
  95.     printf("\n");
  96.     system("pause");
  97.     return 0;
  98. }
  99. void HandleEnemy()
  100. {
  101.     /*Enemy 1*/
  102.     if (direction1)
  103.     {
  104.  
  105.         enemy1.x++;
  106.     }
  107.     else
  108.     {
  109.  
  110.         enemy1.x--;
  111.     }
  112.  
  113.     if (enemy1.x == 0 || enemy1.x == 10) // Controls Enemy 1 x dirextion
  114.     {
  115.  
  116.  
  117.         direction1 = !direction1;
  118.     }
  119.  
  120.  
  121.  
  122.     /*Enemy 2*/
  123.  
  124.     if(direction2)
  125.     {
  126.         enemy2.y -= 1;
  127.     }
  128.     else
  129.     {
  130.         enemy2.y += 1;
  131.     }
  132.     if (enemy2.y <= 0 || enemy2.y >= 10) // Controls Enemy 2 x dirextion
  133.     {
  134.         if (direction2)
  135.         {
  136.             enemy2.y++;
  137.         }
  138.         else
  139.         {
  140.             enemy2.y--;
  141.  
  142.         }
  143.  
  144.         direction2 = !direction2;
  145.     }
  146.     /*Enemy 3*/
  147.  
  148.     if(direction3)
  149.     {
  150.         enemy3.y -= 1;
  151.         enemy3.x -= 1;
  152.     }
  153.     else
  154.     {
  155.         enemy3.y += 1;
  156.         enemy3.x += 1;
  157.     }
  158.     if ((enemy3.y <= 0 || enemy3.y >= 10) || (enemy3.x <= 0 || enemy3.x >= 10)) // Controls Enemy 2 x dirextion
  159.     {
  160.         if (direction3)
  161.         {
  162.             enemy3.y++;
  163.             enemy3.x++;
  164.         }
  165.         else
  166.         {
  167.             enemy3.y--;
  168.             enemy3.x--;
  169.         }
  170.  
  171.         direction3 = !direction3;
  172.     }
  173.     /*Enemy 4*/
  174.  
  175.     if(direction4)
  176.     {
  177.         enemy4.y -= 1;
  178.         enemy4.x += 1;
  179.     }
  180.     else
  181.     {
  182.         enemy4.y += 1;
  183.         enemy4.x -= 1;
  184.     }
  185.     if ((enemy4.y <= 0 || enemy4.y >= 10) || (enemy4.x <= 0 || enemy4.x >= 10)) // Controls Enemy 2 x dirextion
  186.     {
  187.         if (direction4)
  188.         {
  189.             enemy4.y++;
  190.             enemy4.x--;
  191.         }
  192.         else
  193.         {
  194.             enemy4.y--;
  195.             enemy4.x++;
  196.         }
  197.  
  198.         direction4 = !direction4;
  199.     }
  200.  
  201.  
  202. }
  203. int Modulo(int nb, int mod)
  204. {
  205.     if (nb <0)
  206.     {
  207.         nb =+ mod;
  208.         /*the modulus function allows the player to wrap from one side to the other.*/
  209.  
  210.     }
  211.  
  212.  
  213.     return nb % mod;
  214. }
  215. void HandlePlayer(char input)
  216. {
  217.     int tempx = player.x, tempy = player.y;
  218.     if (input == 'a')
  219.     {
  220.         tempx--;
  221.     }
  222.     else if (input == 'd')
  223.     {
  224.         tempx++;
  225.     }
  226.     else if (input == 'w')
  227.     {
  228.         tempy--;
  229.     }
  230.     else if (input == 's')
  231.     {
  232.         tempy++;
  233.     }
  234.  
  235.     player.x = Modulo(tempx, 12);
  236.     player.y = Modulo(tempy, 12);
  237. }
  238. void DrawMap()
  239. {
  240.  
  241.     int y = 0;
  242.     for (y = 0; y < 12; y++) //Colomns
  243.     {
  244.         printf("\t--------------------------------------\n");
  245.         printf("\t");
  246.  
  247.         int x = 0;
  248.         for(x = 0; x < 12; x++) //Rows
  249.         {
  250.             printf("|");
  251.  
  252.             //Target Position
  253.             if (x == target.x && y == target.y)
  254.             {
  255.                 printf("TG");
  256.             }
  257.             //Enemy1
  258.             else if (x == enemy1.x && y == enemy1.y)
  259.             {
  260.                 printf("E1");
  261.             }
  262.             //Enemy2
  263.             else if (x == enemy2.x && y == enemy2.y)
  264.             {
  265.                 printf("E2");
  266.             }
  267.             //Enemy3
  268.             else if (x == enemy3.x && y == enemy3.y)
  269.             {
  270.                 printf("E3");
  271.             }
  272.             //Enemy4
  273.             else if (x == enemy4.x && y == enemy4.y)
  274.             {
  275.                 printf("E4");
  276.             }
  277.             //Player
  278.             else if (x == player.x && y == player.y)
  279.             {
  280.                 printf("P1");
  281.             }
  282.             else
  283.             {
  284.                 printf("  ");
  285.             }
  286.  
  287.         }
  288.         printf("|");
  289.         printf("\n");
  290.  
  291.     }
  292.     printf("\t--------------------------------------\n");
  293.     printf("\n");
  294.     cout << endl;
  295.  
  296. }
  297. int random()
  298. {
  299.     int boo;
  300.  
  301.     boo = rand() % 10 + 1;
  302.  
  303.  
  304.     return boo;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement