I've been learning darkbasic from a book that is full of mistakes. Normally this isn't a problem seeing as I can find and fix any problems. I've just finished a pong game and I've fixed most of the bugs but there's a problem that I just can't find. I think it's the collision detection but I haven't used it before so I'm not sure. I think this is where the mistake is.
FUNCTION ProcessPlayerHits()
`If the background colour behind the ball is not black then the ball has made contact with a paddle
IF POINT(BallX, BallY) > 0
`Reverse ball direction when it collides with a paddle
SpeedX = SpeedX * - 1
PLAY SOUND 1 `Play a sound when the ball collides with a paddle
ENDIF
For some reason the ball keeps rapidly changing direction between left and right while repeatedly playing the sound indicating that the ball has been hit.
In case I'm wrong, here's the whole program.
REM *************** Declare and Initialize Global Variables ***************
GLOBAL PlayerOnePoints AS INTEGER = 0 `Keep track of player 1's score
GLOBAL PlayerTwoPoints AS INTEGER = 0 `Keep track of player 2's score
GLOBAL BallX AS INTEGER = 320 `Start the game with the ball in the middle
GLOBAL BallY AS INTEGER = 240 `Start the game with the ball in the middle
GLOBAL SpeedX AS INTEGER = 3 `Ball starts moving to the right
GLOBAL SpeedY AS INTEGER = 1 `Ball starts moving down at a gentle angle
`Specify coordinates for player 1's paddle
GLOBAL PaddleX2 AS INTEGER = 5 `Set horizontal location of paddle
GLOBAL PaddleY2 AS INTEGER = 200 `Set vertical location of paddle
`Specify coordinates for player 2's paddle
GLOBAL PaddleX1 AS INTEGER = 625 `Set horizontal location of paddle
GLOBAL PaddleY1 AS INTEGER = 200 `Set vertical location of paddle
REM ******** Define Enviroment Settings and Pre-load Data Files ********
`Set the foreground colour to yellow and the background colour to black
INK RGB(255, 255, 0), 0
LOAD SOUND "type.wav", 1 `Load the specified audio file
LOAD SOUND "fire.wav", 2 `Load the specified audio file
SET SOUND VOLUME 1, 100 `Set the volume to be used when playing sound file
SET SOUND VOLUME 2, 85 `Set the volume to be used when playing sound file
REM ********************** Main Processing Section **********************
`Call function responsible for displaying the game's welcome screen
DisplaySplashScreen()
SYNC ON `Disable automatic screen refresh
SYNC RATE 60 `Set the game's synchronisation rate
DO `Loop forever
CLS `Clear the display area
`Call on the game's functions
ProcessPlayerMoves()
UpdateBallPosition()
ManageVerticalCollisions()
ProcessPlayerMisses()
DrawBallAndPaddles()
ProcessPlayerHits()
UpdateScore()
`Game play ends when one of the players scores 10 points
IF (PlayerOnePoints = 10) OR (PlayerTwoPoints = 10) THEN EndOfGame()
SYNC `Perform a screen refresh
LOOP
REM ************************* Procedure Section *************************
`This function is responsible for controlling the movement of game paddles
FUNCTION ProcessPlayerMoves()
`Player 1 uses the Shift key to move the paddle upward
IF SHIFTKEY()
DEC PaddleY2, 3 `Decrement vertical coordinate by 3
`Do not allow the paddle to go past the top of the screen
IF PaddleY2 < 0 THEN PaddleY2 = 0
ENDIF
`Player 1 uses the Control key to move the paddle downward
IF CONTROLKEY()
INC PaddleY2, 3 `Increment vertical coordinate by 3
`Do not allow the paddle to go past the bottom of the screen
IF PaddleY2 > 405 THEN PaddleY2 = 405
ENDIF
`Player 2 uses th Up Arrow key to move the paddle upward
If UPKEY()
DEC PaddleY1, 3 `Decrement vertical coordinate by 3
`Do not allow the paddle to go past the top of the screen
IF PaddleY1 < 0 THEN PaddleY1 = 0
ENDIF
`Player 2 uses the Down Arrow key to move the paddle upward
If DOWNKEY()
INC PaddleY1, 3 `Decrement vertical coordinate by 3
`Do not allow the paddle to go past the bottom of the screen
IF PaddleY1 > 405 THEN PaddleY1 = 405
ENDIF
ENDFUNCTION
`This function is responsible for updating the location of the ball as it
`bounces around the screen
FUNCTION UpdateBallPosition()
INC BallX, SpeedX `Move the ball further along its current direction on its horizontal axis
INC BallY, SpeedY `Move the ball further along its current direction on its vertical axis
ENDFUNCTION
`This function is responsible for reversing the vertical direction of the
`ball when the ball touches the top or bottom edge of the screen
FUNCTION ManageVerticalCollisions()
IF BallY < 5 OR BallY > 475
`If the ball hits the top or bottom reverse the direction of the ball
`moves on the y coordinate
SpeedY = SpeedY * - 1 `Reverse the vertical direction of the ball
PLAY SOUND 1 `Play a sound when the ball collides with the top or
`bottom of the screen
ENDIF
ENDFUNCTION
`This function is responsible for handling player misses
FUNCTION ProcessPlayerMisses()
`If the ball's horizontal location is less than 4 then player 1 has missed the ball
IF BallX < 9
PLAY SOUND 2 `Play a sound that indicates a miss
INC PlayerTwoPoints, 1 `Incrementplayer's score
ResetBallAndPaddles() `Call function responsible for resetting the ball and paddles
`Player 1 missed the ball. Set things up so that the ball moves right
SpeedX = 3 `Reverse the horizontal direction of the ball
SLEEP 2000 `Pause the game execution for 2 seconds
ENDIF
`If the ball's horizontal location is greater than 631 then player 2 has missed
IF BallX > 631
PLAY SOUND 2 `Play a sound that indicates a miss
INC PlayerOnePoints, 1 `Increment player 1's score
ResetBallAndPaddles() `Call function responsible for setting ball and paddles
`Player 2 missed the ball. Set things up so that the ball moves left
SpeedX = -3 `Reverse the horizontal direction of the ball
SLEEP 2000 `Pause game execution for 2 seconds
ENDIF
ENDFUNCTION
`This function displays the game's welcome screen
FUNCTION DisplaySplashScreen()
SET TEXT SIZE 48
CENTER TEXT 320, 170, "Welcome to DB PONG!"
SET TEXT SIZE 16
CENTER TEXT 320, 250, "Press any key to continue"
WAIT KEY `Wait until the player presses a keyboard key
CLS `Clear the display area
ENDFUNCTION
`This function is responsible for drawing the game's ball and paddles
FUNCTION DrawBallAndPaddles()
`Set horizontal and vertical location of the ball and it's radius (size)
CIRCLE BallX, BallY, 8
`Set the horizontal and vertical location of game paddles
BOX PaddleX1, PaddleY1, PaddleX1 + 10, PaddleY1 + 75
BOX PaddleX2, PaddleY2, PaddleX2 + 10, PaddleY2 + 75
ENDFUNCTION
`This function is responsible for reversing the direction that the ball is
`moving after it collides with a paddle
FUNCTION ProcessPlayerHits()
`If the background colour behind the ball is not black then the ball has made contact with a paddle
IF POINT(BallX, BallY) > 0
`Reverse ball direction when it collides with a paddle
SpeedX = SpeedX * - 1
PLAY SOUND 1 `Play a sound when the ball collides with a paddle
ENDIF
ENDFUNCTION
`This function resets the ball to the middle of the screen and repositions
`player paddles to get the game ready to play a new point
FUNCTION ResetBallAndPaddles()
`Re-center the ball to the middle of the screen
BallX = 320
BallY = 240
`Re-center player 2's paddle
PaddleX1 = 625
PaddleY1 = 200
`Re-center player 1's paddle
PaddleX2 = 5
PaddleY2 = 200
ENDFUNCTION
`This function updates the display of player scores
FUNCTION UpdateScore()
`Display player 1 and player 2's current scores
TEXT 50, 10, "Player 1: " + STR$(PlayerOnePoints) + " Points"
TEXT 440, 10, "Player 2: " + STR$(PlayerTwoPoints) + " Points"
ENDFUNCTION
`This function ends the game when one of the players scores 10 points and
`resets player scores to zero to ready the game to be played again
FUNCTION EndOfGame()
SYNC OFF `Disable automatic refresh
CLS `Clear the display area
SET TEXT SIZE 48
CENTER TEXT 320, 120, "Game Over!"
SET TEXT SIZE 24
CENTER TEXT 320, 210, "Player 1: " + STR$(PlayerOnePoints) + " Points"
CENTER TEXT 320, 240, "Player 2: " + STR$(PlayerTwoPoints) + " Points"
SET TEXT SIZE 16
CENTER TEXT 320, 300, "Press any key to start a new game"
WAIT KEY
PlayerOnePoints = 0
PlayerTwoPoints = 0
CLS
SYNC ON `Enable automatic screen refresh
ENDFUNCTION