Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BaseMaze2point0.cpp : Defines the entry point for the console application.
- // Meant to be used for learning and this isn't exactly the best one to learn from.
- // This version of basemaze was made by Christopher Rogers, online name StormWing or StormWingDelta.
- #include "stdafx.h"
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <cmath>
- #include <ymath.h>
- #include <time.h>
- using namespace std;
- struct Position
- {
- int x;
- int y;
- };
- Position player; // Allows for the player's position to be set
- Position target; // Allows for the target's position to be set
- Position enemy1; // Allows for enemy1's position to be set
- Position enemy2; // Allows for enemy2's position to be set
- Position enemy3;
- Position enemy4;
- Position enemy5;
- //Position enemy6;
- int random();
- char direction1 = 1; //Global Variable for tracking direction
- char direction2 = random();
- char direction3 = 1; //Global Variable for tracking direction
- char direction4 = 1;
- char direction5 = 1; //Global Variable for tracking direction
- //char direction6 = 1;
- void HandleEnemy(); // Function declaration
- int Modulo(int nb, int mod);
- void HandlePlayer(char input);
- void DrawMap();
- int main()
- {
- srand ( time(NULL) );
- player.x = 2, player.y = 10; //Location setting
- target.x = 5, target.y = 5;
- enemy1.x = 2, enemy1.y = 2;
- enemy2.x = random(), enemy2.y = random();
- enemy3.x = 2, enemy3.y = 3;
- enemy4.x = 6, enemy4.y = 8;
- enemy5.x = 5, enemy5.y = 1;
- // enemy6.x = 7, enemy6.y = 2;
- //GameLoop
- while (1)
- {
- DrawMap();
- char input;
- printf("Enter a choice: A(left), D(right), W(up), S(down), and e(exit)\n");
- scanf(" %c", &input);
- if (input == 'e')
- {
- goto Exit;
- }
- else
- {
- HandlePlayer(input);
- HandleEnemy();
- system("cls");
- //Colliding with an enemy
- 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
- {
- printf("Sorry, YOU LOSE \n");
- goto Exit;
- }
- //Goal reached
- else if (player.x == target.x && player.y == target.y) //Detects when the player gets to the goal.
- {
- printf("YOU WIN THIS TIME :) ");
- goto Exit;
- }
- }
- }
- Exit: //Label
- printf("\n");
- system("pause");
- return 0;
- }
- void HandleEnemy()
- {
- /*Enemy 1*/
- if (direction1)
- {
- enemy1.x++;
- }
- else
- {
- enemy1.x--;
- }
- if (enemy1.x == 0 || enemy1.x == 10) // Controls Enemy 1 x dirextion
- {
- direction1 = !direction1;
- }
- /*Enemy 2*/
- if(direction2)
- {
- enemy2.y -= 1;
- }
- else
- {
- enemy2.y += 1;
- }
- if (enemy2.y <= 0 || enemy2.y >= 10) // Controls Enemy 2 x dirextion
- {
- if (direction2)
- {
- enemy2.y++;
- }
- else
- {
- enemy2.y--;
- }
- direction2 = !direction2;
- }
- /*Enemy 3*/
- if(direction3)
- {
- enemy3.y -= 1;
- enemy3.x -= 1;
- }
- else
- {
- enemy3.y += 1;
- enemy3.x += 1;
- }
- if ((enemy3.y <= 0 || enemy3.y >= 10) || (enemy3.x <= 0 || enemy3.x >= 10)) // Controls Enemy 2 x dirextion
- {
- if (direction3)
- {
- enemy3.y++;
- enemy3.x++;
- }
- else
- {
- enemy3.y--;
- enemy3.x--;
- }
- direction3 = !direction3;
- }
- /*Enemy 4*/
- if(direction4)
- {
- enemy4.y -= 1;
- enemy4.x += 1;
- }
- else
- {
- enemy4.y += 1;
- enemy4.x -= 1;
- }
- if ((enemy4.y <= 0 || enemy4.y >= 10) || (enemy4.x <= 0 || enemy4.x >= 10)) // Controls Enemy 2 x dirextion
- {
- if (direction4)
- {
- enemy4.y++;
- enemy4.x--;
- }
- else
- {
- enemy4.y--;
- enemy4.x++;
- }
- direction4 = !direction4;
- }
- }
- int Modulo(int nb, int mod)
- {
- if (nb <0)
- {
- nb =+ mod;
- /*the modulus function allows the player to wrap from one side to the other.*/
- }
- return nb % mod;
- }
- void HandlePlayer(char input)
- {
- int tempx = player.x, tempy = player.y;
- if (input == 'a')
- {
- tempx--;
- }
- else if (input == 'd')
- {
- tempx++;
- }
- else if (input == 'w')
- {
- tempy--;
- }
- else if (input == 's')
- {
- tempy++;
- }
- player.x = Modulo(tempx, 12);
- player.y = Modulo(tempy, 12);
- }
- void DrawMap()
- {
- int y = 0;
- for (y = 0; y < 12; y++) //Colomns
- {
- printf("\t--------------------------------------\n");
- printf("\t");
- int x = 0;
- for(x = 0; x < 12; x++) //Rows
- {
- printf("|");
- //Target Position
- if (x == target.x && y == target.y)
- {
- printf("TG");
- }
- //Enemy1
- else if (x == enemy1.x && y == enemy1.y)
- {
- printf("E1");
- }
- //Enemy2
- else if (x == enemy2.x && y == enemy2.y)
- {
- printf("E2");
- }
- //Enemy3
- else if (x == enemy3.x && y == enemy3.y)
- {
- printf("E3");
- }
- //Enemy4
- else if (x == enemy4.x && y == enemy4.y)
- {
- printf("E4");
- }
- //Player
- else if (x == player.x && y == player.y)
- {
- printf("P1");
- }
- else
- {
- printf(" ");
- }
- }
- printf("|");
- printf("\n");
- }
- printf("\t--------------------------------------\n");
- printf("\n");
- cout << endl;
- }
- int random()
- {
- int boo;
- boo = rand() % 10 + 1;
- return boo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement