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.

DarkBASIC Professional Discussion / Looping Functions

Author
Message
Mokraton
14
Years of Service
User Offline
Joined: 4th Dec 2011
Location:
Posted: 19th Dec 2011 04:27
I am writing a game for the is based around the very popular game "Geometry Wars". I am a new programmer, but I am willing to spend alot of time writing as well as learning how to program. So far I have created an avatar and background. Created my avatar to rotate to my mouse position, made my avatar move and also made him shoot bullets.

My error so far is that when my avatar shoots the bullet by clicking the mouse. The bullet will dispear if he clicks the mouse again. I would like to prevent this from happening by creating a loop sequence so that the avatar will automatically shoot bullets, but I have tried looping my statements and they have not worked.

For those of you who would like to help, please download my code which I have attached and take a look at it! Thank You!



Beginning Programmer
Professional Nerd
Millenium7
21
Years of Service
User Offline
Joined: 13th Dec 2004
Location:
Posted: 19th Dec 2011 13:02
this is because you are redoing the same sprite number, so of course it will reset the sprite each time

Your code is good for a beginner but it will take a slight difference in thinking to make it great

One good habit to get in to is thinking of ways to make your code more dynamic. What I mean by that is "how do I write a block of code, that only requires me to write it once, and it will do it's job no matter what I throw at it, without requiring my intervention"

The key to good code is in the planning and the way the developer thinks. So I won't write the code for you but instead give you something to think about

What you need is 2 functions. One to handle the initial bullet creation when the player clicks the mouse. Keep in mind this will have to use a different sprite number for every new bullet created. So you can't use a fixed value of just 'bullet.id'. Rather it'll be bullet.id+0 for the first shot, bullet.id+1 for the 2nd, bullet.id+2 for the third etc

The next will have to handle the movement, lifespan and collision detection of each bullet. Now here's the problem. How does the function know which bullets exist? because you may allow a maximum of 50 bullets to be shot at one time, but what happens once you reach bullet.id+49? it'll have to reset to bullet.id+0, by which time +47, +48 and +49 no longer exist, yet +49 and +0 do exist. So how does it know to only move +49 and +0, and not +47, +48 and +49 since they are no longer there? (or how do you know to delete them after X amount of seconds, since they are older than +49 and +0?)
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Dec 2011 13:09
You need to make an array of bullets, and loop through them to have more than one at a time.

Dim bullet(100) as Objects()

Mokraton
14
Years of Service
User Offline
Joined: 4th Dec 2011
Location:
Posted: 19th Dec 2011 14:15
Thank You for your help everyone, I will continue to work on my project over the next week and will get back to you if I have any problems or more questions.

I appreciate all your help because this is a learning experience for me and I am not used to arrays yet, writing program will help me understand it more.

Thank You everyone!

Beginning Programmer
Professional Nerd
Millenium7
21
Years of Service
User Offline
Joined: 13th Dec 2004
Location:
Posted: 20th Dec 2011 13:35 Edited at: 20th Dec 2011 13:35
Arrays are pretty much the lifeblood of any sort of code, and very easy to understand once you get over the initial hurdle. I'd start by learning about them. Basically you use the same variable name, i.e. 'bullet' but with a number on the end i.e. 'bullet(1)' or 'bullet(32)'. The single biggest advantage is you can loop through arrays extremely efficiently. If you are merely using variables you might write something like


// etc

which is incredibly dull, tiresome and not at all dynamic

much better to simply write



this starts by looking at bullet(1), then bullet(2), then bullet(3) and so on

you can make this even more adaptable by instead using something like



now the beauty of this subtle change is that it will now check every valid value, and is not fixed at 1-50 like before. The benefit of this is that you may decide later on down the track that you would prefer to only track 20 bullets, or maybe track 400 bullets. And you would not have to modify the function code. Since it will automatically loop from 1-20 or 1-400 instead of being fixed at 1-50. Make sense?

The point of it is to
1) shift your way to thinking, more time should go into the planning stage before you start writing any code
2) significantly reduce your workload. You may have several functions which previously relied on looping from 1-50, and if you only wanted to check 20, your code is now broken and all the 'for next' statements need to be modified. Using array count() it's dynamically expanded without requiring any of your input
3) reduce the need to go back and rewrite code should you decide to change something

Your first step should be simply to learn what the commands do, how variables work, how arrays/globals work (arrays are global by default btw, so are great for use in functions since they carry over to the rest of the program) and so on. But your goal should be to think of the longer term and write sections of code that handle tasks dynamically

Login to post a reply

Server time is: 2026-07-11 10:23:48
Your offset time is: 2026-07-11 10:23:48