I'm writing a 2D game to learn the DBP language. I originally wrote the code in 1 big loop. I then wanted to separate the code into functions so I could keep the logic clear. However, as soon as I do that, the collision detection stops working. I went from this
`Basic 2D Game Template
backdrop off
hide mouse
sync on
sync rate 0
set display mode 800, 600, 32
set image colorkey 255, 0, 255
`*** Set the screen size/depth
`*** Initializtion code (define globals, dim arrays, load media, etc)
SetupSpriteDisplay()
moveX# = 1.0
moveY# = 1.0
curBallX# = 100.0
curBallY# = 300.0
level = 1
lives = 3
score = 0
`-------------------------------------------------------------------------------------------
`Load all graphics
`-------------------------------------------------------------------------------------------
load image "graphics\paddle_large.png", 1, 1
sprite 1, 100, 500, 1
paddleWidth = image width(1)
paddleHeight = image height(1)
load image "graphics\ball_large.png", 2, 1
sprite 2, 100, 300, 2
ballWidth = image width(2)
ballHeight = image height(2)
load image "graphics\brick1.png", 3, 1
load image "graphics\brick2.png", 4, 1
load image "graphics\brick3.png", 5, 1
load image "graphics\brick4.png", 6, 1
load image "graphics\brick5.png", 7, 1
load image "graphics\brick6.png", 8, 1
load image "graphics\brick7.png", 9, 1
brickWidth = image width(3)
brickHeight = image height(3)
`-------------------------------------------------------------------------------------------
` Load bricks.
`-------------------------------------------------------------------------------------------
brick = 0
for x = 1 to 800 step 42
for y = 1 to 5
sprite (10 + brick), x, (y*18)+1, 3 + y
brick = brick + 1
next y
next x
totalBricks = brick
bricksLeft = totalBricks
`-------------------------------------------------------------------------------------------
`- -
`- M_A_I_N L_O_O_P S T A R T -
`- -
`-------------------------------------------------------------------------------------------
movetimerset# = 1000.0/600.0
movetimer# = timer()
waitingforkeystroke = 1
do
cls
if timer() - movetimer# > movetimerset#
`*** Call input handler
if escapekey() = 1
`*** Cleanup any resources in use
endif
`if the user clicks the mouse, clear the flag that moves the ball.
if mouseclick() > 0
waitingforkeystroke = 0
endif
`CheckBallBrickCollision()
origXdir# = moveX#
origYdir# = moveY#
for brick = 0 to (totalBricks - 1)
if sprite exist(brick + 10)
if sprite collision(2, brick + 10)
BrickDiffX# = int(curBallX#) - (Sprite x(brick + 10))
BrickDiffY# = int(curBallY#) - (Sprite y(brick + 10))
delete sprite brick + 10
score = score + 100
bricksLeft = bricksLeft - 1
sync
if bricksLeft = 0
level = level + 1
lives = lives + 1
wait 3000
`curBallX# = 100
curBallY# = 300
else
if BrickDiffX# = -ballWidth or BrickDiffX# = brickWidth
moveX# = -origXdir#
endif
if BrickDiffY# = -ballHeight or BrickDiffY# = brickHeight
moveY# = -origYdir#
endif
`waitingforkeystroke = 1
`goto endbrickcheck
endif
endif
endif
next brick
endbrickcheck:
`*** Game logic (AI, Collision, Calculations, physics)
`CheckBallPaddleCollision()
if sprite collision(1, 2) `collision between paddle and ball
`logic for how the ball bounces off the paddle.
if curBallY# = (500-ballHeight)
moveY# = -moveY#
endif
PaddleXDiff# = curBallX# - (Sprite x(1))
if PaddleXDiff# >= -10 and PaddleXDiff# < 0
if moveX# - 2 = 0 then moveX# = -.5 else moveX# = moveX# - 2
endif
if PaddleXDiff# >= 0 and PaddleXDiff# < 10
if moveX# - 1.5 = 0 then moveX# = -.5 else moveX# = moveX# - 1.5
endif
if PaddleXDiff# >= 10 and PaddleXDiff# < 20
if moveX# - 1 = 0 then moveX# = -.5 else moveX# = moveX# - 1
endif
if PaddleXDiff# >= 20 and PaddleXDiff# < 30
if moveX# - .5 = 0 then moveX# = -.5 else moveX# = moveX# - .5
endif
if PaddleXDiff# >= 30 and PaddleXDiff# < 40
if moveX# + .5 = 0 then moveX# = .5 else moveX# = moveX# + .5
endif
if PaddleXDiff# >= 40 and PaddleXDiff# < 50
if moveX# + 1 = 0 then moveX# = .5 else moveX# = moveX# + 1
endif
if PaddleXDiff# >= 50 and PaddleXDiff# < 60
if moveX# + 1.5 = 0 then moveX# = .5 else moveX# = moveX# + 1.5
endif
if PaddleXDiff# >= 60 and PaddleXDiff# < 70
if moveX# + 2 = 0 then moveX# = .5 else moveX# = moveX# + 2
endif
if moveX# > 2.0 then moveX# = 2.0
if moveX# < -2.0 then moveX# = -2.0
endif
`*** Prepare for the next animation frame
if not waitingforkeystroke
curBallX# = curBallX# + moveX#
curBallY# = curBallY# + moveY#
endif
if curBallX# >= (800 - 20) or curBallX# <= 0
moveX# = -moveX#
endif
if curBallY# <= 0
moveY# = -moveY#
endif
if curBallY# >= (600 - 20)
lives = lives - 1
waitingforkeystroke = 1
`curBallX = 10
curBallY# = 300
endif
`better logic for paddle. Make sure it stays on the screen.
curPaddleX = mousex()
if curPaddleX > (800 - 80)
curPaddleX = (800 - 80)
endif
`*** Draw Images/sprites to screen
sprite 1, curPaddleX, 500, 1
sprite 2, curBallX#, curBallY#, 2
movetimer# = timer()
endif
`*** Print stats to screen
text 100, 550, "Level: " + str$(level)
text 200, 550, "Lives: " + str$(lives)
text 300, 550, "Score: " + str$(score)
`text 400, 550, "BrickDiffY#: " + str$(BrickDiffY#)
`text 550, 550, "BrickDiffX#: " + str$(BrickDiffX#)
`*** Make sync call
sync
loop
`-------------------------------------------------------------------------------------------
`- -
`- E N D O F M_A_I_N G_A_M_E L_O_O_P -
`- -
`--------------------------------------------------------------------------------------------
`*** Declare & define Functions
function SetupSpriteDisplay()
hide mouse
` Must take control of the sync, otherwise the screen will go blue when the sprite is created
sync on
sync rate 0
` Set up an image and a sprite
get image 1, 0, 0, 1, 1
sprite 1, -1, -1, 1
` Now disable backsave
set sprite 1, 0, 1
` Remove the sprite and image
delete sprite 1
delete image 1
endfunction
to this
`Basic 2D Game Template
backdrop off
hide mouse
sync on
sync rate 0
set display mode 800, 600, 32
set image colorkey 255, 0, 255
`*** Set the screen size/depth
`*** Initializtion code (define globals, dim arrays, load media, etc)
SetupSpriteDisplay()
moveX# = 1.0
moveY# = 1.0
curBallX# = 100.0
curBallY# = 300.0
level = 1
lives = 3
score = 0
`-------------------------------------------------------------------------------------------
`Load all graphics
`-------------------------------------------------------------------------------------------
load image "graphics\paddle_large.png", 1, 1
sprite 1, 100, 500, 1
paddleWidth = image width(1)
paddleHeight = image height(1)
load image "graphics\ball_large.png", 2, 1
sprite 2, 100, 300, 2
ballWidth = image width(2)
ballHeight = image height(2)
load image "graphics\brick1.png", 3, 1
load image "graphics\brick2.png", 4, 1
load image "graphics\brick3.png", 5, 1
load image "graphics\brick4.png", 6, 1
load image "graphics\brick5.png", 7, 1
load image "graphics\brick6.png", 8, 1
load image "graphics\brick7.png", 9, 1
brickWidth = image width(3)
brickHeight = image height(3)
`-------------------------------------------------------------------------------------------
` Load bricks.
`-------------------------------------------------------------------------------------------
brick = 0
for x = 1 to 800 step 42
for y = 1 to 5
sprite (10 + brick), x, (y*18)+1, 3 + y
brick = brick + 1
next y
next x
totalBricks = brick
bricksLeft = totalBricks
`-------------------------------------------------------------------------------------------
`- -
`- M_A_I_N L_O_O_P S T A R T -
`- -
`-------------------------------------------------------------------------------------------
movetimerset# = 1000.0/600.0
movetimer# = timer()
waitingforkeystroke = 1
do
cls
`if timer() - movetimer# > movetimerset#
`*** Call input handler
if escapekey() = 1
`*** Cleanup any resources in use
endif
`if the user clicks the mouse, clear the flag that moves the ball.
if mouseclick() > 0
waitingforkeystroke = 0
endif
CheckBallBrickCollision()
`*** Game logic (AI, Collision, Calculations, physics)
`CheckBallPaddleCollision()
if sprite collision(1, 2) `collision between paddle and ball
`logic for how the ball bounces off the paddle.
if curBallY# = (500-ballHeight)
moveY# = -moveY#
endif
PaddleXDiff# = curBallX# - (Sprite x(1))
if PaddleXDiff# >= -10 and PaddleXDiff# < 0
if moveX# - 2 = 0 then moveX# = -.5 else moveX# = moveX# - 2
endif
if PaddleXDiff# >= 0 and PaddleXDiff# < 10
if moveX# - 1.5 = 0 then moveX# = -.5 else moveX# = moveX# - 1.5
endif
if PaddleXDiff# >= 10 and PaddleXDiff# < 20
if moveX# - 1 = 0 then moveX# = -.5 else moveX# = moveX# - 1
endif
if PaddleXDiff# >= 20 and PaddleXDiff# < 30
if moveX# - .5 = 0 then moveX# = -.5 else moveX# = moveX# - .5
endif
if PaddleXDiff# >= 30 and PaddleXDiff# < 40
if moveX# + .5 = 0 then moveX# = .5 else moveX# = moveX# + .5
endif
if PaddleXDiff# >= 40 and PaddleXDiff# < 50
if moveX# + 1 = 0 then moveX# = .5 else moveX# = moveX# + 1
endif
if PaddleXDiff# >= 50 and PaddleXDiff# < 60
if moveX# + 1.5 = 0 then moveX# = .5 else moveX# = moveX# + 1.5
endif
if PaddleXDiff# >= 60 and PaddleXDiff# < 70
if moveX# + 2 = 0 then moveX# = .5 else moveX# = moveX# + 2
endif
if moveX# > 2.0 then moveX# = 2.0
if moveX# < -2.0 then moveX# = -2.0
endif
`*** Prepare for the next animation frame
if not waitingforkeystroke
curBallX# = curBallX# + moveX#
curBallY# = curBallY# + moveY#
endif
if curBallX# >= (800 - 20) or curBallX# <= 0
moveX# = -moveX#
endif
if curBallY# <= 0
moveY# = -moveY#
endif
if curBallY# >= (600 - 20)
lives = lives - 1
waitingforkeystroke = 1
`curBallX = 10
curBallY# = 300
endif
`better logic for paddle. Make sure it stays on the screen.
curPaddleX = mousex()
if curPaddleX > (800 - 80)
curPaddleX = (800 - 80)
endif
`*** Draw Images/sprites to screen
sprite 1, curPaddleX, 500, 1
sprite 2, curBallX#, curBallY#, 2
movetimer# = timer()
`endif
`*** Print stats to screen
text 100, 550, "Level: " + str$(level)
text 200, 550, "Lives: " + str$(lives)
text 300, 550, "Score: " + str$(score)
`text 400, 550, "BrickDiffY#: " + str$(BrickDiffY#)
`text 550, 550, "BrickDiffX#: " + str$(BrickDiffX#)
`*** Make sync call
sync
loop
`-------------------------------------------------------------------------------------------
`- -
`- E N D O F M_A_I_N G_A_M_E L_O_O_P -
`- -
`--------------------------------------------------------------------------------------------
`*** Declare & define Functions
function SetupSpriteDisplay()
hide mouse
` Must take control of the sync, otherwise the screen will go blue when the sprite is created
sync on
sync rate 0
` Set up an image and a sprite
get image 1, 0, 0, 1, 1
sprite 1, -1, -1, 1
` Now disable backsave
set sprite 1, 0, 1
` Remove the sprite and image
delete sprite 1
delete image 1
endfunction
function CheckBallBrickCollision()
origXdir# = moveX#
origYdir# = moveY#
for brick = 0 to (totalBricks - 1)
if sprite exist(brick + 10)
if sprite collision(2, brick + 10)
BrickDiffX# = int(curBallX#) - (Sprite x(brick + 10))
BrickDiffY# = int(curBallY#) - (Sprite y(brick + 10))
delete sprite brick + 10
score = score + 100
bricksLeft = bricksLeft - 1
sync
if bricksLeft = 0
level = level + 1
lives = lives + 1
wait 3000
`curBallX# = 100
curBallY# = 300
else
if BrickDiffX# = -ballWidth or BrickDiffX# = brickWidth
moveX# = -origXdir#
endif
if BrickDiffY# = -ballHeight or BrickDiffY# = brickHeight
moveY# = -origYdir#
endif
`waitingforkeystroke = 1
`goto endbrickcheck
endif
endif
endif
next brick
endbrickcheck:
endfunction