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.

2D All the way! / Scoring system problem

Author
Message
Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 18th Jun 2009 14:36 Edited at: 18th Jun 2009 20:57
Hi, I am having problem with a scoring system. Once the players spaceships projectile hits the enemy spaceship it should be destroyed and 10 points added to the score.



I have tried this:



But this doesn't work. Can anyone help me. Thanks in advance, Bankzy10
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Jun 2009 02:43
Did you just type that code without pasting in your actual code? We'll have to see the real code to help you.

If that's the actual code you're stuck in a DO/LOOP forever "do loop" and never get to the sprite collision check (just remove the "do loop"). "end if" should be one word and deleted because the IF statement has THEN in it... and "str$score" should be "str$(score)".

Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 19th Jun 2009 12:15
Heres my code:



This is done in a sub called fire which is called when the user presses the spacebar key. What am I doing wrong?
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Jun 2009 12:44
What the heck... I just saw this code on your last message. Except a few minor changes like "Fire" where DO was and "return" where LOOP was. If this is exactly what is in your Fire routine (that wasn't shown in the last message) then you're doing everything twice... there's your lag.

It might be because you delete the sprite right before another collision check of that same sprite to add the score. At that point it doesn't exist to add the score.

Do it this way instead (one collision check):


You should post your entire code (from beginning to end) and any media. It'll help us to help you easier.

Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 19th Jun 2009 13:04
Here my entire game code. I was told by my lecturer that I had to do it twice for the background to scroll when the user fired...

Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 20th Jun 2009 06:40
Can I see the media too?

Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 21st Jun 2009 19:08 Edited at: 21st Jun 2009 19:39
The files are below. Sorry for not including them before mate.

Attachments

Login to view attachments
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 22nd Jun 2009 11:37 Edited at: 22nd Jun 2009 11:52
I'm quite impressed with that background image. I've never seen a seamless background that size before. The end looks just like the beginning so you don't have to slice up the image into single lines to get the scrolling effect (you really shouldn't slice up the image at all anyway... even if it wasn't seamless). All you have to do is have a starting x and y for the background image and decrease the x coordinate right before the end of the DO/LOOP. Darkbasic allows you to move things off the screen... even images as huge as your background. Because it's a seamless background once the background x coordinate reaches -5120 (the size of the image minus the horizontal resolution 640) you reset the background x coordinate to zero. The user can't tell the difference because it's seamless... to them the background goes on forever. You can also give yourself the ability to change just how fast the background scrolls by adding a variable for the speed of the background (an integer that represents how many pixels to decrease by).

Because you no longer need to cut up the background you can load it as an image instead. The other graphics also don't need to be loaded as bitmaps since all you do is grab the one single image anyway. LOAD IMAGE does the same thing as LOAD BITMAP and GET IMAGE combined into one command. You don't need to tell it how big a box to grab (it just knows automatically) and you don't need to use SET CURRENT BITMAP 0 to get back to the main screen (because LOAD IMAGE doesn't change the working screen).

Load all your images this way instead:


Rem off all the code you have now that has to do with the background scrolling and add this under INK at the top of your code:


Add this under the DO statement.


Add this right above the LOOP command:


Be sure to get rid of the SYNC in the middle of the DO/LOOP too.

There's no need to repeat all your code again just to shoot a bullet. You can put the bullet code in the main DO/LOOP. To be able to hit the spacebar once and not let it detect it over and
over again while the bullet is going across the screen you use a switch. A switch is a variable that you only define as a either on (as a 1) or off (as a zero)... like a regular light switch. When the user hits the spacebar you check if the bullet switch is off too. If it's off you define where the bullets starting x and y coordinates are and turn the bullet switch on. Outside of the IF statement looking for the spacebar you add a check if the bullet switch is on to move the bullet. When the bullet gets to the end of the screen turn off the switch.

Change this:


To this:


Once you add that feel free to delete everything under LOOP... you won't use it again. I know how to fix the lag for your ship too... but I'll save that for another message after you do the above. And of course I'll answer any questions you have with these changes. By the way are you running this in Classic or Pro?

Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 22nd Jun 2009 13:48 Edited at: 22nd Jun 2009 17:54
Yeah that has helped alot mate, thanks. The code is less cluttered and works a dream! Now I have put the alien ship and player spaceship in the code below, but what is your lag ship solution mate? It sort of lags abit when I put in the code about stopping the players ship from leaving the screen (above and below), so I have taken it out for the time being. I'm doing it in Classic by the way. Here is the new updated code:

Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Jun 2009 06:15
Well it's not quite right. You notice when the background image gets to the end there's a huge black space of no stars. It's because you need to change:


To this:


The reason the player sprite caused some lag is because the code before you updated was putting the player sprite on the screen, getting the sprite x and y coordinates, checking if the sprite was beyond the screen, then showing the sprite again to move the sprite back on the screen. There's no need to do it that way. You can have the going off the screen check within the IF/ENDIF for the up and down arrow keys before the sprite is shown. You also don't need to get the x and y coordinates using the SPRITE X() and SPRITE Y() commands because you already know the position of that sprite with the variables xpos and ypos.

So change this:


To this:


That should stop the lag.

Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 23rd Jun 2009 14:02 Edited at: 23rd Jun 2009 14:47
Yeah its lag free! Now this may sound stupid, but you know this:


What do the numbers mean at the end? I've never used load image before, just load bitmap.

Next problem would be to get the enemy to continously fire back. Would I do this the same as I have done for the user to fire, but how would I get it to do it with no key presses?

I also need to get collisions to happen as well. Would I do this?



EDIT: I just tried this, but the bullet doesn't move towards the player it just stops with the enemy spaceship:

Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Jun 2009 18:47
Quote: "What do the numbers mean at the end? I've never used load image before, just load bitmap."


The syntax is: LOAD IMAGE filename,image number,texture flag

The 1 at the end makes Darkbasic load the image exactly the way you see it in a drawing program. If you don't have a 1 at the end it'll slightly blur the image (mainly used for 3D textures). You can test it by removing the 1 off the background image and run it... you'll see the stars aren't exactly the way they look in a drawing program.

Quote: "Next problem would be to get the enemy to continously fire back. Would I do this the same as I have done for the user to fire, but how would I get it to do it with no key presses?"


Yes, you can use the same code just with different variables and switch. To make it work with no key presses you just remove the "spacekey()>0" and only check for the switch.

Quote: "I also need to get collisions to happen as well. Would I do this?"

Yes and no. You never want to delete anything if you're just going to recreate it seconds later. You especially don't want to delete images that are currently being used by sprites because you'd have to reload the image immediately after deletion. You can use HIDE SPRITE so you don't see it anymore but if it's hidden and in the same exact place collision will still be detected. To "remove" without deleting the sprite or hiding it you simply move the sprite offscreen.

Quote: "I just tried this, but the bullet doesn't move towards the player it just stops with the enemy spaceship:"


In the last update the enemy ship goes off the screen and never comes back... and since his bullets stay with him they also never come back. Add a check in there to see if the alien ship is equal to or less than -60 and redefine the enemy x and y coordinates.

Also what I usually do for projects is have a section at the top of the code that names the program, the date the project started, a list of all sprite numbers, images, sounds and a description of what everything is. Since you're using Classic having a to-do list would also help you remember things you want to do in the program.



Trust me it's always better to write all that stuff down... so you don't accidentally use a sprite number, image, or sound that you're already using. And for the future if you suddenly stop this project and come back to it a few months later.

Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 23rd Jun 2009 19:58 Edited at: 24th Jun 2009 01:08
I tried this for the enemies fire - It works for the first bullet, and then it doesn't fire another. I can't see why not as once it ends the EnemyFire=0 again so it should run the top section and go through it again?



All my updated code is below along with the media that can be downloaded at the bottom if it helps.



Also for some reason where you see the rem out collisions between the players bullet and enemy ship - it does work for some reason, its just says bob does not exist.

EDIT: Any help would be appreciated by anyone as this has to be done by the end of tomorrow (Wednesday 5PM UK Time). Many thanks Bankzy108

Attachments

Login to view attachments
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 24th Jun 2009 02:42
Quote: "I tried this for the enemies fire - It works for the first bullet, and then it doesn't fire another. I can't see why not as once it ends the EnemyFire=0 again so it should run the top section and go through it again?"


It's only because you have it check if alienbulletxpos is equal to or more than 640... which is off the right side of the screen. Your bullet goes to the left so check off the left side instead.

Quote: "Also for some reason where you see the rem out collisions between the players bullet and enemy ship - it does work for some reason, its just says bob does not exist."


When the program first runs sprite 2 doesn't exist until the spacebar is hit. So that collision check between the player bullet and the alien ship needs to be inside the bullet check itself... when you know for sure the bullet exists. You could also leave it out there and add BulletFire=1.

Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 24th Jun 2009 04:15
I left it out of the BulletFire=1 check, and just added the BulletFire=1 check above the collisions.

Now I have mostly everything working right, its the hard part. How would I make it so that there are more enemies, and the further you go the more enemies attack? So basically the further you go the harder it is. Could I use a timer of some sort? I've never really used them though.

Also I have the collision working now between the players bullet and the enemies ship...but now the ship continues to fire how would I stop it? I can't hide it because it will still count collisions...

Just in case you need it heres the updated code:



Also, which is better to use - sprite hit or sprite collision?

Thanks, all help muchly appreciated!
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 24th Jun 2009 06:23 Edited at: 24th Jun 2009 06:24
I was ok with helping you when you said you had a lecturer that told you that repeating code was a necessity... because teachers that teach the wrong methods need to be shown the right way. But now that you have a deadline I realize this project of yours is a class assignment. And I was a little annoyed when I saw Corbula asking for help and had the same exact deadline... so you two are apparently in the same class. I was further annoyed when I saw in his code snip code that I wrote entirely (with the exception of the label at the top).

I'll still point you two in the right direction but I won't give you guys code again. If I do everything for you... you won't learn a thing.

Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 24th Jun 2009 07:48
Quote: "Now I have mostly everything working right, its the hard part. How would I make it so that there are more enemies, and the further you go the more enemies attack? So basically the further you go the harder it is. Could I use a timer of some sort? I've never really used them though."


Remember anytime I capitalize a word it's a Darkbasic command you need to look up in the help files. You can create a variable to hold a TIMER() and a variable to store the number of enemies the program currently has. Add a new enemy every minute or lower ( to Darkbasic a second is 1000 in the TIMER() ). The only thing you have to do that may be hard for you is to store the x and y of every enemy in an array. If you don't know how an array works you can search the Darkbasic forums for "array" or I can give you the basics.

Quote: "Also I have the collision working now between the players bullet and the enemies ship...but now the ship continues to fire how would I stop it? I can't hide it because it will still count collisions..."


The same way you stop the player bullet from firing... turn off the enemies bullet switch or a quick solution is to move the y coordinate of the bullet to the top or bottom of the screen. It'll still continue to fire but out of sight and away from any other collision until a new enemy is created.

Quote: "Also, which is better to use - sprite hit or sprite collision?"


I personally only use collision because it detects in the sprite not just around it.

Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 24th Jun 2009 11:24 Edited at: 24th Jun 2009 19:14
I understand. Thanks for all your help mate you've been really helpful. I've had a little experience with arrays, but I'm not really sure what I would do for this situation. Could you possible talk me through it please?

When the player destroys the enemy the bullets and the enemy go hidden, but they are still there and collisions happen. I have moved them the sprites, but they move to the desired place, but instantly move back again. How could I solve this mate?



Thanks

Edit: I have two enemies on the screen at once but it really lags the game. I didn't use an array so I think thats whats making it lag?

Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 25th Jun 2009 19:06
When I saw your message yesterday the time in UK was about 9:00pm way past your deadline so I didn't bother to reply... unless you still want help.

Login to post a reply

Server time is: 2024-04-16 05:57:15
Your offset time is: 2024-04-16 05:57:15