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 Discussion / Main Program loop/Infinite loop help

Author
Message
Darth Gazak
17
Years of Service
User Offline
Joined: 13th Jan 2009
Location:
Posted: 13th Jan 2009 12:31 Edited at: 13th Jan 2009 12:59
Hello,

I'm writing a simple game program for a school computing project which contributes to my overall grade in the subject. It started out well, as i have a grounding in TrueBASIC and found the transition to DarkBASIC quite simple. However, i have now reached a stage where my program has become quite complex, and i'm having trouble implementing several features.

The game consists of a user controlled spaceship sprite, controlled using the mouse. This works fine. However, the aim of the finished game is to dodge oncoming enemy sprites. I cannot seem to construct a loop which does the following:

1. Makes several enemy sprites appear at random y-coordinates along the right edge of the screen.
2. Makes the enemy sprites travel at constant velocity towards the right of the screen.

I have succeeded in displaying and moving one enemy sprite, but i need several to appear at different times and to repeatedly respawn throughout the game.

I have included my program code for reference, with the problem areas highlighted in bold:



This part is the user sprite control, which slows down if i put the enemyload section in a loop.



This is the loading and displaying of the enemy sprite. It is the part of the program i am having problems with.

Thanks for any suggestions. If further detail is needed i will explain in greater detail.

Thanks,
Darth Gazak
Darth Gazak
17
Years of Service
User Offline
Joined: 13th Jan 2009
Location:
Posted: 13th Jan 2009 13:11
Hello,

I'm in the process of creating a simple game program for a school Computing project. The aim of the game is for a user controlled sprite to dodge oncoming enemy sprites. The user sprite is mouse controlled, implemented in this section of code:



This part works fine, it exists within a DO...LOOP which only ends when the program terminates. My problems occurs when i try and create the enemy sprites. I want them to:

1. Spawn at random Y coordinates along the right edge of the screen
2. Move at constant velocity towards the lefte of the screen.
3. Respawn when they exit at the left side of the screen

I'm having trouble implementing these features. I know i must use some kind of loop to make them continually respawn, but when i do so, the mouse control loop becomes sluggish and makes the game unplayable. If i include them in the same DO...LOOP, the same thing happens to the mouse control. Also, it is imperative that there is more than one enemy sprite. This is the section of code where i load the enemy sprites:



I hope i have been clear enough in my explanation. If not, i will strive to describle the problem in greater detail.

Thank you,
Darth Gazak
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 13th Jan 2009 18:06 Edited at: 13th Jan 2009 18:07
Try the sync command. This will give YOU control over the refresh rate, rather than leaving it on the auto refresh (which refreshes after every line or so, which is bad because loops can slow down the main one).

Here is the usage for sync:



So there are 3 commands used: SYNC ON, SYNC RATE, and SYNC.

The SYNC ON simply tells it to turn off auto refreshing and enable the usage of sync.

The SYNC RATE tells it a refresh rate that you want to max at. This is optional, but usually helpful. A value of 0 will tell the computer that it should refresh as fast as the computer will allow (good and bad, depends on situation). Optionally, you can put your own number in there to help control the program (such as if the program is going too fast)

The SYNC command is the command that tells the computer to actually refresh the screen. It is good to have as few of these as possible (I usually do my best to only have 1, so that everything essentially gets displayed at the same time).

Now, for your other questions:

1. You can use a FOR->NEXT loop to spawn the enemies. Using a counter variable you can make sure it only spawns them every 30 loops or whatever. To make them appear randomly, just use RND(479). 479 is chosen because while there are 480 sprites, you have to count 0 as well. Actually, you should put 479-the height of the target so that it doesn't appear offscreen. You would also need the RANDOMIZE TIMER() command. The RANDOMIZE command will reseed the random equation (since it isn't really random). A value of TIMER() will set the seed value to the system timer, which will make it very unlikely for the same event to occur (though not entirely impossible).

The code for all this would look something like this:


This is a big example (not tested, but it should be all right, maybe a few typos here and there, I actually intended for it to be more pseudocode and real code, OOPS) that demonstrates both number 1 and 2. Notice, I included some code in the spawn area to make sure that two sprites wouldn't overlap when spawned. The problem with that though is that you may run into a point where you can't place another sprite and you haven't spawned 10 enemies. That is where the attempts variable comes in. If it tries 3 times and can't place it, it sets i (the for-next loop variable) to 11, so that it will exit immediately (the result: you might not get 10 enemies but they don't overlap).

For 3, do a check (perhaps within the FOR movement= 1 to current_enemy) that if it moves and it is past the edge of the screen, position it at the right side of the screen.

Hope this helps!

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 13th Jan 2009 18:36
If you can get one enemy ship working correctly then all the others should be easily done with the use of an Array.

It is important that you use a subroutine instead of a function to load up your variables or to move your objects. DBC do not have global vairables, all the variable inside a function is local.

The same vairable names outside and inside a function are 2 different variables. What's declared inside a function will only be true while inside that particualar function.

If you revisit Test() n will be 4000.


To make the enemy move to the left it's a good idea to make a subroutine for it, something like.
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 13th Jan 2009 21:36
yes a for next loop would work but for what he is doing wouldn't he need something (not sure how to do it) that would make the enemies come out at different time because this looks like it would all come out at the same time in a line. unless thats what you were going for lol i wasn't sure


@Darth Gazak

is that a typo or is you screen that size for the enemies it says your starting X is 470 and your Y is random 600. my screen is 640X480 and if i'm right you want you enemies to start just a tiny bit off screen and then come in. not 200 into the screen. and 600 would be a good 160 off witch means that you enemy would be able to land 160 of the screen hahaha. jw that sounded like a odd size to me or a odd starting position lol

and how is that working in functions? Ashingda made a good point variables are destroyed when a functions ends or the ones in them are i should say. so how are you changing variables in functions and still getting it to work. if you got global variables somehow that would be a interesting trick lol
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 13th Jan 2009 23:51
Quote: "variables are destroyed"

Not technically. The variables retain their values, but only INSIDE the function. So if you call the function twice, it will keep the value that the variable ended with, unless you reset it at the beginning or end.

However, it isn't usually a good idea to load media in a function, since you can only use the function once (otherwise you get an image already exists error).

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 14th Jan 2009 05:24
really? I didn't know that thanks.

And exactly that was my point
Darth Gazak
17
Years of Service
User Offline
Joined: 13th Jan 2009
Location:
Posted: 14th Jan 2009 16:37
Hello,

Thank you ever so much for your prompt and detailed replies. I apologise for the double post, the forum is set up in a way that posts by new users have to bee verified as suitable before they are posted. I didn't notice this, so i thought i had lost my original post and made a new one! What a waste of time that was

Anyway, i plan to take each of your advice into account and try and implement these techniques into my program. Considering some of your points, i think a full program review is needed, as i was under the impression DarkBASIC never supported subroutines! (inaccurate information from a classmate. A sabotage attempt? ) I found this strange considering BASIC is founded on subs, yet i transgressed and used functions instead. This has clearly proved problematic, and i will try and replace them with subroutines as soon as possible.

Again, thanks for your help. If i have any more problems, i know exactly where to get advice!

Darth Gazak
Stefan p
17
Years of Service
User Offline
Joined: 2nd Nov 2008
Location: Online
Posted: 18th Jan 2009 20:00
@Caleb1994 I think using the screen width() -50 woudl be best becuase it would the same on all screens it is played on.

I was here.

Login to post a reply

Server time is: 2026-07-05 01:47:55
Your offset time is: 2026-07-05 01:47:55