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 / Pong Game Not Working

Author
Message
Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 13th Apr 2007 08:04 Edited at: 13th Apr 2007 08:11
Hello,
I was wondering if anyone could please help me out here.
I started with 3D like every one says not to (although my 3D programs did work),But then I came across TDK's tuts.
So now I am working on pong.
Anyway, this is the code so far and nothing happens!



(Oh...And the ball is supposed to bounce off the left and right walls until I get the paddles working)

It would be great if someone could please help me


Thank You!!!

Attachments

Login to view attachments
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 13th Apr 2007 09:13
At first glance, (it's gone 7am here and after all night at the keyboard I'm tired), your ball starts off at X=500 Y=500 and as you haven't set a screen display mode it's using the default 640x480 mode - making the ball way off the bottom of the screen.

Set the BallposY to 240 (the centre) and take it from there...

TDK_Man

Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 13th Apr 2007 09:56 Edited at: 13th Apr 2007 09:57
LOL I knew that it would be you to awnser first!
7am! Its 4:53pm here in Australia!

That seems very, very logical and now that you brought it up, it clicked in my young brain.
(My very ignorant young brain. lol)
I tried that now and I also tried a range of other smaller numbers but it still dosen't work (The screen is as black as ever)
Any other ideas?

Thank you for the help!
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 13th Apr 2007 11:11 Edited at: 13th Apr 2007 23:45
I can't see about your black screen... but in your bouncing off walls check, the ball doesn't change it's direction when it hits a wall, so it will in theory, never bonce off the the top, or left walls...
Comrade Robski
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location:
Posted: 13th Apr 2007 14:05
You seem to be missing sync commands in your code

Try this: add SYNC ON somewhere before your main loop, and add SYNC just before the LOOP command.
LBFN
18
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 13th Apr 2007 15:27
Comrade is right, you need to turn the sync on and have the sync command within your do - loop. With the x coordinate at 500, the ball zips off the screen pretty quick. So you can see it, for starters anyway, you might want to set the x/y coordinates to half of the screen width/height until you have the code in place to see if the ball has gone out of bounds and bounces accordingly.

LB
Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 14th Apr 2007 04:22
Thank you Guys!! The screen is no longer black.

SimSmall, if you have time could you please help me figure out the bouncing. Here is my code:






Thanks again guys!
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 14th Apr 2007 04:49 Edited at: 14th Apr 2007 04:54
As the ball moves around, it's X and Y screen position are stored in BallposX and BallposY.

The Y co-ordinates run from 0 at the top to 600 at the bottom (assuming you are in 800x600 screen mode).

To bounce of the top wall, all that is needed is a check to see if the ball's Y position (BallposY) tries to get below a certain value. In your program, you are checking to see if it goes less than 5.

At this point, you need to tell the ball to start moving down and keep moving down until told otherwise. Yours only tells it to move down once - at the moment it gets lower than 5.

The direction the ball moves is stored in BallDirY - initially set to 2 which makes it move down. You'll notice I've changed the stating value to -2 so it initially moves up and hits the top wall first.

What you therefore need to do is alter the value of BallDirY when you get below 5. (The values used for the right and bottom wall depend on the width and height of your sprite).

If a value of BallDirY=2 makes it move up, then to make it move down you set BallDirY to -2 (minus 2). To negate any value in a variable you use VariableName = 0-Variablename which in your program gives you:



Using this method, the exact same line will work for reversing the Y direction at both the top and bottom of the screen. You also do not have to alter the line

BallposY=BallposY+BallDirY

as that still works regardless of whether BallDirY is 2 or -2. (BTW you could have used Inc BallposY,BallDirY instead).

Here's your program modified so you can work on it:



TDK_Man

Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 14th Apr 2007 06:16
Thank you TDK for taking the time to explain!
I understand the bouncing and why I needed to decrease the ball positionY variable.

I made the bat sprite, and I added controls. Now I need to understand the collisions. I used these lines but nothing happend.



Can you please help?

Here is my full code:



thank you!
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 14th Apr 2007 08:40 Edited at: 14th Apr 2007 08:42
Quote: "I understand the bouncing and why I needed to decrease the ball positionY variable."


I don't think you do, because you're basically doing the same thing wrong again!



This basically says that if sprite 1 (the ball) hits sprite 2 (the bat) then add 4 to whatever BallDirX currently is.

Remember what I said before - BallDirX can either be 2 to move the ball to the right or -2 to move it to the left. You need to use:



...to make it reverse direction. (You can of course alter the value 2 in the variables BallDirX and BallDirY at any time to speed up or slow down the ball and the BallDirX=0-BallDirX line will still work perfectly).

You can also use ballhit=sprite hit(1,0) and this will return the number of the sprite that the ball hits rather than just get a 1 back when you hit a specified sprite. This allows you to use the same collision command line to check for collision with another paddle if/when you add one.

TDK_Man

SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 14th Apr 2007 13:05 Edited at: 14th Apr 2007 13:07
Quote: "Am I right in thinking that this snippet only checks collision for one bat?


"


Regardless, the same coding would be used in case of collision with either bat. Why?
What this says is:

Ball has hit sprite 2 (one of the bats, we'll call it the bat at the right of the screen for now) BallDirX is 2, so the ball would move right, but the collision then activates an 'if' block which changes this value from 2, to -2 as 0 - 2 = -2
Good quality coding shows through, as the same if block will convert from the ball moving left, to the ball moving right:
0 - -2 (double negative: so 0 + 2) = 2

so, for the checking for collision with bats:

assuming there are two bats (sprites 2 and 3) you might type:



You may notice, the if statement has changed slightly... I'm checking for two conditions (the ball colliding with either bat). Because they are seperated by an 'or' the ball only needs to hit one bat to activate the if.

You can also use a very similar if statement with an or in it to check if the ball is going to bounce off the top, or the bottom of the screen. reading the above snippet, you should be able to figure it out by yourself. You're far more likely to remember this, if you can figure this oone out yourself, but help is still here if you can't...
Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 14th Apr 2007 13:06
Ahh...Got it!
I got the collision working now!
I also added a messy score count.
But now there is a problem with it, it prints alright but when the number changes, well... you'll see (I can't really explain)


Also could you please help me with what happens when you miss the ball. I want it to appear in the middle when you miss, and then come toward the paddle.

Thanks again for taking the time TDK!
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 14th Apr 2007 13:10
I think I know what it's going to be... it just writes the score over the top of the old one... this is where the sync on bit comes in... if you've not already done so:

at the top of your program



and then



cls clears the screen, so gets rid of any previous text that was written on the screen
Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 14th Apr 2007 13:15
SimSmall. I don't know who posted that quote, but I want it to only have one bat. In the menu you can choose which setting: Single play(against pc), multiplay, and scoring points against the wall. Regardless, couldn't you just type:



?
Thank you for the help!
Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 14th Apr 2007 13:17
I've got this:



what does that do?
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 14th Apr 2007 13:21 Edited at: 14th Apr 2007 13:24
The quote was from TDK, I just (incorrectly in this case) assumed that like many other pong games, it was two player, or that in 1 player, you played against the computer with it's own bat...

And, yes you can type the if as > 0 -- but actually, you don't even need that. by specifying both numbers in the sprite hit expression, it will only return 0 or 1. if sprite hit(x,y) = 1 is the same as writing if sprite hit(x,y)

I usually write = 1 on the end just so there's absolutely no mistake. Also, there may be time you want to check that something isn't colliding in which case, you would need the = 0 on the end

Edit:
your sync related post means:

sync on : only draw the screen when the program reaches a sync 'statement'
sync rate 60 : don't draw the screen more than 60 times a second
cls 0 : clear the screen and fill it with colour 0 (black)

that doesn't need changing, but the sync rate will affect the speed of your game. Try experimenting with that value until you find a value you're happy with...
Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 14th Apr 2007 14:36
The score is all good now! (I added the cls)

Just on the subject of screens and sync, what does fps mean (not first person shooter)

Could you please help me with what happens when you miss the ball. I want it to appear in the middle when you miss, and then come toward the paddle.

Thanks
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 14th Apr 2007 15:28
fps = frames per second... sync rate basically limits this... it's not perfect, as setting the sync rate of 60, often results in the frame rate being 64 frames a second, but it's close enough.


When you miss the ball it's a matter of placing the ball back at the centre of the screen, which can be hard coded value, although there is a better way...

BallPosX = screen width() / 2
BallPosY = screen height() / 2

this is going to position the ball at 400,300 since in 800x600 resolution, screen width() will return 800, then just divide it by two. Likewise screen height() will return 600

Your code already places your ball sprite at the correct position, and to make it move the right way, you just need to set the BallDirX and Y variables, to either +2 or -2 (Unless you want to change how far it moves)


With the introduction of screen width() and screen height(), you may also wish to change the wall-boundary checks for:
5 or screen width() - 5 for left / right
and 5 or screen height() - 5 for top / bottom... simply because if you want to change the resolution, you'll only have to change the wall boundaries again.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 14th Apr 2007 18:39
SimSmall is correct when he suggests you can use:



However there are benefits to using the method I suggested - maybe not so much in this program, but in other future programs. If you remember, earlier I suggested you use:

ballhit=sprite hit(1,0)

The first number (1) is the number of the sprite you are checking for collision with - the ball.

The second number is the number of the sprite you want to check collision with. Note that collision with ALL other objects are not detected.

So, as SimSmall says you need two checks - one for each paddle.

If however, you use ballhit=sprite hit(1,0), it doesn't matter what sprite the ball bumps into, the number of that sprite is returned.

This means that you only need one collision test - you simply use the value returned to decide what to do. For example:



This does exactly the same thing, but would work for any sprite the ball encounters. (You may decide to add difficulty levels to the game by introducing wall obstacle sprites randomly in the playing area. This would automatically work when hitting them too).

Programming-wise, this isn't any better a solution than SimSmall's for this particular program - it just gives you a bit more flexibility for the future.

Quote: "The quote was from TDK"


Really?

When did I say that?

TDK_Man

SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 14th Apr 2007 20:06 Edited at: 14th Apr 2007 20:07
Quote: "When did I say that?"


Right here sir, don't know if you copied that from somewhere else, but that's where I saw it...
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 14th Apr 2007 22:59
Now I am confused!

Though it doesn't take much these days!...

Nowhere in that image does it say "Am I right in thinking that this snippet only checks collision for one bat?".

What's more, I can't remember typing it either. I did edit the post, but only because I forgot to put the two bits of DB code at the bottom in bold. I didn't change any text.

Not that it really makes any difference in this case as it wasn't anything important, but the quote attributed to me isn't mine.

TDK_Man

SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 15th Apr 2007 00:22
ah, no you didn't say that, no .. I'm confused... I quoted the snippet... but put the tag in the wrong place

give me a "dunce" hat...
LBFN
18
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 15th Apr 2007 00:40
@SimSmall

Sorry . . . I just can't help myself.

Just kidding!!!


LB

Attachments

Login to view attachments
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 15th Apr 2007 00:59


I'm the last one qualified to take the mickey - if I remember correctly, both SimSmall and I are both getting on in years and prone to the odd 'bout' of confusion now and again.

It could easily have been me...

TDK_Man

Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 15th Apr 2007 04:42
LOL

There isn't much more to a pong game after that is there?
And after I've finished, it what is the next thing I should program?

Thanks for the Help guys!

Why buy a game when you can make one?
LBFN
18
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 15th Apr 2007 06:24
@ Binary

Typically, pong games are played against a human [well mostly] opponent or against the computer. You chose to go with the one bat, but where's the challenge? A good game will offer challenge to the player and be entertaining even to those watching if it's really good.

You are using sprites (2D) in your game and not 3D objects. A pong game that utilizes 3D objects would be more of a challenge to you, though I am not recommending you go there. I would suggest you put some things into the game that will add challenge to it before you start your next project.

LB
Binary Coder
18
Years of Service
User Offline
Joined: 26th Feb 2007
Location: Queensland, Australia
Posted: 15th Apr 2007 12:32
Quote: "In the menu you can choose which setting: Single play(against pc), multiplay, and scoring points against the wall"


There will be 3 game modes when i'm done, so there will be challenge

I want to try making a 2D platformer which are in my opinion really fun, but will that be too difficult?

Why buy a game when you can make one?
LBFN
18
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 15th Apr 2007 16:15
2D platformers are fun and can be challenging to make. You could do a search for "tiles" and there would be a lot of info you could review as to how to go about making one.

Good luck.

LB

Login to post a reply

Server time is: 2025-05-27 22:51:50
Your offset time is: 2025-05-27 22:51:50