I just started working with DBPro and I thought I would start with 2D. I decided to write a "breakout" game. Simple graphics, movement, etc. I found an old post that had a simple "framework" that developers could use when starting something new. What I liked about it was that it used a "movetimer". However, if I set the logic so that it is anything above 0, it runs really slow. If I comment out the "if then" statement that checks this value, it runs much faster. I would really like to be able to use this logic so I can increase the speed after each level.
Does anyone see the logic error?
`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
moveY = 1
curBallX = 100
curBallY = 300
level = 1
lives = 3
score = 0
`-------------------------------------------------------------------------------------------
`Load all graphics
`-------------------------------------------------------------------------------------------
load image "graphics\paddle.bmp", 1, 1
sprite 1, 100, 500, 1
load image "graphics\ball.bmp", 2, 1
sprite 2, 100, 300, 2
load image "graphics\brick.bmp", 3, 1
`-------------------------------------------------------------------------------------------
` Load bricks.
`-------------------------------------------------------------------------------------------
brick = 0
for x = 1 to 800 step 40
for y = 1 to 100 step 20
sprite (10 + brick), x, y, 3
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/1000
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
for brick = 0 to (totalBricks - 1)
if sprite exist(brick + 10)
if sprite collision(2, brick + 10)
delete sprite brick + 10
score = score + 100
bricksLeft = bricksLeft - 1
If bricksLeft = 0
level = level + 1
lives = lives + 1
wait 3000
curBallX = 100
curBallY = 300
else
moveY = abs(moveY)
goto endbrickcheck
endif
endif
endif
next brick
endbrickcheck:
`*** Game logic (AI, Collision, Calculations, physics)
if sprite collision(1, 2) `collision between paddle and ball
if curBallY = (500-20)
moveY = -moveY
endif
PaddleXDiff = curBallX - (Sprite x(1))
if PaddleXDiff >= -10 and PaddleXDiff < 0
moveX = -2
endif
if PaddleXDiff >= 20 and PaddleXDiff < 30
moveX = 2
endif
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 = 100
endif
`*** Draw Images/sprites to screen
sprite 1, mousex()-20, 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)
`*** 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