Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
Volume Control-Button Only
Check QuickTime Version
Check For Qt Version
Dr.Explain - help file tool
Fake Drag Bar - Stage
Color Picker
Bubble Sort Algorithm (for Strings)
Link Email
BitChecker
Make Director window odd sized.
 

 

 

Behavior Break Out Clone

Added on 9/17/2004

 

Compatibilities:
D8 D8_5 D9 Mac PC Shockwave UK US

This item has not yet been rated

Author: BaZ

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
--
--------------------------------------------------------------------------

-- Init game settings
property pMode
property pStartLives, pLives, pScore, pScoreMem, pLevel, pGameOverMem
-- Init screen props
property pStage, pStageW, pStageH, pStageRect
-- Init paddle
property pPaddleW, pPaddleH
property pPaddleImg, pPaddleLoc, pPaddleSpeed
-- Init ball
property pBallRadius, pBallImg
property pBallSpeed, pBallLoc, pDirection
-- Init bricks
property pBrickImg, pBrickLst
property pBrickW, pBrickH

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:
      
      -- Clear screen
      pStage.fill(pStageRect, rgb(0,0,0))
      -- Draw game over message
      tOffSetX = 25
      tOffSetY = 100
      pStage.copypixels(member("start").image, rect(tOffSetX, tOffSetY, (tOffSetX + 545), (tOffSetY + 15)), member("start").rect, [#ink:36])
      
      -- Start game when fire is released
      if keypressed(36) then
        -- Set up game and prepare to play
        me.mInitLevel()
        pMode = #game
      end if
      
    #endgame :
      -- Clear screen
      pStage.fill(pStageRect, rgb(0,0,0))
      -- Draw game over message
      tOffSetX = (pStageW - 345) / 2
      tOffSetY = (pStageH - 45) / 3
      pStage.copypixels(pGameOverMem.image, rect(tOffSetX, tOffSetY, (tOffSetX + 345), (tOffSetY + 45)), pGameOverMem.rect, [#ink:36])
      
      if keypressed(36) then
        pMode = #newgame
      end if
      
    #game :
      
      me.mKeyPressed()
      me.mUpdateBall()
      me.mDrawGame()
      me.mEndLevel()
      
  end case
  
  go to the frame
end

--------------------------------------------------------------------------
-- 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

--------------------------------------------------------------------------
-- Method score showing
--------------------------------------------------------------------------

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
    
  end repeat
  
  -- Move ball
  pBallLoc = pBallLoc + (pDirection * pBallSpeed)
end

--------------------------------------------------------------------------
-- 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
  
  -- Draw score
  pStage.copypixels(member("score").image, rect(5, 5, (member("score").width + 5), (member("score").height + 5)), member("score").rect, [#ink:36])
end

-------------------------------------------------------------------------
-- 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 :
      
      tLeftPt = whichLoc[1] - (pPaddleW /2)
      tRightPt = whichLoc[1] + (pPaddleW / 2)
      tTopPt = whichLoc[2] - (pPaddleH / 2)
      tBottomPt = whichLoc[2] + (pPaddleH / 2)
      return rect(tLeftPt, tTopPt, tRightPt, tBottomPt)
      
    #ball :
      
      tLeftPt = whichLoc[1] - pBallRadius
      tRightPt = whichLoc[1] + pBallRadius
      tTopPt = whichLoc[2] - pBallRadius
      tBottomPt = whichLoc[2] + pBallRadius
      return rect(tLeftPt, tTopPt, tRightPt, tBottomPt)
      
    #brick :
      
      tLeftPt = whichLoc[1] - (pBrickW /2)
      tRightPt = whichLoc[1] + (pBrickW / 2)
      tTopPt = whichLoc[2] - (pBrickH / 2)
      tBottomPt = whichLoc[2] + (pBrickH / 2)
      return rect(tLeftPt, tTopPt, tRightPt, tBottomPt)
      
    otherwise
      --
  end case
end

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail