This was my first complete program written in DBPro. Its mechanics aren't entirely sound, but I thought other newbs like myself could benefit from some of the things I programmed into it, namely game state, scoring, leveling, etc. It's no Michelangelo's David, but it's all mine!
` Display constants
#CONSTANT SCREEN_WIDTH 800
#CONSTANT SCREEN_HEIGHT 600
` Color constants
#CONSTANT COLOR_WHITE RGB(255, 255, 255)
#CONSTANT COLOR_BLACK RGB(0, 0, 0)
` Mode constants to determine what to display during main game loop
#CONSTANT MODE_TITLE = 0 ` Display opening credits
#CONSTANT MODE_PLAY = 1 ` Handle movement/collision/scoring
#CONSTANT MODE_ENDGAME = 2 ` Display end game message
` Set up variables
Mode = MODE_TITLE ` Start game with opening credits
FieldBorder = 10 ` This is the size of the border - changes during play
Level = 1 ` Level - affects ball speed, paddle location, border
Score = 0 ` Player score
Lives = 3 ` Number of lives left
HasBall = 1 ` Is the player holding the ball?
BallSpeed = 5 ` Ball speed - changes during play
BallOffsetX# = 1 ` Controls the movement of the ball - positive or negative
BallOffsetY# = 1 ` Controls the movement of the ball - positive or negative
BallSize = 20 ` Size of the ball - changes during play
PaddleSize = 80 ` Size of paddle (height)
PaddleSpeed = 20 ` Speed of paddle, when moved
PaddleX = 50 ` X location of paddle - changes during play
PaddleY = (SCREEN_HEIGHT / 2) - (PaddleSize / 2) ` Start paddle at screen center
` Set up screen
SYNC ON
SYNC RATE 40
SET DISPLAY MODE SCREEN_WIDTH, SCREEN_HEIGHT, 32
HIDE MOUSE
` Main game loop
DO
SELECT Mode
CASE MODE_TITLE: ` Draw title screen
GOSUB _DrawTitle
if RETURNKEY() = 1 then Mode = MODE_PLAY ` User pressed space - begin play
ENDCASE
CASE MODE_ENDGAME: ` Draw end of game scren
GOSUB _DrawEndGame
SYNC
WAIT KEY
END
ENDCASE
CASE MODE_PLAY: ` Game play
` Poll keys to determine movement
if UPKEY() = 1 then PaddleY = PaddleY - PaddleSpeed
if DOWNKEY() = 1 then PaddleY = PaddleY + PaddleSpeed
if SPACEKEY() = 1 and HasBall = 1 then HasBall = 0
` Determine if paddle is in-bounds, if not, adjust accordingly
if PaddleY < (1 + FieldBorder) then PaddleY = (1 + FieldBorder)
if PaddleY > (SCREEN_HEIGHT - FieldBorder - PaddleSize - 1) then PaddleY = (SCREEN_HEIGHT - FieldBorder - PaddleSize - 1)
GOSUB _DrawField
GOSUB _DrawPaddle
` Handle ball control
if HasBall = 1 ` Player is holding onto the ball - position accordingly
BallX = PaddleX + 11
BallY = PaddleY
BallOffsetX# = 1
BallOffsetY# = 1
INK COLOR_WHITE, COLOR_BLACK
CENTER TEXT SCREEN_WIDTH / 2, (SCREEN_HEIGHT / 2) - 8, "Lives Remaining: " + STR$(Lives)
SET TEXT TO ITALIC
CENTER TEXT SCREEN_WIDTH / 2, (SCREEN_HEIGHT / 2) + 8, "Ready to serve!"
SET TEXT TO NORMAL
else
BallX = BallX + (BallOffsetX# * BallSpeed)
BallY = BallY + (BallOffsetY# * BallSpeed)
endif
` Handle collision detection
if (BallX + BallSize) > (SCREEN_WIDTH - FieldBorder) then BallOffsetX# = BallOffsetX# - 1
if (BallY + BallSize) > (SCREEN_HEIGHT - FieldBorder) then BallOffsetY# = BallOffsetY# - 1
` Ball hit back wall - lose a life
if BallX < FieldBorder
Lives = Lives - 1
HasBall = 1
if Lives < 1 then Mode = MODE_ENDGAME
endif
` Ball hit paddle - adjust score/settings
if BallY < FieldBorder then BallOffsetY# = BallOffsetY# + 1
if (BallX < PaddleX + 10) and (BallX > PaddleX) and (BallY > PaddleY) and (BallY < (PaddleY + PaddleSize))
BallOffsetX# = BallOffsetX# + 1
BallOffsetY# = BallOffsetY# + 1
Score = Score + 1
if Score = 5 then BallSpeed = BallSpeed + 1: PaddleSize = PaddleSize - 1: FieldBorder = FieldBorder + 1: BallSize = BallSize - 1: Level = Level + 1
if Score = 10 then BallSpeed = BallSpeed + 1: PaddleSize = PaddleSize - 1: FieldBorder = FieldBorder + 1: BallSize = BallSize - 1: Level = Level + 1
if Score = 15 then BallSpeed = BallSpeed + 1: PaddleSize = PaddleSize - 1: FieldBorder = FieldBorder + 1: BallSize = BallSize - 1: Level = Level + 1
endif
GOSUB _DrawBall
GOSUB _DrawScore
ENDCASE
ENDSELECT
SYNC
LOOP
_DrawTitle:
CLS COLOR_WHITE
INK COLOR_BLACK, COLOR_WHITE
` Draw each line in the center of the screen
S$ = "SUPER SQUASH"
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 100, S$
S$ = "The first DBPro game written by EP Spader"
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 120, S$
S$ = "To move your paddle, use the up/down keys"
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 150, S$
S$ = "To serve when holding the ball, press the space bar"
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 170, S$
S$ = "But watch out! The game gets fast, and your paddle smaller!"
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 190, S$
S$ = "Press <ENTER> to begin!"
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 230, S$
RETURN
_DrawEndGame:
CLS COLOR_WHITE
INK COLOR_BLACK, COLOR_WHITE
` Draw each line in the center of the screen
S$ = "YOU HAVE LOST!"
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 200, S$
S$ = "Your final score was: " + STR$(Score)
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 220, S$
S$ = "Press any key to quit."
TEXT (SCREEN_WIDTH / 2) - (TEXT WIDTH(S$) / 2), 250, S$
RETURN
_DrawScore:
` Center the level and score just under the top border
INK COLOR_WHITE, COLOR_BLACK
S$ = "LIVES: " + STR$(Lives) + " LEVEL: " + STR$(Level) + " SCORE: " + STR$(Score)
CENTER TEXT (SCREEN_WIDTH / 2), FieldBorder + 4, S$
RETURN
_DrawBall:
` Draw the ball at its location
INK COLOR_WHITE, COLOR_WHITE
BOX BallX, BallY, BallX + BallSize, BallY + BallSize
RETURN
_DrawPaddle:
` Draw the paddle using variables affecting size and location
INK COLOR_WHITE, COLOR_WHITE
BOX PaddleX, PaddleY, PaddleX + 10, PaddleY + PaddleSize
RETURN
_DrawField:
` Clear the screen with white, then build a box in black to give a border effect
CLS COLOR_WHITE
INK COLOR_BLACK, COLOR_BLACK
BOX FieldBorder, FieldBorder, SCREEN_WIDTH - FieldBorder, SCREEN_HEIGHT - FieldBorder
RETURN
I reject your reality and substitute my own.