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 / Everyone should start with Pong..

Author
Message
Garfield1983
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location:
Posted: 23rd Jan 2003 17:45
Or so i've heard..

As this is my first serious attempt at a game (I have lots of ideas but no actual code for them!) i'm having some difficulty..

oO(With Pong? Pong!!)

Yes, i hate to admit that i'm having difficulty with Pong!
What seemed a simple starting point at first has turned out to be mind-boggling!

For now, I just want to have a ball that 'bounces' off each of the four 'walls' (The screen edges).

When I tried to do this the ball would hit the edge of the screen and stay there, I realised this was because my original thought doesn't actually work:

xpos = 320
speed = 5

xpos = xpos + speed
If xpos = 640
xpos = xpos - speed
EndIf

Something like that, I realise that this doesn't work because the first line is always sending the ball towards the right of the screen=S

Does anyone have any pseudocode that might help me? I'd rather try to convert it to code myself rather than actually copy actual code.

I'm going away now to think on this, hopefully i'll get it without any help, but any response would be greatly appreciated.

Thanks.
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 23rd Jan 2003 18:11
I see your problem. When the ball reaches 640 then it starts to bounce back but then the ball is less than 640 so it goes formard again. Also wherever the ball is on the screen it goes forward by the speed but if it hits the walls then it goes backwards and forwards at the same time! This keeps it in the same place. Heres my pseudo code...

move ball in the direction of movement at the specified speed
if ball hits wall then change the direction of movement to the oppsite direction

that kind of thing

Yum! Yum! Yum! Yum!
Troan
21
Years of Service
User Offline
Joined: 22nd Jan 2003
Location: Inner thought about nothing
Posted: 23rd Jan 2003 18:57
how did u figure out to start to make pong all i can do is make stupid number games the tortiles dont work well!

-Troan
The Darthster
21
Years of Service
User Offline
Joined: 25th Sep 2002
Location: United Kingdom
Posted: 23rd Jan 2003 19:27
When the ball hits the edge of the screen it's speed is reversed. Change the speed, not the position.

Other notes on Pong:
You'll need to do some collision with the paddles, the ball only bounces when the paddle and the ball are in the same area.
You'll also need to do scoring, you'll need two score variables (one for each player) which are incremented appropriately when the ball collides with the left or right of the screen.

I tend to have lots of code, but no ideas...
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 23rd Jan 2003 21:38
Troan, try experimenting with the 2d commands like circle and box, try changing their position and making them gradually bigger and smaller, changing their colour, try to make them follow the mouse and make them bounce off the edges of the screen. I learnt tonnes of this stuff on the PS2 version of basic - Yabasic, its pretty much the same as the darkbasic 2d commands so it should be pretty easy. I started off with text games, I dont think they're crappy, I was pretty chuffed with mine when I'd finished. The first game I made was Space invaders (except with squares, yabasic is pretty limited with the amount of stuff it can handle at one time), but I started off by making menus where you had a bouncing circle, a triangle that changed colour when you pressed the triangle button and a background that gradually faded from one colour to another. You dont always have to make something that has a point, just experimenting can be fun, no matter how crap other people think it is.

Yum! Yum! Yum! Yum!
Stevo
21
Years of Service
User Offline
Joined: 4th Oct 2002
Location:
Posted: 23rd Jan 2003 22:30
I totaally agree even the smallest things sometimes seem really hard.
It's a bit hard to tell you, but not tell you (-since you want to work it out for your self) DBHeaven.com has the code for in his tutorial section.

can any one help me with arrays? I havent got a clue!

MattIsFun
21
Years of Service
User Offline
Joined: 4th Jan 2003
Location:
Posted: 23rd Jan 2003 23:30
You must have variables for the ball position, and then variables for the ball velocity.

ballx# = 0
bally# = 0
ballvelox# = 0
ballveloy# = 0

rem movement
ballx# = ballx# + ballvelox#
bally# = bally# + ballveloy#

if ballx# > 640
ballvelox# = -1

rem and so on...

The Darthster
21
Years of Service
User Offline
Joined: 25th Sep 2002
Location: United Kingdom
Posted: 23rd Jan 2003 23:38
Yeah, sure. Arrays are tables of data that can be used for just about anything. You can store a load of data which is linked in some way, for example, attributes of an object.

For example:
I want to store the coordinates of an object. I can do this using an array. First we DIM the array

DIM obj_pos(3)

DIM creates the array ready for you to put data in. Obj_pos is the name I've given to the array. 3 is the number of data items this array will store.

Arrays are as flexible as variables, you can store any data anywhere in an array, so long as you know where it is and can retrieve it again You reference data in the array by it's index number, the number you put in the brackets. When you are DIMming the array, the number is the maximum number of data items you can hold. When you are accessing the array, the number is the index (address) in the array that you want to access. The numbers range from 0 to the maximum minus 1. (array(3)) would have data in array(0), array(1) and array(2))

I want to store coordinates, so I'll put the x y and z coordinates into different places in the array.
obj_pos(0) is the x position
obj_pos(1) is the y position
obj_pos(2) is the z position
I don't have to tell the program that, but it's useful to put in as comments so I know what I'm doing later on.

Now I'll put some data into the array.

obj_pos(0)=100
obj_pos(1)=-50
obj_pos(2)=25

This code represents the coordinates (100,50,-25), but is actually meaningless until you interpret the data. All this code has done has put the values 100, -50 and 25 into different places in the array, but the way you interpret it determines what this data is.

Now I'll interpret the data in the array and use it to position an object, object number 1. The syntax is POSITION OBJECT object number, x, y, z. From earlier, we know that the x position is held in index 0, the y in index 1 and the z in index 2. The program doesn't know this, it's just the way I designed the array to be interpreted.

POSITION OBJECT 1, obj_pos(0), obj_pos(1), obj_pos(2)

This positions the object using the values in the array. Since the data in the array is only numbers, we can interpret it in different ways.

TEXT 0,0,"The number in obj_pos(0) is: " + STR$(obj_pos(0))

You can use the array name and index number in brackets anywhere you would ordinarily use variables or numbers. Arrays are just a convenient way of grouping data.

Once you have one dimensional arrays sorted (arrays with only one index number) you can move onto multidimensional arrays. Using the above example, what if I want to store several object positions?

DIM a different array, a 2 dimensional one.

DIM obj_pos(5,3)

This sets up an array with 5 elements, each element has 3 data items associated with it. I can now store the x y and z positions of 5 objects using one array. All I have to do is change the first index number to input the data for different objects.

obj_pos(3,2) would reference the z position of object 4 (remember even though the indexes range from 0 to 4, the object numbers must be above 0)
obj_pos(1,2) would reference the z position of object 2
obj_pos(0,1) would reference the y position of object 1

Multidimensional arrays are useful in that you can use for...next loops to go through all the data quickly. Above I used:

POSITION OBJECT 1, obj_pos(0), obj_pos(1), obj_pos(2)

to position object 1 at the coordinates I put into the array. Now I can use:

FOR i=0 to 4
POSITION OBJECT i+1, obj_pos(i,0), obj_pos(i,1), obj_pos(i,2)
next i

to position each object (1 to 5) at the positions stored in the array.

Was any of that useful to you? Just ask if you need any of it explaining.
Troan
21
Years of Service
User Offline
Joined: 22nd Jan 2003
Location: Inner thought about nothing
Posted: 24th Jan 2003 03:16
Thx for the help

-Troan
Garfield1983
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location:
Posted: 24th Jan 2003 03:59
Sorry, Darthster, I don't understand what you mean about recersing the balls speed=S

Yeah, i'm leaving the scoring etc til after I have two paddles and a ball working properly, i'm also going add other features like powerups=)
Garfield1983
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location:
Posted: 24th Jan 2003 04:00
*Reversing* sorry
The Darthster
21
Years of Service
User Offline
Joined: 25th Sep 2002
Location: United Kingdom
Posted: 24th Jan 2003 19:57
What I mean is this.

The ball travels by a speed (5), you wrote that yourself.
This means in every cycle it moves 5 units to the right.
xpos = xpos + speed
When the ball hits the end wall, you want it to go the other direction
Do this by making the ball move 5 units to the left = -5 units to the right every cycle.
Make the speed the negative of itself, since you might not know the speed.
speed = speed * -1
This will make speed=-5
Then, by doing xpos = xpos + speed
You are essentially doing xpos = xpos - 5
This moves the ball -5 units to the right every cycle.

This can very easily be modified to get the ball bouncing at both ends, and top and bottom. Basically, when the ball hits an edge, the speed in the direction of the edge is reversed.
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 24th Jan 2003 21:10
Hey, I made a pong game in 30 mins just for fun. It may have a few errors but there you go. It calculates at what angle the ball bounces off of the paddle by finding how far it is from the centre. If its nearer the edge then the ball bounces off at a bigger angle. Dont read the code if you want to figure it out yourself.



Yum! Yum! Yum! Yum!
Kangaroo2
21
Years of Service
User Offline
Joined: 26th Sep 2002
Location: United Kingdom
Posted: 24th Jan 2003 22:43
Hey if you like games like this check out my bounce game, it took around 4 hours and was my first tester program for DBPro



Download it at http://www.myminehead.com/bounce.jpg

I would release source code, but this kinda game is so simple, it would behove you to teach yourself as a learning experience

Epidemicz
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 25th Jan 2003 01:12
Heh, I think Kangaroo2 means:

http://www.myminehead.com/bounce.zip

The other one goes to a JPEG, heh.

I am the very disease you pretend to be.
Necrum
21
Years of Service
User Offline
Joined: 31st Dec 2002
Location:
Posted: 25th Jan 2003 05:38
I am also trying to do pong, and I am also having trouble. I could easily just take someones code and call it my own, but what fun would that be?

Right now Im having trouble getting the ball to move on its on. Any help would be greatly appreciated.

Just when you thought life was done beating your sorry ass down, it invites friends to help.
Kangaroo2
21
Years of Service
User Offline
Joined: 26th Sep 2002
Location: United Kingdom
Posted: 25th Jan 2003 07:02
lol yeah sorry! Thanx Epidemics

personally I gave the ball an x and y position variable, and had another to choose which direction it was going in. Then when a collision is detected, you know which way to bounce it

CarlTaylor
21
Years of Service
User Offline
Joined: 13th Jan 2003
Location: United States
Posted: 25th Jan 2003 08:10
id suggest not having separate speed and direction, but use a slop (rise and run) format. This way if it bounces off say the right or left wall you can just invert the run movement and leave the rise movement the same. If it bounces off the paddle or the top wall you can invert the rise. This simplifies things, and simplification is going to be very important im thinking.

baseball = life
Garfield1983
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location:
Posted: 25th Jan 2003 15:14
Ah thanks Darthster, it works, it seems so simple now of course!=S

So I now have a ball bouncing back and forth across the screen, great!lol Will let you know how I get on with the rest, thanks everyone=)

Yeah, thanks for the code Hamish but I'm forcing myself to not read it until i've finished my version, but thanks anyway=)
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 25th Jan 2003 17:13
Just to give you an example of what to do.. thats all!

Yum! Yum! Yum! Yum!
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 25th Jan 2003 17:15
Oh, yeah, kangaroo. Your xmas demo gets a bit slow on the third level, dunno why though, by pcs 600mhz.

Yum! Yum! Yum! Yum!
Garfield1983
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location:
Posted: 28th Jan 2003 01:46
Mmm..it's me again!

I've hit yet another roadblock - I saw this one coming a mile off - my versiopn of Pong is really starting to take shape, i've kept it simple for now as i just want to finish a basic game, but i'm still stuck with the balls movement.

It 'bounces' back and forth off the paddles, but I know that in Pong it needs to move at an angle aswell (Know what I mean?=S). I was of the thinking that I could kinda somehow get the angle the ball is coming in at and then mirror it (somehow!)to send it out the other way again..umm..as you can tell i'm pretty stuck..sinking fast even!=S

Either the solution is really simple or quite complex, either that or i'm just dumb=)

Sorry to bother you guys again, but any hints would be greatly appreciated!
The Darthster
21
Years of Service
User Offline
Joined: 25th Sep 2002
Location: United Kingdom
Posted: 28th Jan 2003 20:32
As well as having the x speed, as you already do, you can also have a y speed. This will move the ball up and down the screen. You can do collision with the top and bottom of the screen the same way you did it with the sides. The y speed won't change when the ball bounces at the sides of the screen, and the x speed won't change when the ball bounces at the top or bottom of the screen.
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 28th Jan 2003 21:09
also to make it a bit more interesting, I increased the angle (y movement) at which the ball came off at according to where it hit the paddle, maybe you could do this?

Yum! Yum! Yum! Yum!
The Darthster
21
Years of Service
User Offline
Joined: 25th Sep 2002
Location: United Kingdom
Posted: 28th Jan 2003 23:03
I usually add the velocity of the paddle to the velocity of the ball, but having the velocity change proportional to the displacement could be interesting. Let us know if you need help.
Garfield1983
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location:
Posted: 29th Jan 2003 17:18
Yeah i'm not sure how to use the yspeed. The code I'm using to move the ball across the x axis is as follows:



And I thought if I added in a line to increment the y speed (when the ball hits the paddle) it would work but it actually just increments the balls height a little each time and not in a steady diagonal as I had hoped=S The altered code is this:



As always, I'd be so over-joyed (That's a word isn't it?) with any help=)

Later
Garfield1983
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location:
Posted: 29th Jan 2003 17:19
Sorry, should have mentioned that the ball is Sprite 3 and Sprite 2 is one of the paddles (Player 2s in fact). Cheers.
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 29th Jan 2003 17:52
try hello world first before u make pong.

smelly

vivi
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 29th Jan 2003 18:04


Slow down......(cough)......(pant)....(weez).....im nearly there. Finally a subject i can keep up with.

WHY WAIT FOR GAMES.......MAKE THEM COME TO YOU
>>>>www.sharksoft.scripterz.com<<<<<
THANKS IN ADVANCE
The Darthster
21
Years of Service
User Offline
Joined: 25th Sep 2002
Location: United Kingdom
Posted: 29th Jan 2003 18:53
If you increment the y position when the ball hits the paddle, then the y position will only ever change when the ball hits the paddle. Add the y speed every cycle, not just when the ball hits the paddle. Then it will move in a straight diagonal line like in most pong games.



Watch this not work because of the comparison operators.
Daz
21
Years of Service
User Offline
Joined: 26th Dec 2002
Location: United Kingdom
Posted: 29th Jan 2003 19:51
PING, lol, I'm trying to that too. I can make the paddle go up and down using IF UPKEY() = 1/IF DOWNKEY() = 1 so on, and I figured out where to put the paddles, it's when it comes to ball movement, that's the hardest part of pong, seeing as I don't understand it. If you want the negative for speed use the ABS command I think.
I'm going on those new tutorials Rich posted, they better be better than per usual.

DarkBASIC Professional is the best programming utility.
8/10 Housewives agree!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 29th Jan 2003 20:29
If you want to negate the speed, subtract it from 0. If you need to negate a lot of values, you will find that this is quicker than multiplying by -1.

ABS() just gives you the positive equivalent of the number you give to it...hmm, crap explanation.

ABS(-10) => 10
ABS(10) => 10 too.
Daz
21
Years of Service
User Offline
Joined: 26th Dec 2002
Location: United Kingdom
Posted: 29th Jan 2003 20:57
yeah, so if you do a negative 1st, its ok?

DarkBASIC Professional is the best programming utility.
8/10 Housewives agree!
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 30th Jan 2003 03:46
what about a paddle that slid around 3 of the walls?

that would be cool

Kangaroo2
21
Years of Service
User Offline
Joined: 26th Sep 2002
Location: United Kingdom
Posted: 30th Jan 2003 14:48
Sorry it prolly does run slow on a 600Mhz machine because of the moving matrix and transparancies - the reason why level 3 would mess it up is because its then it increases the speed of the ball, and therefore processing power I've never4 tried running it on a lower system than 800Mhz GF2. In the full version (which will be part of my compilation thingy) there are options to change the level of detail and screen res to 320x240 for slower machines Anyway good luck to every1 attempting this - Oh and Vivi? Nice 1 you're learning fast

Stevo
21
Years of Service
User Offline
Joined: 4th Oct 2002
Location:
Posted: 31st Jan 2003 00:29
Thanks for the help on arrays Darthster, sorry I didn't reply earlier (I only really get on the net every 2-3 days).
One of the things I don't understand is in the tutorials they say about entering the data, but then most of the programs I've seen just use a for to loop.

Ps your bit on adding the speed of the paddle to that of the ball is good, that way you can actually give the ball a good wack with the side of the bat which is fun.

mightymax
21
Years of Service
User Offline
Joined: 31st Jan 2003
Location:
Posted: 31st Jan 2003 16:22
I'm new to DB and here is the first (and only) game I ever made (a pong clone). It is actually pretty similar to Hamish's (but inferior, and it took me 3 hours). You apply "spin" to the ball by moving the bat as you hit the ball, so the angle of the ball depends on the speed of the bat. Maybe you'll find it useful? Excuse the messy code, like I said, I am a newbie.



May not help, but then again, maybe it will. Try just changing the x and y velocity when the ball hits a paddle, and do your moving and drawing after you have detected collisions? I don't have a clue what I'm talking about though so I'm probably talking rubbish.

REM get a signature off the user
INPUT "Type your sig here",sig$
PRINT sig$
xmen
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location:
Posted: 1st Feb 2003 01:19
[test]
xmen
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location:
Posted: 1st Feb 2003 01:20
xmen
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location:
Posted: 1st Feb 2003 01:21

Login to post a reply

Server time is: 2024-09-19 03:03:08
Your offset time is: 2024-09-19 03:03:08