For anyone wondering i am reading the dark basic pro 2nd edition book. I've gone through chap 10 through 14. I've tested the game through out my creation with success. But when i put the final touches on it in chap 14 it now wont load correctly. There was alot of changes done but ive read my code over and the authors code over and cant find an error. Does CAPS matter, correct spacing??? I just cant find it. Both codes are below PLEASE HELP. The result i get is the score just starts going up, the level # goes round and round, the ball wont come off the paddle and all of the blocks wont show up. It almost seems like its resetting.
Mine
REM Project: darkenoid
REM Created: 8/8/2009 9:26:48 AM
REM
REM ***** Main Source File *****
REM
type SPRITET
X as integer
Y as integer
SPEEDX as integer
SPEEDY as integer
Endtype
`BACKGROUND IMAGE
global IMG_BACKGROUND as word = 1
`ball VARIABLES
global IMG_ball as word = 3
global SPR_ball as word = 3
global ball AS SPRITET
`BLOCK VARIABLES
global IMG_BLOCKS as word = 100
global SPR_BLOCKS as word = 100
global SCRATCH_BLOCK as word = 101
dim blockData(20,20) as byte
`paddle VARIABLES
global SPR_PADDLE as word = 5
global IMG_paddle1 as word = 20
global IMG_paddle2 as word = 21
global IMG_paddle3 as word = 22
global paddle AS SPRITET
global paddleImage as word = IMG_paddle1
`GAME STATE VAARIABLES
global gameState as byte = 2
global autoPlay as byte = 0
global score AS dword = 0
global level as word = 0
global maxlevels as word
global lives as byte = 3
`POWERUP STATES
global SPR_POWERUP as word = 10
global SCRATCH_POWERUP as word = 14
dim powerups(4) as byte
dim powerupImage(4) as byte
`FOR BLOWING STUFF UP
global SPR_EXPLOSION as word = 15
dim explosionTimer(4) as byte
dim explosions(4) as byte
`SOUND EFFECTS
global SND_paddle as byte = 1
global SND_BLOCK as byte = 2
global SND_WIN as byte = 3
global SND_GONG as byte = 4
global SND_POWERUP2 as byte = 5
global SND_POWERUP1 as byte = 6
global SND_POWERUP3 as byte = 7
global SND_POWERUP4 as byte = 8
`start game running
Init_Game()
Run_Game()
End
function Init_Game()
SYNC ON
SYNC RATE 40
HIDE MOUSE
RANDOMIZE TIMER()
`LOAD BACKGROUND
load image "BACKGROUND.BMP", IMG_BACKGROUND
`LOAD paddle
load image "paddle1.BMP", IMG_paddle1
SPRITE SPR_paddle, 999,999, IMG_paddle1
paddle.X = SCREEN WIDTH() / 2 - SPRITE WIDTH(SPR_paddle) / 2
paddle.Y = SCREEN HEIGHT() - 50
load image "paddle2.BMP", IMG_paddle2
load image "paddle3.BMP", IMG_paddle3
`LOAD ball
load image "ball.BMP", IMG_ball
SPRITE SPR_ball, 999, 999, IMG_ball
ball.SPEEDX = 1
ball.SPEEDY = -1
`LOAD BLOCKS
CREATE ANIMATED SPRITE SPR_BLOCKS, "BLOCKS.BMP", 4, 2, IMG_BLOCKS
SET SPRITE FRAME SPR_BLOCKS, 1
SPRITE SPR_BLOCKS, 999, 999, IMG_BLOCKS
load image "BLOCK_OUTLINE.BMP", SCRATCH_BLOCK
SPRITE SCRATCH_BLOCK, 999, 999, SCRATCH_BLOCK
Load_Level()
`LOAD EXPLOSION ANIMATION
CREATE ANIMATED SPRITE SPR_EXPLOSION, "EXPLOSION.BMP", 7, 1, SPR_EXPLOSION
FOR N = 1 TO 3
CLONE SPRITE SPR_EXPLOSION, SPR_EXPLOSION + N
NEXT N
`LOAD THE POWERUP ANIMATIONS
powerups(0) = 0
powerups(1) = 0
powerups(2) = 0
powerups(3) = 0
CREATE ANIMATED SPRITE SPR_POWERUP, "POWERUPA.BMP", 1, 7 ,SPR_POWERUP
CREATE ANIMATED SPRITE SPR_POWERUP+1, "POWERUPB.BMP", 1, 7, SPR_POWERUP+1
CREATE ANIMATED SPRITE SPR_POWERUP+2, "POWERUPC.BMP", 1, 7, SPR_POWERUP+2
CREATE ANIMATED SPRITE SPR_POWERUP+3, "POWERUPD.BMP", 1, 7, SPR_POWERUP+3
load image "POWERUP_OUTLINE.BMP", SCRATCH_POWERUP
SPRITE SCRATCH_POWERUP, 999, 999, SCRATCH_POWERUP
`load sound EFFECTS
load sound "paddle.WAV", SND_paddle
load sound "BLOCK.WAV", SND_BLOCK
load sound "MEDAL.WAV", SND_WIN
load sound "CORK.WAV", SND_POWERUP1
load sound "BOING.WAV", SND_POWERUP2
load sound "AXE.WAV", SND_POWERUP3
load sound "CRUSH.WAV", SND_POWERUP4
load sound "GONG.WAV", SND_GONG
play sound SND_GONG
REM SETUP NICE FONT
SET TEXT FONT "TAHOMA"
SET TEXT SIZE 14
SET TEXT TO BOLD
endfunction
function Run_Game()
`REM MAIN LOOP
DO
`DRAW BACKGROUND
PASTE IMAGE IMG_BACKGROUND, 0, 0
`MOVE THE paddle WITH THE MOUSE
MOVE_paddle()
` MOVE THE ball BASED ON GAME STATE
SELECT gameState
`NORMAL GAMEPLAY STATE
CASE 1:
DRAW_BLOCKS()
MOVE_ball()
CHECK_COLLISION()
MOVE_powerups()
DRAW_explosions()
EndCASE
`LOST A LIFT-ball FOLLOWING paddle STATE
CASE 2:
DRAW_BLOCKS()
MOVE_powerups()
TRACK_ball()
IF MOUSECLICK() = 1 THEN gameState = 1
EndCASE
`GAME OVER STATE
CASE 3:
`HIDE ALL SPRITES
CENTER TEXT SCREEN WIDTH() / 2, SCREEN HEIGHT() / 2, "GAME OVER"
lives = 3
IF MOUSECLICK() = 1
autoPlay = 1
gameState = 2
score = 0
level = 0
Load_Level()
WAIT 1000
EndIF
EndCASE
EndSELECT
`PRINT NUMBER OF lives LEFT
INK RGB(0,255,128), 0
TEXT 40, 20, "LIVES:" + STR$(LIVES)
`PRINT THE score
INK RGB(255,255,0), 0
CENTER TEXT SCREEN WIDTH() / 2,20, "SCORE: " + STR$(SCORE)
`PRINT THE CURRENT level
INK RGB(128,255,128), 0
TEMP AS STRING
TEMP= "level" + STR$(level)
TEXT SCREEN WIDTH() - TEXT WIDTH(TEMP) - 40, 20, TEMP
SYNC
IF INKEY$() = "A" THEN autoPlay = 1
IF ESCAPEKEY() = 1 THEN EXIT
LOOP
endfunction
function Load_Level()
`READ THE NUMBER OF levelS
RESTORE
READ maxlevel
`GO TO THE NEXT level
level = level + 1
IF level > maxlevel THEN level=1
`JUMP TO THE START OF level DATA
SELECT level
CASE 1: RESTORE level1: EndCASE
CASE 2: RESTORE level2: EndCASE
CASE 3: RESTORE level3: EndCASE
`ADD MORE levelS HERE
EndSELECT
`READ THE level DATA
FOR Y = 1 TO 16
FOR X = 1 TO 16
READ blockData(X,Y)
NEXT X
NEXT Y
endfunction
function DRAW_BLOCKS()
W = SPRITE WIDTH(SCRATCH_BLOCK) + 1
Y = SPRITE HEIGHT(SCRATCH_BLOCK) + 1
BLOCKCOUNT = 0
FOR X = 1 TO 16
FOR Y = 1 TO 16
IF blockData(X,Y) > 0
INC BLOCKCOUNT
`SET TEMP SPRITE TO TEST FOR COLLISION
SPRITE SCRATCH_BLOCK, 50 + W * X, 50 + H * Y, SCRATCH_BLOCK
HIDE SPRITE SCRATCH_BLOCK
`DRAW THE BLOCK
DX = 50 + W * X
DY = 50 + H * Y
SET SPRITE FRAME SPR_BLOCKS, blockData(X,Y)
PASTE SPRITE SPR_BLOCKS, DX, DY
`CHECK FOR COLLISION WITH ball
IF SPRITE COLLISION (SCRATCH_BLOCK,SPR_ball) = 1
PLAY SOUND SND_BLOCK
blockData(X,Y) = 0
score = score + 1
BOUNCE_OFF_BLOCK()
IF (RND(10) < 4) THEN LAUNCH_POWERUP(DX,DY)
EndIF
EndIF
NEXT Y
NEXT X
`CHECK FOR CLEARED BLOCKS
IF BLOCKCOUND = 0
PLAY SOUND SND_WIN
gameState = 2 `LAUNCH MODE
score = score + 500
powerups(0) = 0
powerups(1) = 0
powerups(2) = 0
powerups(3) = 0
ball.X = paddle.X
Load_Level()
EndIF
endfunction
function MOVE_paddle
`GET MOUST MOVEMENT VALUE
M = MOUSEMOVEX()
IF M <> 0
paddle.X = paddle.X + M
autoPlay = 0
EndIF
`AUTOPLAY MODE
IF autoPlay = 1
CENTERX = paddle.X + SPRITE WIDTH(SPR_paddle) / 2
IF ball.X < CENTERX - 2 THEN DEC paddle.X, 6
IF ball.X > CENTERX + 2 THEN INC paddle.X, 6
EndIF
`MOVE THE paddle BASED ON MOUSE MOVEMENT
IF paddle.X < 30 THEN paddle.X = 30
RIGHT = SCREEN WIDTH() - SPRITE WIDTH(SPR_paddle) - 30
IF paddle.X > RIGHT
paddle.X = RIGHT
EndIF
`DRAW THE paddle
SPRITE SPR_paddle, paddle.X, paddle.Y, paddleImage
endfunction
function MOVE_ball()
`MOVE THE ball IN THE X DIRECTION
ball.X = ball.X + ball.SPEEDX
`BOUNCE THE ball OFF THE RIGHT AND LEFT EDGES
IF ball.X < 30
ball.X = 30
ball.SPEEDX = ball.SPEEDX * -1
EndIF
IF ball.X > SCREEN WIDTH() - 35
ball.X = SCREEN WIDTH() - 35
ball.SPEEDX = ball.SPEEDX * -1
EndIF
`MOVE THE ball IN THE Y DIRECTION
ball.Y = ball.Y + ball.SPEEDY
`BOUNCE THE ball OFF THE TOP AND BOTTOM
IF ball.Y < 20
ball.Y = 20
ball.SPEEDY = ball.SPEEDY * -1
EndIF
`HIT THE BOTTOM EDGE YOU LOSE
IF ball.Y > SCREEN HEIGHT() - 6
lives = lives - 1
IF lives < 1
gameState = 3
ELSE
gameState = 2
EndIF
EndIF
`DRAW THE ball
SPRITE SPR_ball, ball.X, ball.Y, IMG_ball
endfunction
function TRACK_ball()
`ATTACH ball TO THE TOP OF THE paddle
ball.X = paddle.X + SPRITE WIDTH(SPR_paddle) / 2 - SPRITE WIDTH(SPR_ball) / 2
ball.Y = paddle.Y - 10
ball.SPEEDX = RND(2) + 1
ball.SPEEDY = RND(2) - 6
`DRAW THE ball
SPRITE SPR_ball, ball.X, ball.Y, IMG_ball
endfunction
function CHECK_COLLISION()
`HIT ball?
IF SPRITE COLLISION(SPR_paddle, SPR_ball) = 1
BOUNCE_OFF_paddle()
PLAY SOUND SND_paddle
EndIF
`POWERUP 1 MAKES paddle SMALL
IF powerups(0) = 1
IF SPRITE COLLISION(SPR_paddle, SPR_POWERUP)
PLAY SOUND SND_POWERUP1
EXPLODE(SPRITE X(SPR_POWERUP), SPRITE Y(SPR_POWERUP))
powerups(0) = 0
paddleImage = IMG_paddle1
HIDE SPRITE SPR_POWERUP
EndIF
EndIF
`POWERUP 2 MAKES paddle MEDIUM
IF powerups(1) = 1
IF SPRITE COLLISION(SPR_paddle, SPR_POWERUP+1)
PLAY SOUND SND_POWERUP2
EXPLODE(SPRITE X(SPR_POWERUP+1), SPRITE Y(SPR_POWERUP+1))
powerups(1) = 0
paddleImage = IMG_paddle2
HIDE SPRITE SPR_POWERUP+1
EndIF
EndIF
`POWERUP 3 MAKES paddle LARGE
IF powerups(2) = 1
IF SPRITE COLLISION(SPR_paddle, SPR_POWERUP+2)
PLAY SOUND SND_POWERUP3
EXPLODE(SPRITE X(SPR_POWERUP+2), SPRITE Y(SPR_POWERUP+2))
powerups(2) = 0
paddleImage = IMG_paddle3
HIDE SPRITE SPR_POWERUP+2
EndIF
EndIF
`POWERUP 4 GAINS EXTRA LIFE AND 50 PTS
IF powerups(3) = 1
IF SPRITE COLLISION(SPR_paddle, SPR_POWERUP+3)
PLAY SOUND SND_POWERUP4
lives = lives + 1
score = score + 50
EXPLODE(SPRITE X(SPR_POWERUP+3), SPRITE Y(SPR_POWERUP+3))
powerups(3) = 0
HIDE SPRITE SPR_POWERUP+3
EndIF
EndIF
endfunction
function BOUNCE_OFF_paddle()
`GET THE ball CENTER
AX = SPRITE X(SPR_ball) + SPRITE WIDTH(SPR_ball) / 2
AY = SPRITE Y(SPR_ball) + SPRITE HEIGHT(SPR_ball) / 2
`GET THE paddle CENTER
BX = SPRITE X(SPR_paddle) + SPRITE WIDTH(SPR_paddle) / 2
BY = SPRITE Y(SPR_paddle) + SPRITE HEIGHT(SPR_paddle) / 2
`GET paddle EDGES
LEFT = SPRITE X(SPR_paddle)
RIGHT = SPRITE X(SPR_paddle) + SPRITE WIDTH(SPR_paddle)
`ball HITS LEFT EDGE OF paddle
IF AX < LEFT + 6
ball.SPEEDX = 0 - RND(4) - 4
ELSE
IF AX < LEFT + 12
ball.SPEEDX = 0 - RND(2) - 2
EndIF
EndIF
``ball HITS RIGHT EDGE OF PADDEL
IF AX > LEFT - 6
ball.SPEEDX = RND(4) + 4
ELSE
IF AX > LEFT - 12
ball.SPEEDX = RND(2) + 2
EndIF
EndIF
`SET ball Y DIRECTION TO UP
ball.SPEEDY = 0 - RND(4) - 4
endfunction
function BOUNCE_OFF_BLOCK()
`GET ball CENTER
AX = SPRITE X(SPR_ball) + SPRITE WIDTH(SPR_ball) / 2
AY = SPRITE Y(SPR_ball) + SPRITE HEIGHT(SPR_ball) / 2
`GET THE BLOCK CENTER
BX = SPRITE X(SCRATCH_BLOCK) + SPRITE WIDTH(SCRATCH_BLOCK) / 2
BY = SPRITE Y(SCRATCH_BLOCK) + SPRITE HEIGHT(SCRATCH_BLOCK) / 2
`GET BLOCK EDGES
LEFT = SPRITE X(SCRATCH_BLOCK)
RIGHT = SPRITE X(SCRATCH_BLOCK) + SPRITE WIDTH(SCRATCH_BLOCK)
`IF ball HITS THE FAR LEFT EDGE TREAT IT AS A LEFT EDGE HIT
IF AX < LEFT + 1
ball.SPEEDX = ABS(ball.SPEEDX) * -1
ELSE
`IF ball HITS THE FAR RIGHT EDGE TREAT IT AS A RIGHT EDGE HIT
IF AX > RIGHT - 1
ball.SPEEDX = ABS(ball.SPEEDX)
else
`NOW JUST HANDLE TOP OR BOTTOMS EDGE HITS NORMALLY
IF AY < BY
ball.SPEEDY = ABS(ball.SPEEDY) * -1
ELSE
ball.SPEEDY = ABS(ball.SPEEDY)
EndIF
EndIF
EndIF
endfunction
function DRAW_explosions()
`DRAW ANY EXPLOSIIONS CURRENTLY IN PROGRESS
FOR N = 0 TO 3
IF explosions(N) = 1
NUM = SPR_EXPLOSION + N
IF TIMER() > explosionTimer(N) + 400
explosions(N) = 0
HIDE SPRITE NUM
EndIF
PLAY SPRITE NUM,1 ,7 ,40
PASTE SPRITE NUM, SPRITE X(NUM), SPRITE Y(NUM)
EndIF
NEXT N
endfunction
function MOVE_powerups()
FOR N = 0 TO 3
IF powerups(N) = 1
NUM = SPR_POWERUP + N
X = SPRITE X(NUM)
Y = SPRITE y(NUM) + 2
`DRAW POWERUP IMAGE
SPRITE NUM, X, Y, NUM
PLAY SPRITE NUM, 1, 7, 50 + RND(5)
PASTE SPRITE NUM, X, Y
IF SPRITE Y(NUM) > SCREEN HEIGHT() -10 THEN powerups(N) = 0
ELSE
HIDE SPRITE N + 10
EndIF
NEXT N
endfunction
function EXPLODE(X,Y)
FOR N = 0 TO 3
IF explosions(N) = 0
explosions(N) = 1
SPRITE SPR_EXPLOSION + N, X, Y, SPR_EXPLOSION + N
explosionTimer(N) = TIMER()
EndIF
NEXT N
endfunction
function LAUNCH_POWERUP(X as word,Y as word)
FOR N = 0 TO 3
IF (powerups(N) = 0)
powerups(N) = 1
SPRITE SPR_POWERUP + N, X, Y, 10
EXIT
EndIF
NEXT N
endfunction
`
Authors
remstart
DARKANOID
Chapter 14
May, 2006
remend
`define the sprite structure
type SPRITET
x as integer
y as integer
speedx as integer
speedy as integer
endtype
`background image
global IMG_BACKGROUND as word = 1
`ball variables
global IMG_BALL as word = 3
global SPR_BALL as word = 3
global ball as SPRITET
`block variables
global IMG_BLOCKS as word = 100
global SPR_BLOCKS as word = 100
global SCRATCH_BLOCK as word = 101
dim blockData(20,20) as byte
`paddle variables
global SPR_PADDLE as word = 5
global IMG_PADDLE1 as word = 20
global IMG_PADDLE2 as word = 21
global IMG_PADDLE3 as word = 22
global paddle as SPRITET
global paddleImage as word = IMG_PADDLE1
`game state variables
global gameState as byte = 2
global autoPlay as byte = 0
global score as dword = 0
global level as word = 0
global maxlevels as word
global lives as byte = 3
`powerup states
global SPR_POWERUP as word = 10
global SCRATCH_POWERUP as word = 14
dim powerups(4) as byte
dim powerupImage(4) as byte
`for blowing stuff up
global SPR_EXPLOSION as word = 15
dim explosionTimer(4) as byte
dim explosions(4) as byte
`sound effects
global SND_PADDLE as byte = 1
global SND_BLOCK as byte = 2
global SND_WIN as byte = 3
global SND_GONG as byte = 4
global SND_POWERUP2 as byte = 5
global SND_POWERUP1 as byte = 6
global SND_POWERUP3 as byte = 7
global SND_POWERUP4 as byte = 8
`start game running
Init_Game()
Run_Game()
End
function Init_Game()
sync on
sync rate 40
hide mouse
randomize timer()
`load background
load image "background.bmp", IMG_BACKGROUND
`load paddle
load image "paddle1.bmp", IMG_PADDLE1
sprite SPR_PADDLE, 999, 999, IMG_PADDLE1
paddle.X = SCREEN WIDTH() / 2 - SPRITE WIDTH(SPR_PADDLE) / 2
paddle.Y = SCREEN HEIGHT() - 50
load image "paddle2.bmp", IMG_PADDLE2
load image "paddle3.bmp", IMG_PADDLE3
`load the ball
load image "ball.bmp", IMG_BALL
sprite SPR_BALL, 999, 999, IMG_BALL
ball.speedx = 1
ball.SpeedY = -1
`load the blocks
create animated sprite SPR_BLOCKS, "blocks.bmp", 4, 2, IMG_BLOCKS
set sprite frame SPR_BLOCKS, 1
sprite SPR_BLOCKS, 999, 999, IMG_BLOCKS
load image "block_outline.bmp", SCRATCH_BLOCK
sprite SCRATCH_BLOCK, 999, 999, SCRATCH_BLOCK
Load_Level()
`load explosion animation
create animated sprite SPR_EXPLOSION, "explosion.bmp", 7, 1, SPR_EXPLOSION
for n = 1 to 3
clone sprite SPR_EXPLOSION, SPR_EXPLOSION + n
next n
`load the powerup animations
powerups(0) = 0
powerups(1) = 0
powerups(2) = 0
powerups(3) = 0
create animated sprite SPR_POWERUP, "powerupa.bmp", 1, 7, SPR_POWERUP
create animated sprite SPR_POWERUP+1, "powerupb.bmp", 1, 7, SPR_POWERUP+1
create animated sprite SPR_POWERUP+2, "powerupc.bmp", 1, 7, SPR_POWERUP+2
create animated sprite SPR_POWERUP+3, "powerupd.bmp", 1, 7, SPR_POWERUP+3
load image "powerup_outline.bmp", SCRATCH_POWERUP
sprite SCRATCH_POWERUP, 999, 999, SCRATCH_POWERUP
`load sound effects
load sound "paddle.wav", SND_PADDLE
load sound "block.wav", SND_BLOCK
load sound "medal.wav", SND_WIN
load sound "cork.wav", SND_POWERUP1
load sound "boing.wav", SND_POWERUP2
load sound "axe.wav", SND_POWERUP3
load sound "crush.wav", SND_POWERUP4
load sound "gong.wav", SND_GONG
play sound SND_GONG
`*** uncomment and insert your own favorite MP3 song here ***
`load music "music.mp3", 1
`loop music 1
`set music volume 1, 40
rem Setup nice font
set text font "Tahoma"
set text size 14
set text to bold
endfunction
function Run_Game()
rem Main loop
do
`draw background
paste image IMG_BACKGROUND, 0, 0
`move the paddle with the mouse
Move_Paddle()
`move the ball based on game state
SELECT gameState
`normal gameplay state
case 1:
Draw_Blocks()
Move_Ball()
Check_Collision()
Move_Powerups()
Draw_Explosions()
endcase
`lost a life--ball following paddle state
CASE 2:
Draw_Blocks()
Move_Powerups()
Track_Ball()
if mouseclick() = 1 then gameState = 1
endcase
`game over state
CASE 3:
`hide all sprites
center text screen width() / 2, screen height() / 2, "GAME OVER"
lives = 3
if mouseclick() = 1
autoPlay = 1
gameState = 2
score = 0
level = 0
Load_Level()
wait 1000
endif
endcase
endselect
`print number of lives left
ink rgb(0,255,128), 0
text 40, 20, "LIVES: " + str$(lives)
`print the score
ink rgb(255,255,0),0
center text screen width()/2,20, "SCORE: " + str$(score)
`print the current level
ink rgb(128,255,128), 0
temp as string
temp = "LEVEL " + str$(level)
text screen width() - text width(temp) - 40, 20, temp
sync
if inkey$() = "a" then autoPlay = 1
if escapekey() = 1 then exit
loop
endfunction
function Load_Level()
`read the number of levels
restore
read maxlevel
`go to the next level
level = level + 1
if level > maxlevel then level = 1
`jump to the start of the level data
select level
case 1: restore LEVEL1: endcase
case 2: restore LEVEL2: endcase
case 3: restore LEVEL3: endcase
`add more levels here
endselect
`read the level data
for y = 1 to 16
for x = 1 to 16
read blockData(x,y)
next x
next y
endfunction
function Draw_Blocks()
w = sprite width(SCRATCH_BLOCK) + 1
h = sprite height(SCRATCH_BLOCK) + 1
blockCount = 0
for x = 1 to 16
for y = 1 to 16
if blockData(x,y) > 0
inc blockCount
`set temporary sprite to test for collision
sprite SCRATCH_BLOCK, 50 + w * x, 50 + h * y, SCRATCH_BLOCK
hide sprite SCRATCH_BLOCK
`draw the block
dx = 50 + w * x
dy = 50 + h * y
set sprite frame SPR_BLOCKS, blockData(x,y)
paste sprite SPR_BLOCKS, dx, dy
`check for collision with ball
if sprite collision(SCRATCH_BLOCK, SPR_BALL) = 1
play sound SND_BLOCK
blockData(x,y) = 0
score = score + 1
Bounce_Off_Block()
if (rnd(10) < 4) then Launch_Powerup(dx,dy)
endif
endif
next y
next x
`check for cleared blocks
if blockCount = 0
play sound SND_WIN
gameState = 2 `launch mode
score = score + 500
powerups(0) = 0
powerups(1) = 0
powerups(2) = 0
powerups(3) = 0
ball.x = paddle.x
Load_Level()
endif
endfunction
function Move_Paddle()
`get mouse movement value
m = mousemovex()
if m <> 0
paddle.X = paddle.X + m
autoPlay = 0
endif
`autoplay mode?
if autoPlay = 1
centerx = paddle.x + sprite width(SPR_PADDLE)/2
if ball.x < centerx - 2 then dec paddle.x, 6
if ball.x > centerx + 2 then inc paddle.x, 6
endif
`move the paddle based on mouse movement
if paddle.x < 30 then paddle.x = 30
right = screen width() - sprite width(SPR_PADDLE) - 30
if paddle.x > right
paddle.x = right
endif
`draw the paddle
sprite SPR_PADDLE, paddle.x, paddle.y, paddleImage
endfunction
function Move_Ball()
`move the ball in the X direction
ball.x = ball.x + ball.speedx
`bounce the ball off the right and left edges
if ball.x < 30
ball.x = 30
ball.speedx = ball.speedx * -1
endif
if ball.x > screen width() - 35
ball.x = screen width() - 35
ball.speedx = ball.speedx * -1
endif
`move the ball in the Y direciton
ball.y = ball.y + ball.speedy
`bounce the ball off the top edge
if ball.y < 20
ball.y = 20
ball.speedy = ball.speedy * -1
endif
`hit the bottom edge...you lose!
if ball.y > screen height() - 6
lives = lives - 1
if lives < 1
gameState = 3
else
gameState = 2
endif
endif
`draw the ball
sprite SPR_BALL, ball.x, ball.y, IMG_BALL
endfunction
function Track_Ball()
`attach the ball to the top of the paddle
ball.x = paddle.x + sprite width(SPR_PADDLE) / 2 - sprite width(SPR_BALL) / 2
ball.y = paddle.y - 10
ball.speedx = rnd(2) + 1
ball.speedy = rnd(2) - 6
`draw the ball
sprite SPR_BALL, ball.x, ball.y, IMG_BALL
endfunction
function Check_Collision()
`hit ball?
if sprite collision(SPR_PADDLE, SPR_BALL) = 1
Bounce_Off_Paddle()
play sound SND_PADDLE
endif
`powerup 1 makes paddle small
if powerups(0) = 1
if sprite collision(SPR_PADDLE, SPR_POWERUP)
play sound SND_POWERUP1
Explode(sprite x(SPR_POWERUP), sprite y(SPR_POWERUP))
powerups(0) = 0
paddleImage = IMG_PADDLE1
hide sprite SPR_POWERUP
endif
endif
`powerup 2 makes paddle medium
if powerups(1) = 1
if sprite collision(SPR_PADDLE, SPR_POWERUP+1)
play sound SND_POWERUP2
Explode(sprite x(SPR_POWERUP+1), sprite y(SPR_POWERUP+1))
powerups(1) = 0
paddleImage = IMG_PADDLE2
hide sprite SPR_POWERUP+1
endif
endif
`powerup 3 makes paddle big
if powerups(2) = 1
if sprite collision(SPR_PADDLE, SPR_POWERUP+2)
play sound SND_POWERUP3
Explode(sprite x(SPR_POWERUP+2), sprite y(SPR_POWERUP+2))
powerups(2) = 0
paddleImage = IMG_PADDLE3
hide sprite SPR_POWERUP+2
endif
endif
`powerup 4 gains extra life and 50 points
if powerups(3) = 1
if sprite collision(SPR_PADDLE, SPR_POWERUP+3)
play sound SND_POWERUP4
lives = lives + 1
score = score + 50
Explode(sprite x(SPR_POWERUP+3), sprite y(SPR_POWERUP+3))
powerups(3) = 0
hide sprite SPR_POWERUP+3
endif
endif
endfunction
function Bounce_Off_Paddle()
`get the ball center
ax = sprite x(SPR_BALL) + sprite width(SPR_BALL) / 2
ay = sprite y(SPR_BALL) + sprite height(SPR_BALL) / 2
`get the paddle center
bx = sprite x(SPR_PADDLE) + sprite width(SPR_PADDLE) / 2
by = sprite y(SPR_PADDLE) + sprite height(SPR_PADDLE) / 2
`get paddle edges
left = sprite x(SPR_PADDLE)
right = sprite x(SPR_PADDLE) + sprite width(SPR_PADDLE)
`ball hits left edge of paddle
if ax < left + 6
ball.speedx = 0 - rnd(4) - 4
else
if ax < left + 12
ball.speedx = 0 - rnd(2) - 2
endif
endif
`ball hits right edge of paddle
if ax > right - 6
ball.speedx = rnd(4) + 4
else
if ax > right - 12
ball.speedx = rnd(2) + 2
endif
endif
`set ball y direction to up
ball.speedy = 0 - rnd(4) - 4
endfunction
function Bounce_Off_Block()
`make the ball bounce off the block realistically
`get the ball center
ax = sprite x(SPR_BALL) + sprite width(SPR_BALL) / 2
ay = sprite y(SPR_BALL) + sprite height(SPR_BALL) / 2
`get the block center
bx = sprite x(SCRATCH_BLOCK) + sprite width(SCRATCH_BLOCK) / 2
by = sprite y(SCRATCH_BLOCK) + sprite height(SCRATCH_BLOCK) / 2
`get block edges
left = sprite x(SCRATCH_BLOCK)
right = sprite x(SCRATCH_BLOCK) + sprite width(SCRATCH_BLOCK)
`if ball hits the far left edge, treat it as a left edge hit
if ax < left + 1
ball.speedx = abs(ball.speedx) * -1
else
`if ball hits the far right edge, treat it as a right edge hit
if ax > right - 1
ball.speedx = abs(ball.speedx)
else
`now just handle top or bottom edge hits normally
if ay < by
ball.speedy = abs(ball.speedy) * -1
else
ball.speedy = abs(ball.speedy)
endif
endif
endif
endfunction
function Draw_Explosions()
`draw any explosions currently in process
for n = 0 to 3
if explosions(n) = 1
num = SPR_EXPLOSION + n
if timer() > explosionTimer(n) + 400
explosions(n) = 0
hide sprite num
endif
play sprite num, 1, 7, 40
paste sprite num, sprite x(num), sprite y(num)
endif
next n
endfunction
function Move_Powerups()
for n = 0 to 3
if powerups(n) = 1
num = SPR_POWERUP + n
x = sprite x(num)
y = sprite y(num) + 2
`draw powerup image
sprite num, x, y, num
play sprite num, 1, 7, 50 + rnd(5)
paste sprite num, x, y
if sprite y(num) > screen height() - 10 then powerups(n) = 0
else
hide sprite n+10
endif
next n
endfunction
function Explode(x,y)
for n = 0 to 3
if explosions(n) = 0
explosions(n) = 1
sprite SPR_EXPLOSION + n, x, y, SPR_EXPLOSION + n
explosionTimer(n) = Timer()
endif
next n
endfunction
function Launch_Powerup(x as word, y as word)
for n = 0 to 3
if (powerups(n) = 0)
powerups(n) = 1
sprite SPR_POWERUP+n, x, y, 10
exit
endif
next n
endfunction