![]() |
|
#1
|
|||
|
|||
Having trouble with Galaga-gameHello everyone. This is my first time posting, but I've been watching here for some time. I'm currently taking a C course in the university, and have a project due on monday. The program is basically a Galaga type of game. I currently have the ship, it fires, and moves.
The problem is, however, that I can't create the enemy ships because I use clrscr whenever the ship moves. Here's my code CPP / C++ / C Code:
Press E to play, A-D for left-right, S for shooting, M for menu, and Z for quitting. I compiled with borlandC 3.1 So I need some advice on how to create the enemy ships. Any help would be really appreciated : 8-) Thanks Last edited by dsmith : 07-Feb-2004 at 06:42.
Reason: Added c syntax highlighting code
|
|
#2
|
||||
|
||||
|
Hi DWk. Bienvenidos. (Man, I love spanish - I was in Argentina for several years).
I haven't tried to compile your program yet, but when I boot into windows I will give it a go. If I was you, I would try to avoid using clrscr at all. Otherwise you will need to redraw everything when your ship moves. Instead, I would call a clear routine that draws blanks at where the ship is before it moves CPP / C++ / C Code:
You will need to change your looping routine a bit. Maybe just throw it into a function called game_loop. Then set a timer to create your enemy ships. I assume that you are going to have several ships? Either use a structure or a couple of arrays to define these enemies and then do the movement similar to your ship. For example: CPP / C++ / C Code:
and then when it comes to moving, throw these into a for loop: CPP / C++ / C Code:
I think with todays computers this shouldn't lag too bad. Let us know what you come up with. BTW - I editted your post to sorround your code with c and /c. This gives the lovely syntax highlighting that you see... |
|
#3
|
|||
|
|||
|
Quote:
Hehe. Gracias Avoid clrscr? Yea, I'm thinking about that, since I would have to re-draw not only the ship, but the enemies. I don't have trouble re-drawing the ship, but the enemies would be a pain, I think. Bare with me on this - I'm in no way a l33t programmer (or resembling a good one I worked on the other program already, the one that doesn't use clrscr. Check it out Refer to Attached code file There are 2 current problems I have there: If I go all the way to the left side, and I press A again to go left again, it goes right. And when I go all the way to the right side, and press D to go right again, it shoots. Oh boy I'm a mess Last edited by dsmith : 08-Feb-2004 at 11:11.
Reason: Moved in-line program to attachment
|
|
#4
|
||||
|
||||
|
DWk
Your two problems have similar solutions. Take a look at this code snippet and where you have placed your break command. It is inside the else statement. Therefore if x<=5, your code will not break out of the switch and will proceed to the next case comparison, which in this instance is the d. CPP / C++ / C Code:
That should solve your problems there. I think that you have a good start to this game, but if I were you, I would change a thing or two, that may make it easier in the long run. First of all, you have defined x and y as globals, this is confusing for me, because you are passing x & y as well. So in your functions which x is being used, the global one or the local one? Also, I think I would try indexing x and y instead of calling the function iteratively like you have. For example, your 'a' case and 'd' case do the exact same thing, except that 'a' subtracts 7 and 'd' adds 7. If you use subroutines for erase_ship and draw_ship, I think it would be much more efficient. for example: CPP / C++ / C Code:
Then your case statement can be rewritten as: CPP / C++ / C Code:
Also, if you notice, instead of using if/else, I just used if because there is no code to run for instances where x is less than 5. Does this make more sense? One more note that you may want to consider. Look at the erase_ship routine that we have, it can be written easier: CPP / C++ / C Code:
I'm lazy. Less code is good... I think with this logic, creating your enemies and moving them will be alot easier. |
|
#5
|
||||||
|
||||||
|
Quote:
Someone else advised me to put a break inside the if as well. However, if I do that, my program exits to bash Quote:
Ok hold up x&y are just the position of the ship (you already know that). However, they are global at first because they are sent to centership, which is just called once (at the start of the game). Therefore, it all goes chained up, and I still use x&y because it tells you where you are gonna have to fire, erase, etc. Quote:
Sorry for being such a n0ob Quote:
There should be a break here, or at least the ship shouldn't move. If I put the break, the program exits. Quote:
Yea I understand this. It would be the same, but just in a cyclic way Quote:
But it still remains without solution. I've been trying and my head is like this guy because of that Thanks for your help, btw |
|
#6
|
|||
|
|||
|
Hmmmm nevermind about the break problem and the program exiting.
Another guy advised me to put ship(x,y); break; inside the if, and it works now to the real deal edit - by the way: what is that "x-=7" inside the case? I've never seen that |
|
#7
|
||||
|
||||
|
Quote:
Sorry, that is what I meant by indexing. The statement above is equivalent to: CPP / C++ / C Code:
You will see this a lot in c, for instance: CPP / C++ / C Code:
As for your structure, I would write a main game_loop that looks something like this: CPP / C++ / C Code:
Anytime in C (at least in my syntax ) when you see a word in all capitals, it is a Compiler definition. So at the top of your file, you would need to add :CPP / C++ / C Code:
The reason to do it like this, is that it will be easier to modify your program to wait longer between enemy movements & the maximum number of enemies. Look at this and let me know if there is something that you don't understand. |
|
#8
|
|||
|
|||
|
Hey
Ok, I just edited it with the draw_ship and erase_ship. Yea looks pretty good, and it works perfectly Now, what you said about the game_loop is what I don't understand Remember, I'm a n0ob at this By the way, I'm posting my program again, just so that you see what I have Refer to attachment By the way, the draw_radar, etc, took out 10 lines out of the program Peace, and thanks again for your help Last edited by dsmith : 08-Feb-2004 at 11:14.
Reason: Moved in-line program code to attachment
|
|
#9
|
|||
|
|||
|
By the way, I'm not intending to make it A galaga game. I just want enemies that go from the upper screen to the down screen. If you shoot em, fine. If you don't, fine as well. If they go to the bottom of they screen, they disappear. However, I don't want more than 3 or 4 enemies on-screen, and specially not with a special move or whatever. However, I DO need a Score and a boss
Hope you got what I meant :O EDIT - by the way, how can I create random numbers? between 80x25? EDIT2 - by the way...(i use a lot of "by the way", eh? Quote:
|
|
#10
|
||||
|
||||
|
Hey, its looking good. I am being a bit elusive in my answers because I want to make sure that you will understand your program when you are done.
Anyway - one comment I have on your code so far. Look how similar the centership and draw_ship functions are. Just a suggestion, but if it were me, I would combine them. Now for the game loop. Let me try to explain what I see should happen in there.
The first thing you should do is make your functions to draw and erase your enemies. In addition, you could make seperate functions to draw/erase your boss. Next, do you understand the do/while loop okay? CPP / C++ / C Code:
Basically, the kbhit() call is part of the conio library. It should return a 0 when no charecter is waiting and a 1 when a charecter is waiting. So this is the basic part to find a charecter (but not wait for it) and process it: CPP / C++ / C Code:
Finally, the time part. In order to use the time library, you need to include <time.h>. Basically the call to the function time will return the current time in seconds. So we want to make a basic timer using this call. First I set the time before entering the loop with: CPP / C++ / C Code:
Then I compare the time at each iteration through the loop. If it is greater than 5 seconds, I move my enemies and reset the timer. CPP / C++ / C Code:
Basically, this program is going to fly through this loop and most of the time do nothing. But it will always check to see if a key has been pressed and see if 5 seconds has elapsed and do what it needs to from there. Let me know what parts you don't understand. For random numbers, see this thread. Basically using rand()%80 or rand()%25 should work. |
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Game Cheating? | pcxgamer | Computer Software Forum - Games | 18 | 24-Nov-2005 11:12 |
| Trouble With .htaccess | amneziac85 | Apache Web Server Forum | 2 | 27-Jan-2004 17:35 |
| Trying to create the game of life | warny_maelstrom | C Programming Language | 10 | 21-Jan-2004 21:14 |
| Tips for game troubleshooting | pcxgamer | Computer Software Forum - Games | 0 | 02-Jan-2004 05:27 |
| X2 The Threat | pcxgamer | Computer Software Forum - Games | 0 | 25-Dec-2003 09:16 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The