Hey Guys,
I've only literally just started with DBPro, I'm having to learn it for a group project at uni, so I thought I'd try a nice basic 3D Pong game, but for some reason I can't get the thing to work properly and it is really starting to give me a headache, can someone look over my code and give me some advice? It would be much appreciated.
My problems with this are:
1) I am trying to randomise the angle the ball moves along once it resets to the middle of the screen, but RND() doesnt seem to send it along more than 2 directions. Also, the speed seems to vary, something I cannot get my head around.
2) When moving the paddles, the ball angle is supposed to change if it contacts with the ball, I can't test this too well because the angles the ball moves along are right at the boundary of the paddle movement zone.
3) Again, an issue with RND(), but I am trying to point the ball at a random point on the screen during initialisation but this still goes to the same point everytime.
Once I ran into problems making it myself I did try to use the code from one of the popular 3D pong tutorials on the web (cannot remember who atm) but I still can't get it to work, it most likely is something really simple but once things go belly up I run out of ideas very fast, since I am still learning programming I haven't managed to work out how to best fix things like this once I hit a wall, I just get very agitated
.
REM Setup display settings
REM Checks for supported display modes and picks highest one, locks frame rate to 60fps max
REM This code was downloaded from The Game Creators, created by sevarian
REM It is reproduced here with full permission
REM http://www.thegamecreators.com
sw = 0
sh = 0
sd = 0
Perform Checklist For Display Modes
i = Checklist Quantity()
For a = 1 to i
tw = Checklist Value A(a)
th = Checklist Value B(a)
td = Checklist Value C(a)
If sw <= tw And sh <= th And sd < td Then sw = tw : sh = th : sd = td
If sw < tw And sh <= th And sd <= td Then sw = tw : sh = th : sd = td
If sw <= tw And sh < th And sd <= td Then sw = tw : sh = th : sd = td
Next a
Set Display Mode sw, sh, sd
Sync on : Sync Rate 60 : Hide Mouse : Autocam Off
REM Loading screen
Sync : Center text (sw / 2), (sh / 2), "Loading..." : Sync
REM Create game objects
Set Global Collision On
REM Player 1 paddle
Make Object Box 1, (sw / 30), (sh / 5), 25
Position Object 1, (sw / 4), (sh / 2), 0
REM Player 2 paddle
Make Object Box 2, (sw / 30), (sh / 5), 25
Position Object 2, (3 * (sw / 4)), (sh / 2), 0
REM Game ball
Point_X = RND(sw)
Point_Y = RND(sh)
Make Object Sphere 3, 25
Position Object 3, (sw / 2), (sh / 2), 0
Point Object 3, Point_X, Point_Y, 0
Set Object Smoothing 3, 100
REM Setup play area
Make Object Box 4, (sw / 2), sh, 25
Position Object 4, (sw / 2), (sh / 2), 0
Hide Object 4
REM Game variables
Randomize (TIMER())
Ball_Angle# = RND(360)
Ball_X# = (sw / 2)
Ball_Y# = (sh / 2)
Player1Pos# = (sh / 2)
Player2Pos# = (sh / 2)
REM Camera
Make Camera 1
Position Camera 1, (sw / 2), (sh / 2), -(sw / 2)
Point Camera 1, (sw / 2), (sh / 2), 0
Color Backdrop 1, RGB(0, 0, 0)
REM Main loop
Do
REM Player paddle movement
If UPKEY() And (Object Position Y (1) < (sh - ((sh / 5) + 20)))
Player1Pos# = Player1Pos# + 5
Endif
If DOWNKEY() And (Object Position Y (1) > ((sh / 5) - 20))
Player1Pos# = Player1Pos# - 5
Endif
If UPKEY() And Object Collision (1, 3)
Ball_Angle# = (360 - (Ball_Angle# + 10))
Endif
If DOWNKEY() And Object Collision (1, 3)
Ball_Angle# = (360 - (Ball_Angle# - 10))
Endif
If Object Collision (1, 3)
Ball_Angle# = (360 - Ball_Angle#)
Endif
REM Computer paddle movement
If KeyState(17) And (Object Position Y (2) < (sh - ((sh / 5) + 20)))
Player2Pos# = Player2Pos# + 5
Endif
If KeyState(31) And (Object Position Y (2) > ((sh / 5) - 20))
Player2Pos# = Player2Pos# - 5
Endif
If KeyState(17) And Object Collision (2, 3)
Ball_Angle# = (360 - (Ball_Angle# + 10))
Endif
If KeyState(31) And Object Collision (2, 3)
Ball_Angle# = (360 - (Ball_Angle# - 10))
Endif
If Object Collision (2, 3)
Ball_Angle# = (360 - Ball_Angle#)
Endif
REM Game ball movement
If (Object Collision (3, 4) = 0) And (((sw / 4) > Ball_X#) Or (Ball_X# > (3 * sw / 4)))
Ball_X# = (sw / 2)
Ball_Y# = (sh / 2)
Randomize (TIMER())
Ball_Angle# = RND(360)
Else
If (Object Collision (3, 4) = 0)
Ball_Angle# = (180 - Ball_Angle#)
Endif
Endif
Ball_Angle# = Wrapvalue (Ball_Angle#)
Ball_X# = NewXValue (Ball_X#, Ball_Angle#, 15)
Ball_Y# = NewYValue (Ball_Y#, Ball_Angle#, 15)
Position Object 1, (sw / 4), Player1Pos#, 0
Position Object 2, (3 * sw / 4), Player2Pos#, 0
Position Object 3, Ball_X#, Ball_Y#, 0
REM Refresh screen
Sync
REM End loop
Loop