This behaviour is a all in one behaviour. Create a new file and just stick it somewhere in the frame script channel and everything will work automaticly.
--------------------------------------------------------------------------
--
-- (C) by BaZ 2004
--
-- Game Instructions :
--
-- Paddle is moved by left-arrow and right-arrow key.
-- Game is started by pressing the return/enter key
--
-- Game Object :
--
-- Simple actually move the paddle to bounce the ball back up
-- and score as many points as you can.
--
-- About :
--
-- Why this game ? It's done a million times, well, ya true but
-- not in one behaviour as far as I know....
-- Got a comment on this piece ? Mail me at barry@daarmaar.nl
--
--------------------------------------------------------------------------
on getPropertyDescriptionList me
descriptionList = [:]
addProp descriptionList, #pStartLives, [#comment: "Starting Lives", #format: #integer, #default: 3]
return descriptionList
end
on beginSprite me
-- Start state of game
pMode = #newgame
-- Screen settings
pStage = (the stage).image
pStageW = (the stage).rect.width
pStageH = (the stage).rect.height
pStageRect = pStage.rect
-- Clear screen
pStage.fill(pStageRect, rgb(0,0,0))
-- Welcome message
-- Start message
-- If no member score exist make one
if member("start").number = -1 then
pGameOverMem = new(#text)
pGameOverMem.name = "start"
-- Text color
member("start").foreColor = 34
-- Text Size
member("start").fontSize = 16
end if
tText = ""
put "Press Enter to play." after tText
member("start").text = tText
end
on endSprite me
-- Clean up all
pStage.fill(pStageRect, rgb(0,0,0))
end
on exitFrame me
-- Program State descision
case pMode of
#newgame:
--------------------------------------------------------------------------
-- Method init values for a new level.
--------------------------------------------------------------------------
on mInitLevel me
-- Reset all live and score stuff
pLives = pStartLives
pScore = 0
-- Level
pLevel = 1
-- Display
me.mShowScore()
-- Now it's time to setup the game elements
me.mInitObj()
end
--------------------------------------------------------------------------
-- Method add points to score.
--------------------------------------------------------------------------
on mAddPoints me, whichAddPoints
pScore = pScore + whichAddPoints
me.mShowScore()
end
on mShowScore me
-- If no member score exist make one
if member("score").number = -1 then
pScoreMem = new(#text)
pScoreMem.name = "score"
-- Text color
member("score").foreColor = 34
end if
tText = ""
put "Score:"&&pScore&&"Level:"&&pLevel&&"Lives:"&&pLives after tText
member("score").text = tText
end
--------------------------------------------------------------------------
-- Method take care of u lives
--------------------------------------------------------------------------
on mEndLife me
pLives = pLives - 1
if pLives < 0 then
pMode = #endgame
else
me.mShowScore()
-- Reset all game pieces except the bricks
me.mResetObj()
end if
end
--------------------------------------------------------------------------
-- Method Init and create game obj's
--------------------------------------------------------------------------
on mInitObj me
-- Make a paddle
pPaddleW = 50
pPaddleH = 16
pPaddleSpeed = 10
pPaddleImg = image(pPaddleW,pPaddleH, 16)
pPaddleImg.draw(0, 0, pPaddleW, (pPaddleH - 1), [#shapeType:#rect, #lineSize:1, #color: rgb(150, 0, 0)])
pPaddleLoc = [(pStageW / 2), (pStageH - pPaddleH) - 15]
-- Make a ball
pBallRadius = 5
pBallSpeed = 5
pBallImg = image((pBallRadius * 2), (pBallRadius * 2), 16)
pBallImg.fill(0, 0, (pBallRadius * 2), (pBallRadius * 2), [#shapeType:#oval, #lineSize:1, #color: rgb(150, 0, 0)])
pBallLoc = [pStageW/2, pStageH/2]
pDirection = [1,-1]
-- Make bricks
pBrickW = 20
pBrickH = 10
pBrickImg = image(pBrickW, pBrickH, 16)
pBrickImg.draw(0, 0, pBrickW, pBrickH, [#shapeType:#rect, #lineSize:1, #color: rgb(0, 0, 255)])
me.mCreateBricks(20, 6)
-- Game Over message
-- If no member score exist make one
if member("gameover").number = -1 then
pGameOverMem = new(#text)
pGameOverMem.name = "gameover"
-- Text color
member("gameover").foreColor = 34
end if
tText = ""
put "GameOver:"&RETURN&RETURN&"Press Enter to play again." after tText
member("gameover").text = tText
end
--------------------------------------------------------------------------
-- Method to create the bricks in memory
--------------------------------------------------------------------------
on mCreateBricks me, whichNumber, whichColNumber
-- Create a list with bricks to hold there state and position
pBrickLst = [:]
-- Starting offset
tOffSetX = 45
tOffSetY = 45
tSpaceX = 10
tSpaceY = 20
tColCnt = whichColNumber -- Number of bricks on one row
tCol = 1
repeat with tBrick = 1 to whichNumber
-- Save the coordinates
tBrickPos = [tOffSetX, tOffSetY]
-- Calculate new offset
tOffSetX = tOffSetX + pBrickW + tSpaceX
if tCol = tColCnt then
-- Update Y offset only after a number of columns
tOffSetY = tOffSetY + pBrickH + tSpaceY
-- Set the column counter back to one and the offset of x
tOffSetX = 25
tCol = 0
end if
tCol = tCol + 1
tBrickLst = [tBrickPos, #alive]
pBrickLst.addProp(symbol("brick"&tBrick), tBrickLst)
end repeat
end
--------------------------------------------------------------------------
-- Method reset game obj's
--------------------------------------------------------------------------
on mResetObj me
-- Paddle
pPaddleLoc = [(pStageW / 2), (pStageH - pPaddleH) - 35]
-- Ball
pBallLoc = [pStageW/2, pStageH/2]
pDirection = [1,-1]
end
--------------------------------------------------------------------------
-- Method to update ball position and do apropiate checks.
--------------------------------------------------------------------------
on mUpdateBall me
-- As a test we use boundaries from the stage as a point of collision
if pBallLoc[1] <= 0 or pBallLoc[1] >= pStageW then
-- hit sides so change x dir.
if pDirection[1] = -1 then
pDirection[1] = 1
else
pDirection[1] = -1
end if
end if
-- Hit the top
if pBallLoc[2] <= 0 then pDirection[2] = 1
-- Hit the bottom
if pBallLoc[2] >= pStageH then me.mEndLife()
-- Hit the paddle
tRect = me.fDrawReturnRect(#paddle, pPaddleLoc)
if point(pBallLoc[1], pBallLoc[2]).inside(tRect) then
pDirection[2] = -1
end if
-- Hit a brick
repeat with tBrick = 1 to pBrickLst.count
tLoc = pBrickLst[tBrick][1]
tRect = me.fDrawReturnRect(#brick, tLoc)
-- If the brick is hit
if point(pBallLoc[1], pBallLoc[2]).inside(tRect) then
-- Put it offscreen
pBrickLst[tBrick][1] = [-999, -999]
-- Set it to dead
pBrickLst[tBrick][2] = #dead
-- Add to score
me.mAddPoints(10)
end if
--------------------------------------------------------------------------
-- Method where all game elements are drawn
--------------------------------------------------------------------------
on mDrawGame me
-- Clear stage
pStage.fill(pStageRect, rgb(0,0,0))
-- Draw the paddle
tRect = me.fDrawReturnRect(#paddle, pPaddleLoc)
pStage.copypixels(pPaddleImg, tRect, pPaddleImg.rect, [#ink:36])
-- Draw the ball
tRect = me.fDrawReturnRect(#ball, pBallLoc)
pStage.copypixels(pBallImg, tRect, pBallImg.rect, [#ink:36])
-- Draw the walls
-- Draw the bricks
repeat with tBrick = 1 to pBrickLst.count
tLoc = pBrickLst[tBrick][1]
tRect = me.fDrawReturnRect(#brick, tLoc)
pStage.copypixels(pBrickImg, tRect, pBrickImg.rect, [#ink:36])
end repeat
-------------------------------------------------------------------------
-- Method to check if the game level is completed.
--------------------------------------------------------------------------
on mEndLevel me
tBrickCnt = 0
repeat with tBrick = 1 to pBrickLst.count
if pBrickLst[tBrick][2] = #dead then
tBrickCnt = tBrickCnt + 1
end if
end repeat
-- Check if all bricks are dead
if tBrickCnt = pBrickLst.count then
-- New level again
me.mResetObj()
-- New bricks
me.mCreateBricks(30, 10)
-- Raise level
pLevel = pLevel + 1
end if
end
-------------------------------------------------------------------------
-- Method where user input is checked.
--------------------------------------------------------------------------
on mKeyPressed me
if keyPressed(123) then
-- Check for left border
pPaddleLoc[1] = pPaddleLoc[1] - pPaddleSpeed
end if
if keyPressed(124) then
-- Check for right border
pPaddleLoc[1] = pPaddleLoc[1] + pPaddleSpeed
end if
end
--------------------------------------------------------------------------
-- Function to return a rect according to a point, and
-- the object in question.
--------------------------------------------------------------------------
on fDrawReturnRect me, whichObj, whichLoc
case (whichObj) of
#paddle :