| cbeppe (10) | |||
|
Hey, Here is a game I wrote in C++ as a project to teach myself the syntax once and for all. It is a small game, kinda arcade style, but still "fun." Some of you might want to try it :D Anyway, while my syntax is correct, and everything works, there are some things I am not to happy about. You might notice I'm using a lot of "goto"s, and that is because I just didn't know how else to do it. If anyone could tell me a better way to do it, I would be very grateful. Also, if you find anything else you would have done different (probably a lot) then please tell me, and I will do my best to fix it. Thanks, code to follow:
| |||
|
Last edited on
|
|||
| manojr (19) | |
| Congrats for your first game. :-) | |
|
|
|
| jRaskell (347) | |
|
You only need to call srand once, at the start of your program. all of the other control structures can be used instead of goto, depending on the situation you're using goto in. If you're using goto to repeatedly loop through the same code, then you could use while or for instead. If you're using goto to control the flow of code, then you should look at using if and switch statements Since you're already using all of these control structures, in addition to goto, it's clear you already understand how to use them, so it's just a matter of continuing to apply that knowledge instead of resorting to goto. One other thing you need to start doing to assist in this area is start using functions to clean up your code. This will make it more readable and easier to modify, and make it easier to see the high level program flow, and apply the proper control structures to get the flow you want. Also look into file access so instead of having all those item/object values hard coded, they're stored in a text file that gets read in. Then you can tweak values, even add new items, without having to modify the code. Modern games use this concept quite heavily, allowing the programmers to focus their time on adding more game features, and allowing non-programmers to focus on adding actual content to the game world itself. | |
|
|
|
| cbeppe (10) | |
|
Thank you very much for your replies. @jRaskell: About your suggestions, I tried a lot of different combinations with srand() and rand() and this is the one that worked best for me. As far as I can see, I'm only using srand() once, but you might have been referring to the rand() function? I'm using goto to jump to different places in the code. Mostly, I'm using it in the Store menu, redirecting players back to the Main menu after they are done. My menus are constructed like branches where the goto statement is used at the end of a branch to bring a user back to the "trunk." I am not completely sure, but I think I might have been able to use functions for it. Unfortunately, I'm not sure how. I actually thought about using files, but decided against it since I have been sending it back and forth to people via IM. If I was making a big game, or working with a team, the idea is definitely a good one. Again, thank you both for you comments. Very much appreciated ;) | |
|
|
|
| computerquip (1997) | |
|
Concerning goto statements: People tend not to use goto functions very often you'll soon find out. It usually ends up in buggy and unorganized code. In certain situations, it's simply easier to use a goto statement but that rarely comes across. In your situation, goto statements are confusing and seems to not be needed. Concerning massive amounts of variables: I don't feel these are neccessary in your game. In MANY MANY games, people avoid the allocation of new variables such as your by defining them with macros and letting the compiler hard code them wherever you use your macro. To add to that, for security (sort of) and clarification, if you want to keep variables, I suggest you make them constant unless they plan to be changed so as to tell anyone who looks at your code including yourself that the variables are for strict hard-coded configuration of your program. I see an overuse of system();. For the sake of good practice, I really do suggest you not use it for pause or cls and find alternative and faster methods to accomplish these tasks. | |
|
Last edited on
|
|
| cbeppe (10) | |||
|
@ computerquip: Thanks for taking the time to read through my code. I already know that using goto statements is a bad practice, but what I need to know is how you would replace them. For example, here in my first switch statement, I use a goto to get back to the main menu, where I was before. How would you elimiate that goto?
I will be sure to read up on Macros - I'm not really sure how to use them yet, but that will change. About the "System() overuse," do you have any suggestions as to how to replace them? In the beginning I used the "cin >> a;" to wait for the user, but that requires them to press 2 buttons. System("PAUSE") only requires one button press. Again, thanks for looking into my case. | |||
|
Last edited on
|
|||
| jRaskell (347) | |
| You're calling srand on line 112, which is inside both your attack loop (while loop) and your main loop (goto loop). | |
|
|
|
| firedraco (5494) | |||
To do your kind of switch and stuff, you would generally do something like:
| |||
|
|
|||
| arathalion (33) | |||||||
|
What did you write your code with? In which text editor? It might pay to invest in a good one because they can help with neatness as it may make your code easier to follow. A few things you may like to try: * Whenever you enter new level of scope, or open a {, indent your code until you exit that level of scope, or until you close the code block with }; * Try spacing things out, so if you declare a group of variables, and then start making function calls put some white space in between them. You may also like to do this by relevance. * You may also find it easier to read if you hand warp your code. Although most text editors have word wrap functionality, not all do (this forum being one) some examples of this
With your use of goto, If you changed the main label into a while loop, close it on line 169 and delete your other goto loops and it should work in the same way. Because the goto main statments are the last line of every route of that switch, then if you instead leave them, it will hit the break statement and move to the end, which is the end of an if statement. As the rest of the code is in the else that corresponds to the if statement you will go strait to the end of the while loop. Try watching the flow of your code and you may discover that you will get to where you want to go using a few more if statements. Perhaps in different places. One last thing
that was on line 159 and I see you have done it in at least one other spot. what you want to do is remove 10 from playerxp. To do that all you have to do is playerxp = playerxp - 10;. The -= operator makes it easier to do by removing the right hand side from the variable on the left. So, the following two lines of code are the same.
What you have done still works, but is essentially assigning -10 to the same variable twice. Hope that helped a little. Sorry if I seemed a bit condescending. | |||||||
|
Last edited on
|
|||||||
| computerquip (1997) | |
Macros are simple creatures. It basically says, replace x with y whenever you see x. It's completely precompilation which means it's done before compilation and has zero affect on your program. Although macros are known to be buggy and have bad reactions (sort of. It really depends on how you use them.) To accomplish macros, we use preprocessor directives such as #define BUFFERSIZE 255 . For more info, see http://cplusplus.com/doc/tutorial/preprocessor/For more on why system() is evil: http://www.cplusplus.com/forum/articles/11153/ For ways to clear your screen without system: http://www.cplusplus.com/forum/articles/10515/ For ways to pause the current console program: http://www.cplusplus.com/forum/articles/7312/ | |
|
Last edited on
|
|
| molenator10 (10) | |
| doesnt work:( | |
|
|
|
| molenator10 (10) | |
|
do we just put it in C++? what type of project? | |
|
|
|
| xitan (25) | |
| if you type exit in console it spams fighting. not sure why but it does. | |
|
|
|
| Galik (2239) | |||
Here is a way you can bunch your separate menus/tasks into functions and remove the gotos:
| |||
|
|
|||