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.

Newcomers DBPro Corner / problem with sprites

Author
Message
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 23rd Mar 2005 05:25
hi, im having a bit of a problem displaying a sprite on the screen.

i am using DBPro and when use sprites i have to code them like this


or else the sprite won't show up on the screen, this is causing me other problems in the game i am creating.

can someone suggest a different way of doing this without using the SET CURRENT BITMAP command??

i have also tried LOAD IMAGE command, but everytime i use it the image i want to display dosn't show, just a black screen.

thanks in advance
Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 23rd Mar 2005 08:09
This should work.



Note: In DBP if you ever use the load bitmap command with anything other than 0 for the number you will have to put set current bitmap 0
for anything to be drawn to the screen. DBP will set the current bitmap to the last one loaded but, it will only draw bitmap 0 to the screen.

Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 23rd Mar 2005 14:37 Edited at: 23rd Mar 2005 14:40
but in my game, if i press the left and right keys the ship moves left and right, so my code is like the:



if i use this code, when the right key is pressed the starship sprite should be hidden and show the starshipright sprite and then the ship will move right, right?!

but what happens is, the starship sprite dosn't hide and the starshipright sprite shows over the starship sprite(so you can still see some of it when the ship moves).

have a look at the pic i attached.

thanks

Edit: also, if i put the left key control code above the right key code, it happens when pressing the left key instead of the right.

Attachments

Login to view attachments
Soy Cocktail
20
Years of Service
User Offline
Joined: 23rd Dec 2003
Location:
Posted: 23rd Mar 2005 15:52 Edited at: 23rd Mar 2005 15:53
Just follow the flow of the code. Try to read it and do what the computer would do if you pressed the rightkey. It's what i use to help me debug my code. I will show you.

Lets say that the rightkey was pressed since this is the case when the bug occours. so lets look at the first part of that code
if rightkey()=1 and starshipX < 540
hide sprite starship
show sprite starshipright
inc starshipX,6
else
hide sprite starshipright
show sprite starship
endif

Since rightkey=1 we hide the sprite starship and show the sprite shipright.
This part is working just fine. Lets take a look at the second part.

if leftkey()=1 and starshipX > 2
hide sprite starship
show sprite starshipleft
dec starshipX,6
else
hide sprite starshipleft
show sprite starship
endif

Well the left key isin't pressed so we do the else action
hide sprite shipleft show sprite starship
Do you see what is wrong now?
Since the left key isint pressed the sprite starship is shown.

To fix this I would just combine the 2 if else statements into one if else if else if statement like this

if rightkey()=1 and starshipX < 540
hide sprite starship
hide sprite starshipleft
show sprite starshipright
inc starshipX,6
else
if leftkey()=1 and starshipX > 2
hide sprite starship
show sprite starshipleft
hide sprite starshipright
dec starshipX,6
else
hide sprite starshipleft
hide sprite starshipright
show sprite starship
endif
endif
I just moved the hide sprite shipright to the bottom else statement so I could delete the else statement below the if rightkey()=1 statement. I deleted the else statement from it, but i dont want to close the if statement just yet so we move the endif statement down with the other one. When we do it this way if you change from rightkey to left key without depressing both keys then the sprite shipright wont get hidden, so I added that to both the if leftkey()=1 statement. I did the same with the if rightkey()=1 statement for the shipleft sprite.

If you press both right and left keys it will display the shipleft sprite but the ship will move right then left. If you are useing manual syncing then the ship will stay in its same spot but will show the shipleft sprite. We can solve this by adding in another if statement below the one we have. The statement would look something like this:

if rightkey()=1 and leftkey()=1
hide sprite starshipleft
hide sprite starshipright
show sprite starship
endif

Hope all this made sense, and you understand it if it did.

When life hands you lemons go buy some oranges to make orange juice, and stop expecting everything to be given to you.
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 23rd Mar 2005 16:32
Thanks, thats helps loads and its starting to make sence!

Thanks again
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 23rd Mar 2005 20:27 Edited at: 23rd Mar 2005 20:28
Can u also help me with my laser firing code, the code is set to shoot when i press the left Ctrl key, but how do i make the the laser shoot up the screen by its self when i let go of the Ctrl key. At the moment, when i press the key, then laser shoots up and stops when i let go of the key.

Here's my code:


thanks again
Soy Cocktail
20
Years of Service
User Offline
Joined: 23rd Dec 2003
Location:
Posted: 24th Mar 2005 02:27
Lets follow the flow of the code again. This time we will assume that the controll key is pressed and then depressed since this is where the bug occours.
do

if keystate(29)=1
gosub shoot
else
laserx = starshipx +49
lasery = starshipy -2
endif

sync
loop


shoot:
if sprite exist(4)=1
show sprite 4

dec lasery,70
else
laserx = starshipx + 49
lasery = starshipy - 2
endif
sprite laser1,laserX,laserY,4
set sprite 4,1,1
return

Since the left controll button is pressed it runs the command gosub shoot. Everything looks just fine with that. Now lets assume that the controlkey was depressed to try to recreate the error. Since the controll key isin't pressed it never goes to the subroutine shoot, and it resets the laser to the ships position (with an offset). What I normally do for things I only want to run one time then key is pressed and not rerun it whle the key is held is to set a variable such as shooting. When you press left controll set shooting to 1. In your shoot subroutine you would probably need to add an if statement. Somthing like:

if shooting=1
show sprite 4
dec lasery,70
else
laserx = starshipx + 49
lasery = starshipy - 2
endif
sprite laser1,laserX,laserY,4


Now to stop the laser you would need another if statement to set shooting to 0. Something like if lasery=<0 then shooting=0. This would make the maser stop once it reaches the top of the screen.

I ended up witht his code after the sugggested changes.

do

if keystate(29)=1
shooting=1
endif
gosub shoot
sync
loop


shoot:
if shooting=1
show sprite 4
dec lasery,70
else
laserx = starshipx + 49
lasery = starshipy - 2
endif
sprite laser1,laserX,laserY,4
set sprite 4,1,1
if laserY=<0 then shooting=0
return

This has a little bug that I can already see from following the flow of the code. If you hold down left control then then the ship will just continue to fire even when the laser is off the screen. Its a simple fix and just goes to show you that the order of code is important. Simply move the if lasery statement to the top of the subroutine. When the laser reaches the top of the screen it is moved back to the ship and shot again (if the left controll key is held the whole time)

Hope all this made sense, and that if it did you understand it.

When life hands you lemons go buy some oranges to make orange juice, and stop expecting everything to be given to you.
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 24th Mar 2005 03:53
so the shooting variable is sort of the life span of the laser??
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 24th Mar 2005 04:09 Edited at: 24th Mar 2005 04:21
thats absolutly amazing, it works!!!. i hope i can be as good as you one day!!! thanks again

edit: oh, i forgot, i made a 3d model for me game, im going to make pic's of the model in different positions and use them as sprites (much nicer graphics than drawing them!).

attached is a pic of my model

Attachments

Login to view attachments
Soy Cocktail
20
Years of Service
User Offline
Joined: 23rd Dec 2003
Location:
Posted: 24th Mar 2005 08:16
That sounds like a good idea. I assume that the stars are just for there for the render. When you take the pictures have the background be all one color so you can set that color to transparent and have a scrolling star texture that your ship can fly over. that way there wont be a black box around your ship if you plan to put in stars below it (or any background)

When life hands you lemons go buy some oranges to make orange juice, and stop expecting everything to be given to you.
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 24th Mar 2005 08:46
yeah the stars are there just 4 the render.

how long have u been coding 4 Soy?
Soy Cocktail
20
Years of Service
User Offline
Joined: 23rd Dec 2003
Location:
Posted: 24th Mar 2005 13:32
Well, I recieved Dark Basic Professional as a chirstmas gift in 2003. I code off and on, whenever I feel like making something. I havent really made much. I recreated some old atari games because they are simple yet fun games. I have a project going for a wysiwyg editor that will create the key binding menus for games. I should probably be working on that...

When life hands you lemons go buy some oranges to make orange juice, and stop expecting everything to be given to you.
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 24th Mar 2005 15:47 Edited at: 24th Mar 2005 22:40
that sounds cool, i'm starting with 2d games first but after i've made a couple, i want to start 3d but i want to make something thats never been done before (i'm still thinking).

do u think its possible to make a high quality game in dbp, like the one's in the shops??

EDIT: what am i saying!!!! of couse it is!!!!
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 25th Mar 2005 02:45
Quote: "so the shooting variable is sort of the life span of the laser?? "


i have realized now that the quote above is wrong.

the shooting variable is a trigger, so if the shooting variable = 1, it triggers the gosub.

hope thats right!!
Soy Cocktail
20
Years of Service
User Offline
Joined: 23rd Dec 2003
Location:
Posted: 25th Mar 2005 03:19
Yes. Shooting means that the laser needs to be updated (as in moved upwards) the shooting variable=1 when the ship is firing a laser. It returns to 0 when the laser is off the screen. Later when you add in collision you will need to set shooting=0 when it collides to make the laser go away and stop moving up.

When life hands you lemons go buy some oranges to make orange juice, and stop expecting everything to be given to you.
Krad2004
19
Years of Service
User Offline
Joined: 8th Mar 2005
Location:
Posted: 25th Mar 2005 05:50
cool, i will try to do a collision test now to see what i can come up with

Login to post a reply

Server time is: 2024-09-23 15:33:07
Your offset time is: 2024-09-23 15:33:07