|
-
April 16th, 2010, 04:29 AM
#1
I want to optimize my code
Hello guys if you remember few days ago i was going crazy with trying to make my game work. Well the game works and it's all good, but i am really unimpressed with how the code looks and is managed.
i got 6 classes in total in order to make the code better but my main still looks like a mess. I went over it few times, changed few bits and added comments.
Could anyone see where it would be improved?
thank you very much 
Code:
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include"RandomNumber.h"
#include"Man.h"
#include"console.h"
#include<windows.h>
#include"Zombie.h"
#include"Hole.h"
#include<vector>
#include<algorithm>
int main()
{
srand((unsigned)time( NULL ));
//allows to pick amount of holes and zombies
int zomb = 5;
int hol = 5;
//creates the objects in the game
Man a_man;
std::vector<Zombie> zombies(zomb);
std::vector<Hole> holes(hol);
//randomly sets the postion of the man
a_man.set_position();
//randomly sets the postion of the zombies
for(int i = 0; i < zomb; i++)
{
zombies[i].set_position();
}
//randomly sets the postion of the holes
for(int i = 0; i < hol; i++)
{
holes[i].set_position();
}
//integer variables for cheking winning and losing
int lose = 0;
int win = 0;
//main game loop to move, show and check the objects
while(( lose == 0)&&(win == 0))
{
//displays and check the collision of holes
for( int i = 0; i < hol; i++)
{
holes[i].show_position();
if(holes[i].collides_with(a_man))
{
lose = 1;
}
//checks the collision of zombies with holes
for( unsigned int j = 0; j < zombies.size(); j++)
{
//if zombie fall into hole it gets removed from the game
if(holes[i].collides_with(zombies[j]))
{
zombies.erase(zombies.begin()+j);
}
}
}
//checks the collision of zombies with man
for( unsigned int i = 0; i < zombies.size(); i++)
{
if(zombies[i].collides_with(a_man))
{
lose = 1;
}
}
//shows the postion and moves the zombies
for( unsigned int i = 0; i < zombies.size(); i++)
{
zombies[i].show_position();
zombies[i].movement_Zombie_Hunt(a_man);
}
//if no zombies left in the game
if(zombies.size() == 0)
{
win = 1;
}
//shows position and moves the man
a_man.show_position();
a_man.movement_Man();
//clears and refreshes the screen
std::cout.sync_with_stdio();
Sleep(200);
system("CLS");
}
//checks for a win or loss
if(win == 1)
{
std::cout<< "YOU won" << std::endl;
}
else
{
std::cout<< "LOL YOU LOST" << std::endl;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|