Posted: 23rd Jan 2011 10:24
REM Project: Brick_game
REM Created: 21/01/2011 11:33:59
REM
REM ***** Main Source File *****
REM Dwain Maxwell
REM Define global variables
GLOBAL BallX as integer = 0
GLOBAL BallY as integer = 0
GLOBAL SpeedX as integer = 1
GLOBAL SpeedY as integer = -1
GLOBAL PaddleX as word = 5
GLOBAL PaddleY as word = 20
GLOBAL backgroundimage as integer = 1
GLOBAL ballimage as integer = 2
GLOBAL brickimage as integer = 3
GLOBAL paddleimage as integer = 6
GLOBAL ballsprite as integer = 2
GLOBAL bricksprite as integer = 3
GLOBAL paddlesprite as word = 5
GLOBAL Gamestate$ as string = "Initialize"
GLOBAL Playerturns as integer = 3
DIM BricksArray(15,15) as integer
REM Define State of Bricks
DATA 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
DATA 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
DATA 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
DATA 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
DATA 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
REM Main Function
ReadyGameForPlay()
LoadBricksArray()
PlayBricksGame()
END
REM Procedure and Functions
FUNCTION ReadyGameForPlay()
SYNC ON
SYNC RATE 60
HIDE MOUSE
RANDOMIZE TIMER()
SET TEXT FONT "Arial"
SET TEXT SIZE 16
SET TEXT TO BOLD
INK 0,0
REM Set Up Images
LOAD IMAGE "Background.bmp", Backgroundimage
LOAD IMAGE "Paddle.bmp", Paddleimage
LOAD IMAGE "Gameball.bmp", Ballimage
LOAD IMAGE "Brick.bmp", Brickimage
REM Create Sprites
SPRITE Paddlesprite, 999,999, Paddleimage
SPRITE Ballsprite, 999,999, Ballimage
SPRITE Bricksprite, 999,999, Brickimage
REM Determine length and width of paddle
PaddleX = (SCREEN WIDTH()/2) - (SPRITE WIDTH(paddlesprite)/2)
PaddleY = SCREEN HEIGHT() - 25
ENDFUNCTION
FUNCTION LoadBricksArray()
FOR j = 1 TO 5
FOR i = 1 TO 15
READ BricksArray(i,j)
NEXT i
NEXT j
ENDFUNCTION
FUNCTION PlayBricksGame()
DO
PASTE IMAGE BACKGROUNDIMAGE, 0 ,0
SELECT Gamestate$
CASE "Play"
DisplayBricks()
BounceTheBall()
LookForCollisionWithPaddle()
ENDCASE
CASE "Initialize"
ReadyBallAndPaddle()
DisplayBricks()
RESTORE
IF UPKEY() = 1
Gamestate$ = "Play"
ENDIF
ENDCASE
CASE "Over"
CENTER TEXT SCREEN WIDTH()/2 , SCREEN HEIGHT()/2, "GAME OVER"
CENTER TEXT SCREEN WIDTH()/2 , SCREEN HEIGHT()/2 + 30, "Press UP key to play new game"
IF UPKEY() = 1
PlayerTurns = 3
RESTORE
Gamestate$ = "Initialize"
LoadBricksArray()
ENDIF
ENDCASE
ENDSELECT
IF gamestate$ <> "Over"
ProcessPlayerInput()
TEXT 25,25, " Balls Remaining: " + STR$(PlayerTurns)
ENDIF
SYNC
LOOP
ENDFUNCTION
FUNCTION DisplayBricks()
Width = SPRITE WIDTH(BrickSprite) + 2
Height = SPRITE HEIGHT(BrickSprite) + 2
Blockcount = 0
FOR i = 1 to 15
FOR j = 1 to 5
IF BricksArray(i,j) = 1
INC Blockcount
SPRITE Bricksprite, 65 + Width * i, 75 + Height * j, Brickimage
PASTE SPRITE Bricksprite, 65 + Width * i, 75 + Height * j
REM Determine if there is a collision between ball and brick
IF SPRITE COLLISION(Bricksprite,Ballsprite) = 1
BricksArray(i,j) = 0
DeflectBallOffBricks()
ENDIF
ENDIF
NEXT j
NEXT i
REM All blocks cleared
IF Blockcount = 0
GAMEstate$ = "Initialize"
Loadbricksarray()
ENDIF
ENDFUNCTION
FUNCTION BounceTheBall()
BallX = BallX + SpeedX
BallY = BallY + SpeedY
ProcessSideOfScreenCollisions()
ProcessTopOfScreenCollisions()
ProcessMisses()
SPRITE Ballsprite, BallX, BallY, BallImage
ENDFUNCTION
FUNCTION ProcessSideOfScreenCollisions()
IF BallX < 1
BallX = 1
SpeedX = SpeedX * -1
ENDIF
IF BallX > SCREEN WIDTH() -14
BallX = SCREEN WIDTH() -14
SpeedX = SpeedX * -1
ENDIF
ENDFUNCTION
FUNCTION ProcessTopOfScreenCollisions()
IF BallY < 0
BallY = 0
SpeedY = SpeedY * -1
ENDIF
ENDFUNCTION
FUNCTION ProcessMisses()
IF BallY > SCREEN HEIGHT() -6
playerturns = playerturns - 1
IF playerturns < 1
Gamestate$ = "Over"
ELSE
Gamestate$ = "Initialize"
ENDIF
ENDIF
ENDFUNCTION
FUNCTION LookForCollisionWithPaddle()
IF SPRITE COLLISION(paddlesprite, ballsprite) = 1
DeflectballoffPaddle()
ENDIF
ENDFUNCTION
FUNCTION ReadyBallAndPaddle()
BallX = PaddleX + (SPRITE WIDTH(Paddlesprite)/2) - (SPRITE WIDTH(BallSprite)/2)
BallY = PaddleY - 14
SpeedX = rnd(2) + 1
SpeedY = rnd(2) - 6
SPRITE BallSprite, BallX, BallY, BallImage
ENDFUNCTION
FUNCTION ProcessPlayerInput()
IF PaddleX < 6 Then PaddleX = 6
Rightsideofpaddle = PaddleX + SPRITE WIDTH(Paddlesprite)
IF Rightsideofpaddle > SCREEN WIDTH()
PaddleX = SCREEN WIDTH() - SPRITE WIDTH(Paddlesprite)
ENDIF
IF LEFTKEY()
DEC PaddleX, 6
ENDIF
IF RIGHTKEY()
DEC PaddleX, 6
ENDIF
SPRITE Paddlesprite, PaddleX, PaddleY, Paddleimage
ENDFUNCTION
FUNCTION DeflectballoffPaddle()
Centerofball = SPRITE X(Ballsprite) + SPRITE WIDTH(Ballsprite)/2
Leftsideofpaddle = SPRITE X(Paddlesprite)
Rightsideofpaddle = SPRITE X(Paddlesprite) + SPRITE WIDTH (Paddlesprite)
IF Centerofball < leftsideofpaddle + 15
SpeedX = -RND(4) -1
ENDIF
IF Centerofball > rightsideofpaddle - 15
SpeedX = RND(4) -1
ENDIF
SpeedY = -RND(2) -3
ENDFUNCTION
FUNCTION DeflectBalloffbricks()
Centerofball = SPRITE X(Ballsprite) + SPRITE WIDTH(Ballsprite)/2
Leftsideofbrick = SPRITE X(bricksprite)
Rightsideofbrick = SPRITE X(bricksprite) + SPRITE WIDTH(Bricksprite)
IF Centerofball < leftsideofbrick OR Centerofball > rightsideofbrick
SpeedX = SpeedX * -1
ELSE
SpeedY = SpeedY * -1
ENDIF
ENDFUNCTION