|
|
| My First Game |
|
Derik Botha
Member #9,330
December 2007
|
I was wondering if anyone could please help me with my little project here. This is a Tank War game that I have been writing. This is my first real full game project. That is to say that this is the first project I have actually tried to get all the errors removed and the game itself working properly. However, I'm still left with two errors that defy my attempts to get rid of them. The first is a lag issue that start less than a minute after the program runs and the second is a problem with the homing algorithm of my missile. I was wondering if someone could please look at the code and see if they can find a solution to one of these problem. The lagging is the one that worries me that most as I've had no success so far at even establishing which part of my code causes it, only that the game runs smoothly for about 30 - 60 seconds and then it starts lagging. All the files needed to run the program is included above. The bitmap files should all be put in a folder called 'Graphics', the wave files all in a folder called 'Sound' and the FMP file should be put in a folder called 'Maps'. Other than that the files just need to be included in the project and it should compile smoothly. I would like to thank everyone in advance for any help they may give and say that I would appreciate any help that anyone can offer. P.S. I would also like other advice, if not too much trouble, if anyone sees something that I have done badly, as in bad coding method, or if there is something that can be done more easily and/or efficiently in another way. Along with this comments on my variable and function names, if they help identify the purposes of different variables and function and my indentation, if it is too much, not enough etc. Other than that comments on the amount of in code comments that I should be putting in code like this would also be appreciated. General lowdown of the game. It's two players on the same screen. One player plays with WASD and the other plays with the arrow keys. The player with WASD uses E to fire and Q to change weapon type, while the player with the arrow keys uses Space to fire and Enter to change weapon type. Edit - Got rid of all the useless attachments, didn't realize they stayed there. The one currently on is the latest version. In it I've fixed the missile homing problem, was just using the wrong variable somewhere in there. Some advice on how to improve the code would still be appreciated. I will try to implement what you have suggested this weekend Edgar, if I get time between all the other work I have to do. |
|
X-G
Member #856
December 2000
|
Quote: The first is a lag issue that start less than a minute Without having looked at the code, this smells like a memory leak or other failure to properly clean up to me. -- |
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Quote: All the files needed to run the program is included above. The bitmap files should all be put in a folder called 'Graphics', the wave files all in a folder called 'Sound' and the FMP file should be put in a folder called 'Maps'. Other than that the files just need to be included in the project and it should compile smoothly. If you put all the files in their appropriate directories in a TankWar folder that has all the source files and compress everything into a zip file for one single download then I might be tempted to look it over. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
Derik Botha
Member #9,330
December 2007
|
X-G said: Without having looked at the code, this smells like a memory leak or other failure to properly clean up to me. Yeah, thats what I though, and still think, I just can't seem to find the leak. Edgar Reynaldo said: If you put all the files in their appropriate directories in a TankWar folder that has all the source files and compress everything into a zip file for one single download then I might be tempted to look it over. Oops. Didn't think to do that. It's done now though, all the sound, graphics, map and source files in their right place, I hope. |
|
Indeterminatus
Member #737
November 2000
|
Just having looked at your main.cpp. You're calling the destructory PlayerOne->~Tank();, PlayerTwo->~Tank(); and PlayingField->~Map(); manually, while they were created with "normal" operator new, and never deleted. This may cause some serious problems. While it may not have such an effect in those particular cases (as it happens only once a game), it could if you do that with every dynamically created object. You should only call the destructor manually in very selected cases (which is very, very, very rare). Once you create an object with new, use delete to rid of it. _______________________________ |
|
Derik Botha
Member #9,330
December 2007
|
Thanks Indeterminatus, just changed that. Still don't understand why it makes a difference, but that's not too important now, and I'm sure I could find out with a bit of research. EDIT: Actually I see now how that makes a difference. I was destroying each of my dynamically created objects in that manner, and that was coincidentally what caused that lag, as far as I can tell. So THANK YOU SO MUCH for that. |
|
X-G
Member #856
December 2000
|
... there's your answer right there. Calling destructors doesn't free any allocated memory. That's what delete is for. -- |
|
Derik Botha
Member #9,330
December 2007
|
X-G said: ... there's your answer right there. Calling destructors doesn't free any allocated memory. That's what delete is for. ok thanks, I know what that means atleast, and how it makes a difference. Didn't realize it doesn't do that, releases the memory that is. Thanks for the help all three of you. Now I would just like some more general advice on how I could improve on the code. As this is my first project I'm not that good at optimization yet, well I really suck at it really, so any help in that regard would be apprecited. Other than that there is still the homing for the missiles, I've checked over my logic for that and re-written it a couple of times but can't seem to make it work. A solution doesn't necesarily have to be anywhere close to what I have either, all I want is a homing algorith that, while does home in on the target, isn't too good at it. EDIT: I've uploaded an updated version of the game, one where the lag is fixed and some other minor tweaks have been made. |
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
For your homing algorithm you might want to keep a pointer to a tank object in your projectile class that is the current target of the missile. When one is fired , store the projectile's initial direction of movement and when it is updated find the angle from it's current position to the tank's current position and allow it to turn towards it by a set maximum angular amount. If you adjust it right it should make it so that a tank will be able to get out of the way if it moves at a right angle to it's path. If you want a dumb projectile , set the target pointer to NULL and base your projectile's turning AI off of whether there is a target. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
Derik Botha
Member #9,330
December 2007
|
Thanks for the advice Edgar. The suggestion seems a bit complicated for my game though. I only have 8 directions of movement at the movement and your suggestion seems to be for a game that has many more than that, correct me if I'm wrong. Also, could you please give an example of the type of algorithm that would be used to achieve something along the lines of which you are suggesting? Another problem I have just encountered is my collisions. They use to throw the tank halfway into the collisable block if you collided, turned around and then tried to drive off. I though I fixed this problem but it still seems to be present, but does not occur very often. Another problem with the collisions is the tank seems to 'bounce off' the collidable block. As in get drawn over them and then a frame later gets taken out of the collidable block again, this is an undesired effect of the collision but I can't seem to get rid of that one either. First I though it must be where the collision algorithm is called in relation to the move algorith, and it most likely is, but I just can't seem to identify the problem. Any help on optimization of my collision algorithm would actually be greatly appreciated. I'm sorry about the trouble but I just started really using allegro a little while ago, and my C++ knowledge in general is shaky at best. |
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
You may only have 8 directions of movement for your tanks but that doesn't mean the projectiles have to stick to those directions , especially for a homing projectile. Wouldn't it look kind of strange to see a projectile traveling right all of a sudden start traveling up to the right? For your work on collision detection , are all the tanks 32X32 sprites? I would suggest doing a simple rectangular bounding box collision detection when you go to update the tanks movement. Check for a collision where the tank would be after it is updated but before you update its position. If the resulting movement would cause a collision then prevent it from moving that direction in the first place. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
Audric
Member #907
January 2001
|
One naive "homing" algorithm: If you don't care much about movement being smooth and speed consistent, it could fit the bill. |
|
Neil Black
Member #7,867
October 2006
|
Audric: I could see that working if the projectile didn't need to rotate its sprite to point along the axis of movement.
|
|
Derik Botha
Member #9,330
December 2007
|
Thank you for the advice Edgar, I think I might try that. Now the problem is getting an algorithm that can make the missile move at any of those direction. I have a rough idea of how I'd like to do it but can't go into detail now as I have to run. Hopefully I'll get time to post it for some input later today. |
|
|