Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Basic 2D help

Author
Message
Chris Fodge
18
Years of Service
User Offline
Joined: 31st Aug 2006
Location: New Jersey
Posted: 5th Aug 2008 09:19
Hello,

This is my first post on the DarkGDK. I have used DBPro in the past but was trying to teach myself c++. I installed everything and started to try and make a basic(or at least what i thought would be basic) 2D space invader style game. i started with basics and made a menu, then put the main player on the screen and made it move. I am trying to make the player fire now when the space key is pressed. i have been looking at some of the code in the tutorials that came with darkgdk but i just cant get it.

i made my little laser sprite. place it on the screen(just center for now). I can code it so the laser sprite moves but only when space key is held down... then i try to put it in a do loop that so it will continue after space is let go... when i do that the sprite disapears... here is my input functions for firing and movmnet



This code makes the sprite disapear... what am i doing wrong... i evenually want 5 - 10 of these laser sprites on the screen at once but i cant even get one to work correct right now

Any help would be awsome.

Thanks!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 5th Aug 2008 17:17
In essence you're saying that if the space key is down isPlayerFiring is true. Otherwise you set it to false. So, as soon as you release the space key it becomes false.

My opinion is that your game structure is flawed. If I'm reading what you're doing right, as soon as the space key is hit you fire off your laser and track it to the top (I assume) of the screen. If that's supposed to show some movement I don't see where you're calling dbSync() to update the display.

Without real time to get into code I'm going to fire off some really rough pseudo code. Everything should be encapsulated in a loop and updated by state variables. Personally, and this is only my opinion, dbMoveSprite () isn't always the best way to move a sprite. I much prefer maintaining the position and direction as program variables and updating the position using dbSprite().


You only want to set a variable to track the state of your laser sprite when you first detect the space key down. If the variable says that the laser is active you don't, and shouldn't check for the status of the space key but instead update the position of the laser. If the laser "goes off screen" then you set the status of the laser to inactive and can begin to test for the space key again.



Sorry to be so sketchy but I don't have time to develop a full treatment.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Chris Fodge
18
Years of Service
User Offline
Joined: 31st Aug 2006
Location: New Jersey
Posted: 5th Aug 2008 18:10
Ok, thanks alot that really help... i am able to draw one laser now and have it move across the screen after space is hit



Now what i need to do is when space is press 2,3,4,5 times other lasers to fire and do the same thing as this laser... i have differant sprites for them i just need to know how to tell the game to do it. i tryed changing dbSpaceKey() == 1 to == 2 but with no luck... thanks
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 5th Aug 2008 19:02 Edited at: 5th Aug 2008 20:36
Quote: "Now what i need to do is when space is press 2,3,4,5 times other lasers to fire and do the same thing as this laser... i have differant sprites for them i just need to know how to tell the game to do it. i tryed changing dbSpaceKey() == 1 to == 2 but with no luck"


This is where object oriented programming should come into play and why I feel it's important to learn that aspect of C++. Otherwise you're just programming in Basic with curly braces.

Essentially what you do is create a class that holds things like the sprite number and, if necessary, the image number. You also maintain information like direction, speed and location. A flag can keep track if the sprite is active. The code would be the same for each class object but the data would differ. Each object knows what it's supposed to do and if it's active or not. Then, on each cycle, you tell all of them to update. If any one of them isn't active the code says not to bother with this particular one. Otherwise it can calculate the next position and test for collision, etc.

In this manner you don't have to write code for each individual item you're trying to keep track of.

Edit: dbSpaceKey() will only return 1 or 0. It only tells you if the space key is down. You also have to be careful that you don't read it too fast because if you hold it down it will give you a 1 on subsequent test cycles until it's released. This could mean firing more than once. As for your multiple ammo issue, you need to test if you have any laser charge that isn't active and then activate it. Usually this is done by putting all your laser objects into an array and cycle through the elements of the array until you find one that's not showing active. The same array could be cycled through to update your charges also

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Chris Fodge
18
Years of Service
User Offline
Joined: 31st Aug 2006
Location: New Jersey
Posted: 6th Aug 2008 01:06
ok... i understand what i need to do. I set up an array with all my data about my laser sprites. the position, velocity and so on. I have been reading the c++ for dummies book and just came accross to chapter on array's. i cant seem to put the text base programs in the book and what i need to do here as far as an array. here is what i think my array needs to look like before i go further

ill just put info for 2 sprites right now



That puts the position for sprite 4 and 5 in the array and my laserSpeed.

i dont know what i am confused about. i feel kind of silly because it seems like something soo simple. maybe im just overlookin something small.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 6th Aug 2008 02:23
What you need to do is accumulate the information for each laser blast in one place.



This establishes a collection of data that defines what a laser needs to know about itself and we created it as a new data type known as LASER. Then you can declare five laser blast as:


LASER laser1, laser2, laser3, laser4, laser5;

Each of these LASERs contains its own data and you can assign something to a member of the LASER by doing something like:



The dot (.) indicates to use the specific variable member within the LASER.

Or better yet, make array like the following:

LASER allLasers [5];

which declares an array of LASERs and you can access the variables of the individual LASERs like the following:



Then you can do something like



The same technique can be used to hunt for a laser blast that that's not active and use that to set the initial position and speed then turn it active.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office

Login to post a reply

Server time is: 2024-11-20 17:30:09
Your offset time is: 2024-11-20 17:30:09