pehlx94 on DeviantArthttps://www.deviantart.com/pehlx94/art/BreakOut-270644645pehlx94

Deviation Actions

pehlx94's avatar

BreakOut

By
Published:
259 Views

Description

flash actionscript 3.0 game : breakout .

yeah , i'm new at flash, i know the angle's wrong, etc,etc...
so if you know flash , tell me what went wrong .

code :
package {
import flash.display.*;
import flash.events.*;
import flash.ui.*;

public class Breakout extends MovieClip
{
private var score,life: Number;
private var player1Move: Number;
private var ballDirX, ballDirY: Number;
private var oldBallX, oldBallY: Number;

public function Breakout()
{

}


public function startGame()
{
addEventListener(Event.ENTER_FRAME,update);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
score = 0;
life = 3;
player1Move = 0;
ballDirX = 1 ;
ballDirY = -1 ;
oldBallX = mcBall.x ;
oldBallY = mcBall.y ;


}

private function keyDownHandler(evt:KeyboardEvent)
{
if (evt.keyCode == Keyboard.LEFT)
{
player1Move = -1;


}
if (evt.keyCode == Keyboard.RIGHT)
{
player1Move = 1;

}
}

private function keyUpHandler(evt:KeyboardEvent)
{
if (evt.keyCode == Keyboard.LEFT)
{
if (player1Move == -1)
player1Move = 0;
}
if (evt.keyCode == Keyboard.RIGHT)
{

if (player1Move == 1)
player1Move = 0;
}
}

public function update(evt:Event)
{

//This is the game loop

//Handle user input
if (player1Move == -1)
{
mcPlayer1.x -=10;

if (mcPlayer1.x <= 30)
{
mcPlayer1.x = 30
}
}

if (player1Move == 1)
{
mcPlayer1.x +=10;

if (mcPlayer1.x >= 520)
{
mcPlayer1.x = 520
}
}

//Handle game logic
oldBallX = mcBall.x ;
oldBallY = mcBall.y ;

mcBall.x += ballDirX * 3
mcBall.y += ballDirY * 3

//Check for collisions
//Top, Right and Left walls
if (mcBall.x <= 7)
{
ballDirX = 1;
}
if (mcBall.x >= 540)
{
ballDirX = -1;
}
if (mcBall.y <= 7)
{
ballDirY = 1;
}
else if (mcBall.y >= 400)
{
life -= 1;
resetGame();
}

//Player collisions
if (mcBall.hitTestObject(mcPlayer1))
{
ballDirY = -1
}

//Ball collision with the bricks
{
for (var i=1; i<=4; i++)
{
for (var j=1; j<=8; j++)
{
if (this["mcBrick"+i+j].visible)
{
if (mcBall.hitTestObject(this["mcBrick"+i+j]))
//bricky die :D
{
this["mcBrick"+i+j].visible = false;
score += 10 ;
if (oldBallX + mcBall.width/2 <this["mcBrick"+i+j].x - this["mcBrick"+i+j].width/2)
{
//hit left side ?
ballDirX *= -1;
}

else if (oldBallX - mcBall.width/2 >this["mcBrick"+i+j].x + this["mcBrick"+i+j].width/2)
{
//hit right side ?
ballDirX *= -1;
}

else if (oldBallY + mcBall.width/2 >this["mcBrick"+i+j].y - this["mcBrick"+i+j].height/2)
{
//hit top side ?
ballDirY *= -1;
}

else if (oldBallY - mcBall.width/2 <this["mcBrick"+i+j].y + this["mcBrick"+i+j].height/2)
{
//hit bottom side ?
ballDirY *= -1;
}
}
}


}
}
}


//to pawn ppl ;d
if (score >= 100 && score <200)
{
mcBall.x += ballDirX * 4
mcBall.y += ballDirY * 4
}
if (score >= 200 && score <250)
{
mcBall.x += ballDirX * 5
mcBall.y += ballDirY * 5
}
if (score >= 250 && score <300)
{
mcBall.x += ballDirX * 6
mcBall.y += ballDirY * 6
}
if (score >= 300 && score <350)
{
mcBall.x += ballDirX * 7
mcBall.y += ballDirY * 7
}
if (score >= 350 && score <400)
{
mcBall.x += ballDirX * 8
mcBall.y += ballDirY * 8
}
if (score >= 400 && score <410)
{
mcBall.x += ballDirX * 9
mcBall.y += ballDirY * 9
}
if (score >= 410 && score <420)
{
mcBall.x += ballDirX * 10
mcBall.y += ballDirY * 10
}
if (score >= 420 && score <430)
{
mcBall.x += ballDirX * 11
mcBall.y += ballDirY * 11
}
if (score >= 430 && score <440)
{
mcBall.x += ballDirX * 12
mcBall.y += ballDirY * 12
}
if (score >= 440 && score <450)
{
mcBall.x += ballDirX * 13
mcBall.y += ballDirY * 13
}
if (score >= 450)
{
mcBall.x += ballDirX * 15
mcBall.y += ballDirY * 15
}
//Check for Game Over
if (life == -1 )
{
gameOver();
}

//reset game after no brick
var shouldReset = true;
for (var i=1; i<=4; i++)
{
for (var j=1; j<=8; j++)
{
if (this["mcBrick"+i+j].visible)
{
shouldReset = false;
}
}
}
if (shouldReset)
{for (var i=1; i<=4; i++)
{
for (var j=1; j<=8; j++)
{
this["mcBrick"+i+j].visible = true ;
}
}
mcBall.x = 275;
mcBall.y = 350;
mcPlayer1.x = 275;
}




//Handle display
txtScore.text = String(score);
txtLife.text = String(life);
}

private function resetGame()
{
{
mcBall.x = 275;
mcBall.y = 350;
mcPlayer1.x = 275;
}
}

private function gameOver()
{
removeEventListener(Event.ENTER_FRAME,update);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);

}

}//end class
}//end package
Image size
550x400px 4.95 KB
© 2011 - 2024 pehlx94
Comments0
Join the community to add your comment. Already a deviant? Log In