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 in DBC Really need some thoughts on this..... Noob to DBC

Author
Message
Xatnys
20
Years of Service
User Offline
Joined: 8th Jul 2004
Location:
Posted: 9th Jul 2004 23:03
Hello,

I tried posting this in "2d All the way", but it has gotten no replies, and I really need advice, so I came here. Below is more information regarding my problem:

This is in regards to the tutorial found here:
http://forum.thegamecreators.com/?m=forum_view&t=27598&b=4
I posted in that thread, but also posted it here to increase exposure.

Hi there, I'm new to posting so I hope I've done this right. Actually I'm new to DarkBasic, and have only been trying to learn it for 5 days now. I have put my version of this tutorial below. I was hoping to get some advice on how I can correct the following problems:

1. After making modifications to the original code, my sprite based collision detection is no longer functioning and I have no idea why or how to fix it.

2. The actual fire weapons method I have setup, seems to be cumbersome, with jumping from Playerfire to Shoot, for basically a very similar function. Any thoughts on the proper way to address this?

3.I had the hardest time figuring out how to get this program(the original to loop), while I think I have it looping well now, I'd like to know the best way to do so, as I'm really trying to learn as much as I can.

Thank you for your help!

Xatnys


sync on
sync rate 30
color backdrop 0
hide mouse

rem this are the graphics the game will be using
load image "yourship.bmp",1
load image "alienship.bmp",2
load image "orb.bmp",3
load image "greenlazer.bmp",4
load image "yellowlazer.bmp",5
load image "exp1.bmp",6
load image "exp2.bmp",7
load image "exp3.bmp",8
load image "exp4.bmp",9
load image "exp5.bmp",10
load image "exp6.bmp",11
load image "stars.bmp",50
load image "title.bmp",9999

load sound "phaser03.wav",1
load sound "boom2.wav",2

rem this makes an object for use as a background
Make Object Plain 4,600,56

rem this makes the object semi-transparent
ghost object on 4

rem this positions the above object
Position Object 4,0,-4,80

rem this puts stars.jpg image onto the object
Texture Object 4,50

rem then lock the object (although not exactly sure why) I'm learning
Lock Object on 4



rem now make background black to create starfield.
color backdrop rgb(0,0,0)



pf=0:score=0:Levels=1:Lives=3
ysx=50:ysy=200


rem this should put playership into starting position
Sprite 1,ysx,ysy,1

rem these are used to position the laser fire sprites
lx=sprite x(1)+20
ly=sprite y(1)+10

rem display's the sprite of the enemy ship
Sprite 3,500,200,2

rem this sets the title to the screen
Sprite 9999,10,10,9999



do
rem this sets up the Game score and info fields
set cursor 200,400: Print "SCORE:";score; " LEVEL:";level;" LIVES:";lives



rem this checks to make sure you are not closer than 100 pixels to the top of screen then moves you 10 units
IF upkey()=1 and ysy>100
dec ysy,5
endif

rem this checks if you are closer than 420 to the bottom of screen, if not it moves you down 10 units
IF downkey()=1 and ysy<420
inc ysy,5
endif
rem greenlazer.bmp's variables
lx=sprite x(1)+20
ly=sprite y(1)+10

rem re-draws the ship to the screen
Sprite 1,ysx,ysy,1
Sprite 3,500,200,2

rem function call to MoveAll()
MoveAll()

rem function call to MoveStars()
MoveStars()

rem Function Call to Shoot()
Shoot()

rem Function Call to Playerfire
Playerfire()

rem Refreshes the screen
sync
rem end of Do Loop
loop




rem If spacebar is pressed, go to function Shoot()
Function Playerfire()
If Spacekey()=1
shoot()
endif
endfunction

rem Has the texture on the object scroll to simulate motion
Function movestars()
Scroll Object Texture 4,0.01,0.0
Endfunction

rem if spacebar is pressed, display greenlazer.bmp play sound and
rem goto blowup()

Function shoot()
if Spacekey()=1
sprite 2,lx,ly,4
play Sound 1
Blowup()
endif
Endfunction


Function MoveAll()
If Sprite Exist(2)=1
If Sprite x(2)<640
LX= LX+30
Sprite 2,lx,ly,4
endif
endif

IF Sprite Exist(2)=1
IF Sprite x(2)>640
PF=0
LX= Sprite x(1)+20
LY= Sprite y(1)+10
endif
endif
EndFunction


Function blowup()
Sprite 2,lx,ly,4
If Sprite Exist(2)=1
If Sprite Hit(2,3)=1
delete sprite 3
play sound 2
for t=6 to 11
Sprite 3,500,200,t
wait 1
next t
delete sprite 3
Sprite 3,500,200,2
endif
endif
Endfunction

Hello.
Goodbye.
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 9th Jul 2004 23:56
It looks like you got the basics down pat. Unfortunately, I don't code in DBC, so I can only offer limited advice. Due to the small size of your functions, it might be better to write the code directly into your main game loop and save on the overhead of making the function calls.
--
TAZ

History did not begin with PONG. -- Greg Costikyan

Game Beavers
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 10th Jul 2004 04:51
Yeah, I'd also note that you have multiple tests for shooting in particular, which go into a long chain of functions that make the same tests. For example, playerfire() and shoot() are really just the same function, so having playerfire() is redundant. (one or the other should be removed imho.)

As for why the Lock Object 4 command, that's an easy one, it's to produce a fixed background of the stars. Locking keeps the image centered on the screen as it ignores the camera's movements, thus your star field is always in the same place as is scrolls.

Sprite collision collision not working, I think you'll find this is a result of how you are moving them, particularly sprite number 2. I can't locate your exact movement code in this source at this time because it's spread out in several places, but I do note that it appears to keep moving the sprite even when NOT shooting. Looks to me you need a flag of some kind to indicate whether or not the laser has been fired, then it might be able to detect properly again.

S.

Any truly great code should be indisguishable from magic.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 10th Jul 2004 05:54 Edited at: 10th Jul 2004 06:04
Hi again....

Now I know why. It's your main loop. After I removed the comments and extra space it became clear to me;



The result is -- you sprite never moves. Thus it can't travel far enough to hit the target. I know you want to "update" the laser position in the event the player moves in that section, but you're doing it wrong. Try this;



A better way is to set up a reset value for the laser map, for this code allows the player to move the laser even after it has been fired. (as they move) so you may want to re-think you process here...


Good Luck with your game...
S.

Any truly great code should be indisguishable from magic.
Xatnys
20
Years of Service
User Offline
Joined: 8th Jul 2004
Location:
Posted: 11th Jul 2004 06:11
Wow, thank you all for the excellent replies!

I'd scanned this forum and didn't see this thread so I thought it got deleted for being a double post.



Well I've not had the language long, but hope to get a real understanding of it. I've made several things so far. Not really original stuff, just taking tutorials and changing them, trying to make them more of a game than a tutorial. I'm hoping it's a good way to learn DB, because I think I learn best that way.

I understand what you mean about the small functions and how they're not needed. I really just tried making them to see if I could make 'em . And I've read on here that they're really the best way to go later on when doing big stuff, so you can upgrade code easy etc, so I figured I'd give 'em a go. I'll be moving back to a sub routine format with this project, great experience though!

As far as the redundant function (playerfire and shoot), glad you saw that, as it really has helped me to define the use of functions. Great stuff.

As far as the code that you looked into, I can't thank you enough, I've not tried it yet, but will be doing so in a minute and will post results a.s.a.p.

Thank you, both. I really am glad that DB has a good community, I'd read the forums for the past month or so. Seems there's a great willingness to help others understand, that was truly the reason I went with DB over others I was looking at(Pure and Blitz3D).

Xatnys

Thank you again!

Hello.
Goodbye.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 11th Jul 2004 06:17
(**smiles**)
No trouble at all... we're all learning.
S.

Any truly great code should be indisguishable from magic.
Xatnys
20
Years of Service
User Offline
Joined: 8th Jul 2004
Location:
Posted: 11th Jul 2004 06:25
I think I see what you mean about the refreshing of the laser sprite, and how it'd ruin the loop. Let me show you my latest bit of code, and tell you of the new(er) problem(s).





I think I still have the same, or similar problem. I know it's something in the loop, but I can't make out what. Here's what happens:

When you run the program, at first everything works the way it should. You can fire One shot, it will travel to the enemy ship, collision detection works, and my explosion routine goes into action.

That's when it turns bad.

After that first time through(or so it seems anyways), everything becomes "broken" :

The "fire" button will fire the laser bitmap, but you must hold the fire button down to get the laser to travel across the screen.

When it does make it across the screen to the target, the collision detection no longer works. It passes right over the enemy sprite and off the page.

I'm certain(well as certain as a noob can be) this has to do with my loop, but I'm unable to figure out where or why. I want to say that it occurs during the blowup subroutine, but it may also occur in other spots. I'm going to see what I can do as far as remarking out the lines you noticed, but that probably won't be the full solution.

If you could, would you look over the code and see if you can see the culprit?

Sorry to be so verbose, but I wanted to note all I could.

Xatnys


Hello.
Goodbye.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 12th Jul 2004 06:03
Well,

I am glad you took the time to make note of everything, because it makes it easier to look at, and hopefully duplicate, the difficulty you're having. Thus is makes it easier to find out what's going wrong, over say, "What wrongs with this code?" postings. I've grabbed the code for now and will look it over in a little while, then get back to you...

TTFN
S.

Any truly great code should be indisguishable from magic.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 12th Jul 2004 08:08
Well...

There are a couple of things going on here all at once, which is why you're having trouble locating the problem. First off, your shoot code has trouble;

If scancode()=44 and Sprite Exist(2)=0 Then Gosub shoot : pf=1

In this line, you go to the shoot function with PF=0 and then set it to 1 after shoot returns. But shoot on the other hand has this code;

Shoot:
If pf=1
lx=lx+10
sprite 2,lx,ly,4
sprite 1,ysx,ysy,1
Play Sound 1
endif
pf=0
gosub moveall
return

Meaning that only after the keyboard detection up above over runs a SECOND TIME (with PF=1) does the sprite get built by shoot. Then of course, since the sprite has been made, PF gets put back to zero, and we enter the moveall code.

Everything in moveall looks okay, so long as PF remains at zero, but then it gets set back to one when the keyboard detect finishes. Now the shoot code doesn't run until the sprite is deleted, so PF stays at one forever.

This means, even when the sprite is at the end of its journey and has not hit anything, it doesn't get deleted.


Now we move on to collision. Which first takes place in;

If Sprite Exist(2)=1
lx= Sprite x(2) +10
rem ly= Sprite y(1) +10
Sprite 2,lx,ly,4
If Sprite Collision(2,3)=1 then Gosub blowup
endif

Right after the call above for testing the keyboard. Trouble is, when you reach blowup you delete the sprite, so when the do-loop begins again it's not available for Score to detect again anymore;

Score:
If Sprite Exist(2)=1 ( no exist no more! )
If Sprite collision(2,3)=1 ( so it can't hit )
score=score+500
set cursor 200,400 : Print "Player Score:";score; " Current Level:";level; " Lives Remaining:";lives
else
score = score
endif
endif
return

And, because the laser doesn't show up, niether does the sore printing or anything. Indeed, the score priting only shows up when BOTH the sprite 2 exists and it collides with sprite 3, which can't happen if the detection has gone to blowup. And like I said before, so long as PF remains 1 moveall can't delete the sprite, so it is up to blowup to remove alone if a collision has occured.

From there on, things get really whacked. But here are some suggestions;
First, change the keyboard line to set PF to 1 before calling the subroutine of shoot.

Second, call SCORE from inside the detection code when you call blowup, but do it before blowup can remove the sprite from memory.

Or third, and better, when you detect the collision and call blowup, add the change of score there instead. This way score does not need to do anything with the sprites at all, it just prints the score and that's it.

And forth, change the fire and movement processes to be sensitive to the value of PF rather than the sprites themselves, because if you want to create more than one blast of the laser you're going to have a real mess on your hands.

And finally, you must reset the PF value in the manner you're doing things, or simply remove the "and pf=0" from the moveall code. Since you have already done the detection to see if the sprite exists and is beyond the distance limit, there's no reason to check PF.

Well, I hope this helps... good luck with it!
S.

Any truly great code should be indisguishable from magic.
Xatnys
20
Years of Service
User Offline
Joined: 8th Jul 2004
Location:
Posted: 12th Jul 2004 09:45
SandraD,

Hello again. Thanks for such a in depth reply, I'm not sure I understand everything you've mentioned, but I'll be trying to implement it, hope I'll learn something in the process. I think I may have bitten off too much,too soon, and that made things even more convoluted.

I set that project aside for the day, to reflect, and mainly, to wait to see your response. I'm glad I did, I think with what you've shown me, I may be able to get it to some form of normal function... maybe.

To keep moving along, even though this project was on hold, I went through the codebase and pulled out a simple 2D Pong game, and have spiced it up a bit by giving it levels, sound effect(s) adding my own weak form of A.I., etc etc. I've attached it here:

Just in case you wanted to look it over. Notice I have 2 AI "routines" one set marked out, I think it has promise, but at the moment it is not very "smart". heh The one in use however, really does do an ok job, but it could be better.

I hope to keep the 2 player functionality, giving the user a startup screen which displays controls, as well as asking if they wish to play a single or multiplayer version of it. Also I'd like to get rid of the simple boxes(paddles), and replace them with a 3D object and texture it, unless of course I find a way to texture these. But the 3D and an addition of a nice background probably would be nice.

Anyways, I really appreciate your advice, and will be looking over that post in *much* more detail in a moment .


Xatnys

Hello.
Goodbye.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 12th Jul 2004 10:44
Sounds like a plane....
S.

Any truly great code should be indisguishable from magic.

Login to post a reply

Server time is: 2024-09-22 16:26:04
Your offset time is: 2024-09-22 16:26:04