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 / My First Pong Game

Author
Message
Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 18th Dec 2003 18:48
I'm creating my first pong game any ideas on where I should start?

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 18th Dec 2003 18:49 Edited at: 23rd Mar 2005 21:22
.

My Showcase - It's DBpro-tastic
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 18th Dec 2003 19:05 Edited at: 23rd Mar 2005 21:24
Chris 2005 - 11000 views and still going strong!!

------------------------------

First of all you need a bat. This will be a rectangle.
The code for making a rectangle is make object box
this is followed by four numbers:

First Number - The Object Number
Second Number - Its Width
Third Number - Its Height
Fourth Number - Its Depth

So make object box 1,1,1,1 would make a cube, 1 unit high, wide and deep.

You want a pong bat, which is longer in one direction.
Use this code: make object box 1,1,1,3
It should look like this:


My Showcase - It's DBpro-tastic
CattleRustler
Retired Moderator
20
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 18th Dec 2003 19:30 Edited at: 20th Dec 2003 04:27
...remeoved post.

-RUST-

VB.NET makes me all goose-pimply!
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 18th Dec 2003 19:31 Edited at: 18th Dec 2003 19:34
Now you need to do some positioning and camera work.
Add this code to the program:



position object puts an opject in a specific location in space. The first number is which object you want to move, the next three are the x,y and x coordinates of location.
The code I have said position object 1 (the bat) five places to the left of the centre.



Position camera is the same, but with the camera. Point camera points the camera at a specific point in space.

Try changing the numbers to get slightly different results.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 18th Dec 2003 19:40 Edited at: 18th Dec 2003 19:49
Now you want to make the other bat and position that.
This code will do that:



You may want to colour the bats different colours.
Add this code:


The rgb() bits are how DB does colours. It makes them from a mixture of red green and blue. Each amount is a number between 0 and 255.

rgb(255,255,255) is white
rgb(0,0,0) is black
rgb(255,0,0) is bright red
rgb(100,0,0) is dark red

Now you can add the ball. Put this code with the other make object commands.

make object sphere 3,1 will make your third object, the ball, 1 unit wide. Colour it as well.

Now you should have this code (I rearranged it a bit):



It should look like this:


CattleRustler
Retired Moderator
20
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 18th Dec 2003 20:05 Edited at: 20th Dec 2003 04:27
...removed post

-RUST-

VB.NET makes me all goose-pimply!
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 18th Dec 2003 20:21
OK. Now you have everything you need and you can start programming it. First of all you need to be able to move the paddles.

Now we need to introduce the main loop. This is the part of the code that is repeated again and again, several times a second. It looks like this:

do

loop

The 'do' is the start of the loop. After this you put all the commands that need to be repeated. The 'loop' tells DarkBASIC to go back to the 'do' and start again.
Put the position object commands inside the loop.

Also, change the z value for a variable.
A variable is a number that can change. in DarkBASIC they look like this:

variable#, x#, player1ypos# etc.
They all have a '#' at the end.

Changing the z position for a variable mean the bat can move forward and backwards.

Now you should have this:



It won't act any differently except now the game won't quit until you press escape.

Now you want the bat to move up when you press up:-
IF up key is pressed THEN move paddle up.
Because the paddle is always positioned at player1pos#, changing this will move the paddle. Therefore:
IF up key is pressed THEN player1pos# is a bit more than what it was.

In DB language this is:


add this code to the loop. Now you can move the paddle upwards.
This code will make it move downwards:


Change the values around and see what it does until you fully understand what the code is doing. Try changing the if upkey()=1 to if upkey()=0.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 18th Dec 2003 20:25 Edited at: 21st Dec 2003 11:54
removed post...

CattleRustler
Retired Moderator
20
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 18th Dec 2003 21:19 Edited at: 20th Dec 2003 04:28
...removed post.

-RUST-

VB.NET makes me all goose-pimply!
Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 19th Dec 2003 02:48 Edited at: 26th Dec 2003 02:49
removed post..

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 19th Dec 2003 02:53 Edited at: 26th Dec 2003 02:49
Removed post...

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 19th Dec 2003 02:56 Edited at: 26th Dec 2003 02:50
removed post

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 19th Dec 2003 03:19 Edited at: 8th Jan 2004 04:33
remove!

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 19th Dec 2003 11:15
Ok. Let's continue:
At the moment you can just move of into space and below the camera. To stop this we need to add a bit to the movement commands.

We want it so you can't move past 5 units away.
In other words, you can only move away if you are less than five units away when you press the up key. This code will do that:

IF upkey()=1 AND player1pos#<5 then player1pos#=player1pos#+0.5

and for downwards:

IF downkey()=1 AND player1pos#>-5 then player1pos#=player1pos#-0.5

This will restrict the paddle's movement to the playing area. If you want a bigger playing area, then change the 5's to another number. If you do this then you may need to adjust the camera position.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 19th Dec 2003 11:21 Edited at: 19th Dec 2003 11:22
You may notice that the movement is very jerky, so now would be a good time to explain about syncing.
A sync is when the computer updates the screen and shows the objects in their new positions. At the moment DB is doing this automatically, but we need to do it manually. To tell DB that you want to sync manually, add
sync on
to your code, before the main loop.
If you have the sync on command in your game, then everytime you need to update the screen you need to use the sync command. We want to update it at the end of every loop; so add sync just before the loop command.

Now you will notice that it is much smoother.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 19th Dec 2003 11:33 Edited at: 19th Dec 2003 11:34
Now you need to do the player two controls.
Copy all the commands for player movement:

IF upkey()=1 and player1pos#<5 then player1pos#=player1pos#+0.5
IF downkey()=1 and player1pos#>-5 then player1pos#=player1pos#-0.5


and paste them below. Now change it so it is for player 2, or object 2. You need to change the variable to the one that is player 2's position. Mine is called player2pos#, but you may have something different (p2#, 2pos#, blahblahfafwa2#...)

If you run now, you will notice that both players are controlled by the arrow keys, we need to change this.

It would be good to have the W and S keys to control player 2. To do this we need to learn about scancodes. These are actually very simple. Instead of having to type of the keyname each time you need to use this command:
keystate()
Inside the brackets you need to put a number for what key you want:
This is a map of the number:

Using this we can see that, if upkey()=1 is the same as if keystate(200)=1. Now change the player two controls to what you want, using keystate.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 19th Dec 2003 11:42 Edited at: 19th Dec 2003 11:46
Now you should have this code:



You should be able to smoothly move each paddle independantly.
Before we continue, it would be good to tidy up the code a bit.

A colon (===> : ) can be used to put two pieces of code on the same line to save space.



becomes:


We know that this piece of code works, and we don't need to change it, so we can compress it up so that it doesn't confuse us.
Do this with the rest of the code and you may get something like this:-


It will still run exactly the same as the spaced out code. I have moved the sync on to just infront of the main loop. I have also added hide mouse at the beginning.

REMEMBER: You can't have two IF....THEN commands on the same line

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 19th Dec 2003 11:48 Edited at: 19th Dec 2003 11:52
Before we continue, have a go at changing bits of the code until you fully understand what it is doing.

- Try making the camera look at player 1.
Clue:



- Try making the camera be behind player 1 and move along with it.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 19th Dec 2003 12:16
OK.
Now we need to program the ball. This is a bit more complicated but I will explain it slowly. Post if you don't understand anything or if it doesn't work for you.

The ball can vary its x and z position. It also needs to move at lots of different angles. Therefore we need to have a variable that controls its angle and two variables for its x and z positions. Add this code to the main loop:



This means, position it at a point that can be moved (but is always on the floor) and yrotate it an angle that can be change.

Yrotate means rotate it around the y axis. Imagine you have stuck the y axis through it and are spinning it around (like a kebab).


Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 19th Dec 2003 15:22 Edited at: 8th Jan 2004 04:41
editied

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 19th Dec 2003 16:02
Which part?

Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 20th Dec 2003 01:38 Edited at: 26th Dec 2003 02:50
Removed

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Major Payn
20
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 20th Dec 2003 04:29
please post some more steps I was looking foward to knowing how the ball moves I am also a beginner.

Guns dont kill people, death kills people.
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 20th Dec 2003 17:38 Edited at: 20th Dec 2003 17:50
OK.
I'll post the final steps today.
I'll also put all of these together in a tutorial at the end.

In the mean time, play around with the commands you have learn.

- Try making the paddles rotate with the left and right keys


rogier
20
Years of Service
User Offline
Joined: 20th Dec 2003
Location: Netherlands
Posted: 21st Dec 2003 00:35
This tutor rox I learned alot from it Cant wait to see the ball collide and bounce back
Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 21st Dec 2003 03:36 Edited at: 12th Feb 2004 04:59
GONE!

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 11:26 Edited at: 21st Dec 2003 11:31
Try both.

Anyway, continuing with the game...
The ball needs to be moving forwards at all times. The code for this is:

ballx#=newxvalue(ballx#,balla#,0.3)
ballz#=newzvalue(ballz#,balla#,0.3)


I will explain the newxvalue command. This command will give you the new x position something is in, based on the three numbers you give it inside the bracets. The first number is its current position, the second is its angle and the third is the distance you want it to move.
Combined with the newzvalue command it will move the ball forward in the direction it is facing, at a speed of 0.3 units per sync.
Add the code to the main loop and see what it does. It should make the ball move off into the distance.

- Try changing the balla# inside the bracets for a number (90, 270, 45....)

OR

- Keep the balla# inside the bracets but add balla#=45 before the main loop.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 11:52 Edited at: 21st Dec 2003 12:29
Ok.
Make sure you have this code - balla#=90 - just before your main loop. This will mean the ball goes towards player 1 at the start.

You will notice that at the moment the ball goes straight through the paddle. . This has to change.

First of all we need an IF....THEN statement for when the ball gets to the edge of the court, for the moment we will make it turn player 1 purple, just to see if it is working.

The code is this:
if ballx#>4 then color object 1,rgb(128,0,255)

Run this, you will see that player 1 turns purple at the exact moment that the ball hits it. We know that it is doing it at the right time.

Now we need to change the color player 1 purple code for code that says bounce ball back, or turn ball around

There is no specific code for this so we need to think of how to do it. The ball is always facing balla# degrees, so when it hits the bat, this needs to change. We know that the code will be:

if ballx#>4 then balla#= .......

To work out this code we may need to draw a picture:
You will notice that the two angles add up to make a full circle. This will always be the case.
Therefore:

balla# (original) + balla# (bounced) = 360
------take balla# (bounced) from both sides------
balla# (original) = 360 - balla# (bounced)

This may seem a bit like a maths equation to you but how many maths equations have you done where you end up with a computer game?

Now add this code to the main loop and run it:



Yay!! It bounces back!!!
Now add this code for player 2:



Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 12:15
Now the ball is moving and bouncing. You may want to watch it bouncing back and forth and admire your coding.

However, it still bounces back even if the player misses it. This needs to change else no-one can win the game.

We change this by adding to the If statement and giving it some more conditions.
The code is similar to when we stopped the players being able to move of into the distance.

It should only bounce back when the difference between the player 1 z position and the ball z position is less than 1.5 (half the bat's length)

In DB language ====> if player1pos#-ballz# < 1.5
The problem with this code, however, is that if the ballz# is bigger than player1pos# then the number will be negative.
DB had a command that will help here ====> ABS()
This means take the absolute value or make it positive

So the code we need is:
if ABS(player1pos#-ballz#)<1.5

Now replace the code for turning the ball round with:

and for player 2:


Now you should be able to miss the ball.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 12:25
A small problem that you may have noticed with the code you have at the moment is that if you miss the ball but then line up with it again, the ball will still bounce back. In fact it will bounce frantically on the spot.

Wow. This is your first bug/glitch in a game!

To fix this we need to change the code a little bit. We don't want it to bounce back when ballx#>4 but when ballx#>4 and ballx#<4.5

Change the bouncing back code for player 1 to:


- Work out the code for player 2 for yourself

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 12:45 Edited at: 21st Dec 2003 12:46
Now you should have glitch-free bouncing and missing.

Now would be a good time to code the playing area a bit more.
First of all, make a floor.
Add this code before the main loop (with the other make object commands):



Now you should have a floor.

Oops! I just noticed a little problem. The paddles can move further than they need to. Change the code that won't let them move past 5 to 3.5

Now you need to make it so the ball can bounce of the top and bottom of the court. This code is similar to the other bouncing code but a bit simpler:


Notice how I used OR to combine two statements into one. This can only be done when you want the same thing to happen on both occasions.

You need to change the balla#=90 code to another angle to see this working.

This code works, but it raises a slight problem. Occasionally balla# will come out as a number smaller than 0 or bigger than 360. DB can't use angles like this in equations. Luckily, it has a command that will convert any angle into one between 0 and 360. This command is:

wrapvalue()


So this code will make sure balla# is always a number that DB can handle:


Add this after the bouncing code.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 13:03 Edited at: 21st Dec 2003 13:05
The game is almost finished now.
The next thing you need to add is the ability to change the ball's angle slightly when you hit it.
In most pong games, if you are moving when you hit the ball, it comes off at a different angle to if you are still.

We will do this by saying that for the split second when the ball is touching your paddle, but has not bounced back yet, the player can turn the ball slightly.

To achieve this we need to rearrange the code. Put all the position object and rotate object commands at the end of the main loop. Almost all DB games will have them there.

You should have code that looks like this:



This is the code for changing the ball's direction when the player is moving up (for player 1):


- Work out the code for changing the ball's angle in the other direction
- Work out the code for player 2


Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 13:11
Now you should have a playable version of 3d Pong.
The code I have is:



At the moment you can only play it for one point, but it works nonetheless.

rogier
20
Years of Service
User Offline
Joined: 20th Dec 2003
Location: Netherlands
Posted: 21st Dec 2003 13:21
Thanks for this tut!! It really helped me out again, only that sync thingy makes my screen go black and the ball and paddles moves sooo fast i had to divide every value by 10
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 13:26 Edited at: 17th Oct 2004 08:02
The final thing you need to add is scoring.

A player scores one point when the ball goes past their opponent.
So player 1 scores when ballx#>6 and player 2 scores when ballx#<-6.

Add this code to the main loop:



You may notice that you can use a colon to have more than one thing happen in an if...then command. Here I have used it to make it reset the ball and point the ball towards the player who scored.

- Add scoring for player 1 yourself

To have the score shown on the screen you need to use the print command.

To tell the computer where to 'print' the text you need to set cursor. set cursor is followed by two numbers, x and y coordinates. These are measured in pixels and start in the top left corner:


Text will be printed where ever the cursor is.

This code will show the score above the court. It needs to be in the main loop:



Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 13:28
@ rogier

Don't divide every value by ten, this may cause problems later.
Instead add sync rate 40 to the begining of your program. This will stop the computer from going too fast.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 13:38 Edited at: 21st Dec 2003 13:43
Ok. That's pretty much it. The only problem is that the ball gets replaced too quicly

This is the final code that I have. I added comments to remind you of what the code means. Copy it into DB to see it better:



Mods, quite a lot of newbies seem to have found this helpful. Any chance of it being made sticky for a bit? Feel free to delete posts and lock it if you want to.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 21st Dec 2003 17:41
remember:

There are two types of commands in DB:
action commands - these do something and are followed by one or more numbers. eg. POSITION OBJECT 1,5,3,5
return commands - these give you some information and are followed by bracets which contain numbers. eg. OBJECT POSITION X(1)

Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 22nd Dec 2003 00:46
This tut was great! What would you suggest as creating next?

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 22nd Dec 2003 00:52 Edited at: 22nd Dec 2003 00:54
Try Breakout as it shares a lot of the code (paddle, ball bouncing etc.). Stick with Arcade games for quite long time. I still make arcade games. Click my sig image to play my latest game.

Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 22nd Dec 2003 02:06
I played it, it was a lot of fun.

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 22nd Dec 2003 12:48
Thanks.

Have a go at making a breakout game. You should be able to do quite a lot of it by just changing the pong code a bit.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 22nd Dec 2003 17:38
Please can a Mod delete all the unimportant posts and sticky this for a bit? I think it would help quite a lot of people.

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 24th Dec 2003 10:35
How's breakout going?

Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 24th Dec 2003 19:33
Thanks for that.

Real helpful.

Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 25th Dec 2003 14:27
MMORGP? What in the world is that? Breakoutis coming along interestingly.

That's what I have so far, I want that block that's in the middle to be next to the other block. So that's my first bug.

John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Rennekon
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location:
Posted: 25th Dec 2003 14:32
Wow that last code was dumb. I had copied it and pasted to lower the work and forgot to fix everything. Is there someway to separate the boxes with a line or something? (in my new code)


John "Dark Lord of Angel Destroying Destiny" Pringlebee, maybe?
Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 27th Dec 2003 11:08
Just have the boxes slightly apart, or change thier colour every time. You may find this code helpful:



Chris K
20
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 5th Jan 2004 11:55 Edited at: 5th Jan 2004 11:56
It could move on the y axis, but almost every DB game will have movement mainly on the x/z axes. It's good practice.

AI should be something like:



That basically says, If the ball isn't in line with the bat, move the bat.

Change the number for different difficulties.

Login to post a reply

Server time is: 2024-04-19 22:58:13
Your offset time is: 2024-04-19 22:58:13