
Added by roScripts on August-25-2007, 2:55 pm
// ********* BRICK (aka ARKANOID) GAME ********* //
// Author: Broly
// Contact : davb86@supereva.it
//
// Snippet made in May 2005 for the dream.in.code
// spring.this('05') contest
//
// ********************************************** //
// ******************* DESCRIPTION ************************ //
//
// This snippet create a complete arkanoid-style game,
// the levels are made with XML files, the brick can have different
// colors and resistance.
// The game is entirely generated
// in Actionscript, so it's quite light (2 kb).
// The graphic is very minimal.
//
// You can see an example of the game at
// http://www.brolyweb.com/Script/Brick/Brick_Alpha.html
// NOTE: This sample has only 2 levels.
//
// ******************************************************* //
// Attention: modifying the code you can cause
// some error. Modify with care :-p
// The parts you can/must modify for customize the game
// are signed
function brick(){
// Game field vars
gamefield_w = 300
stage_h = 400
panel_h = 40
// You can modify those vars for
// set the numbers of level (that must correspond to the .xml files, every level will have its .xml)
// and total lives.
total_levels = 2
lives = 3
// Graphic vars, you can modify for change the border
// width, alpha and color
border_w = 3
border_color = 0x000000
border_alpha = 100
// As before, but for the pad (you can also set pad width and height)
pad_border_w = 1
pad_border_color = 0x000000
pad_border_alpha = 100
pad_color = 0xFF0000
pad_alpha = 70
pad_w = 50
pad_h = 10
// The available field is smaller, because we
// leave some space between the pad (with the ball) and the bricks
// DON'T MODIFY
gamefield_h = stage_h - panel_h
available_field = gamefield_h - 100
// Those vars set the starting level and score
// You can modify them but it's recommended don't
currentlevel = 1
points = 0
// This var is needed by the game, don't modify ordelete
ini_lives = lives
// This function generate the gamefield (borders and panel)
// following the values we setted in the graphic vars
// The function create also the text fields
// for the panel (score, lives and level)
function generateGameField(){
createEmptyMovieClip('borderup',-1)
createEmptyMovieClip('bordersx',-2)
createEmptyMovieClip('panel',-102)
with(borderup){
lineStyle(border_w,border_color,border_alpha)
lineTo(gamefield_w,0)
}
with(bordersx){
lineStyle(border_w,border_color,border_alpha)
lineTo(0,gamefield_h)
}
with(panel){
lineStyle(border_w,0x000000,100)
beginFill(0x9999999,80)
lineTo(gamefield_w,0)
lineTo(gamefield_w,panel_h)
lineTo(0,panel_h)
lineTo(0,0)
endFill()
_y = gamefield_h
}
createTextField('score',-100,0,0,0,0)
createTextField('lev',-101,0,0,0,0)
createTextField('liv',-99,0,0,0,0)
score.autoSize = true
score.selectable = false
score.text = "Score : " + points
score._x = panel._x + 20
score._y = panel._y + panel._height/2 - score._height/2
lev.autoSize = true
lev.selectable = false
lev.text = "Level 1"
lev._x = panel._x + panel._width - lev._width - lev._width/2
lev._y = panel._y + panel._height/2 - lev._height/2
liv.autoSize = true
liv.text = "Lives left : " + lives
liv._x = panel._x + panel._width/2 - liv._width/2
liv._y = panel._y + panel._height/2 - liv._height/2
duplicateMovieClip(borderup,'borderbottom',-4)
duplicateMovieClip(bordersx,'borderdx',-5)
borderbottom._y = gamefield_h
borderdx._x = gamefield_w
}
// This function create the pad
// using the values we setted in the graphic vars for the pad
function createPad(){
createEmptyMovieClip('pad',1)
with(pad){
lineStyle(pad_border_w,pad_border_color,pad_border_alpha)
beginFill(pad_color,pad_alpha)
lineTo(pad_w,0)
lineTo(pad_w,pad_h)
lineTo(0,pad_h)
lineTo(0,0)
endFill()
_x = gamefield_w/2 - pad_w/2
_y = gamefield_h - (pad_h+10)
startDrag(this,false,_y,gamefield_w - pad_w,_y,0)
}
pad.onEnterFrame = function(){
ball._x = pad._x + pad_w/2
}
}
// This function will be called when the lives will be 0
function gameOver(){
createTextField('gover',getNextHighestDepth(),0,0,0,0,0)
gover.autoSize = true
gover.bold = true
gover.selectable = false
gover.text = "Sorry, game over." + newline + "Click for new game"
gover._x = gamefield_w/2-gover._width/2
gover._y = available_field/2 - gover._height/2
g = new Object()
g.onMouseDown = function(){
lives = ini_lives
liv.text = "Lives left: " + lives
currentlevel = 1
level(currentlevel)
newball()
delete this.onMouseDown
gover.removeTextField()
}
Mouse.addListener(g)
}
// This function will be called when all the brick
// in a level will be destroyed
function endLevel(){
createTextField('completed',getNextHighestDepth(),0,0,0,0,0)
completed.autoSize = true
completed.bold = true
completed.selectable = false
delete ball.onEnterFrame
if(currentlevel<total_levels){
completed.text = "Level " + currentlevel + " completed" + newline + "Click to start next level"
l = new Object()
l.onMouseDown = function(){
completed.removeTextField()
currentlevel++
level(currentlevel)
lev.text = "Level " + currentlevel
newball()
delete this.onMouseDown
Mouse.removeListener(l)
}
Mouse.addListener(l)
}else{
completed.text = "You won! Congratulations!"
}
completed._x = gamefield_w/2-completed._width/2
completed._y = available_field/2 - completed._height/2
}
// when the ball hit a brick, this function is called
// and check what bricks must be deleted
function removeBrick(who){
speed_y = -speed_y
points+= 100
score.text = "Score : " + points
who.res--
if(who.res == 0) {
who.removeMovieClip()
total_bricks--
if(total_bricks==0){
endLevel()
}
}
}
// This function create the ball
function createBall(){
radius = 4 // you can change this value for make the ball bigger or smaller, but is better leave it to 4
// Those lines create the ball movieclip
createEmptyMovieClip('ball',2)
with(ball){
lineStyle(0,0x000000,0)
beginFill(0x000000,100)
lineTo(0,radius)
curveTo(radius,radius, radius, 0)
curveTo(radius,-radius,0,-radius)
curveTo(-radius,-radius,-radius,0)
curveTo(-radius,radius,0,radius)
endFill()
// The ball is positioned over the pad
_x = pad._x + pad_w/2
_y = pad._y - _height/2
}
}
// We set a listener to the mouse so when the user
// click, the game begin
s = new Object()
s.onMouseDown = startgame
Mouse.addListener(s)
// This function let the game begin
function startgame(){
// We delete pad.onEnterFrame so the ball won't follow it
delete pad.onEnterFrame
// We remove the listener so the user can click without cause errors
Mouse.removeListener(s)
// Set speed_x and speed_y for the ball. You can change this values
speed_x = 8
speed_y = -7
// Set the ball actions. Don't modify this lines.
ball.onEnterFrame = function(){
// Move the ball
this._y = this._y + speed_y
this._x = this._x + speed_x
// If the ball it a border, bounce
this._x-this._width/2 <= bordersx._x || this._x+this._width>borderdx._x ? speed_x = -speed_x : null
this._y-this._height/2 < borderup._y ? speed_y = Math.abs(speed_y) : null
// If the ball fall, lose a live and start a newball
if(this._y > borderbottom._y) {
lives--
liv.text = "Lives left: " + lives
newball()
}
// Check what brick delete
colonna = int((this._x - brick_iniX) / brick_w)
riga = int((this._y - brick_iniY) / brick_h)
if(this._y < 300){
targetmc = eval("brick"+riga+"|"+colonna)
if(this._y>targetmc._y){
removeBrick(targetmc)
}
}
// Check if the ball hit the pad and where and make the ball bounce
if(pad.hitTest(this._x,this._y+(this._height/2)+3)){
if((this._x-pad._x<10 && this._x<pad._x+1) || (this._x - pad._x > pad._width/3&& this._x>pad._x+pad._width-5)){
speed_x = -speed_x
}
speed_y = -speed_y
}
}
}
// This function is called when the ball fasll
function newball(){
// Re-set the pad.onEnterFrame so the ball will follow it
// until the user will click
pad.onEnterFrame = function(){
ball._x = pad._x + pad_w/2
}
// Stop the ball
delete ball.onEnterFrame
// If the player have more than 0 lives, the game can continue
if(!lives == 0){
// Re-set the listener so when the player click the game begin
Mouse.addListener(s)
// Position the ball over the pad
ball._x = pad._x + pad_w/2
ball._y = pad._y - ball._height/2
}else{
// Otherwhise the game is over
gameOver()
}
}
// Load the info about a level
level_info = new XML()
level_info.ignoreWhite = true
level_info.onLoad = setLevelInfo
function level(n){
level_info.load('level'+n+'.xml')
}
// Put the info about the level into arrays
function setLevelInfo(){
bricks_type = []
brickinfos = this.firstChild.childNodes[0].childNodes
brick_w = Number(brickinfos[0].attributes.width)
brick_h = Number(brickinfos[0].attributes.height)
brick_iniY = Number(brickinfos[0].attributes.borderup)
brick_iniX = Number(brickinfos[0].attributes.bordersx)
trace(brick_w)
for(b=1;b<brickinfos.length;b++){
bricks_type[Number(brickinfos[b].attributes.id)] = new Array()
bricks_type[b]["res"] = brickinfos[b].attributes.res
bricks_type[b]["color"] = brickinfos[b].attributes.col
}
level_structure = this.firstChild.childNodes[1].childNodes
level_lines = new Array()
// Count how many lines of brick the level has
for(l=0;l<level_structure.length;l++){
// Put the structure of the line into an array
level_lines[l] = new Array()
level_lines[l] = level_structure[l].firstChild.toString().split("")
// Build the line
createLine(l)
}
}
// Set total_bricks to 0 (the level must be build yet
total_bricks = 0
// Create a line of bricks
function createLine(l){
xid = 0
for(cur=0;cur<level_lines[l].length;cur++){
if(level_lines[l][cur] != 0){
// Add one to the total_bricks var
total_bricks++
// Set the brick color
color = bricks_type[level_lines[l][cur]]["color"]
// Create and draw the brick
_root.createEmptyMovieClip('brick'+l+"|"+xid,getNextHighestDepth())
with(eval('brick'+l+"|"+xid)){
lineStyle(1,0x000000,100)
beginFill(color,100)
lineTo(brick_w,0)
lineTo(brick_w,brick_h)
lineTo(0,brick_h)
lineTo(0,0)
endFill()
// Position the brick
_x = brick_iniX + cur*20
_y = brick_iniY + l*brick_h
}
// Set the brick resistance
eval('brick'+l+"|"+xid).res = res = bricks_type[level_lines[l][cur]]["res"]
}
xid++
}
}
// Call the basic functions of the snippet
generateGameField()
createPad()
createBall()
level(currentlevel)
}
// USAGE
// Set the framerate to 30fps (or higher)
// Paste this code on the first frame
// Write brick() in the frame actions
// It's suggested to set the stage size
// to 300x200, but it's not obligatory
// EXAMPLE
brick()
Added by roScripts on March-26-2008, 5:14 pm
Added by roScripts on March-26-2008, 5:13 pm
Added by roScripts on March-26-2008, 5:09 pm
Added by roScripts on March-26-2008, 5:08 pm
Added by roScripts on March-26-2008, 5:07 pm