Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import Entities.Bricks;
- import Entities.Bullet;
- import Entities.Framework;
- import Entities.Racket;
- import Entities.Score;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.display.Graphics;
- import flash.events.FocusEvent;
- import flash.events.KeyboardEvent;
- import flash.ui.Keyboard;
- import Math;
- /**
- * ...
- * @author Jonas Jaszkowic
- */
- public class Main extends Sprite
- {
- //vars for entities
- private var bullet:Bullet;
- private var framework:Framework;
- private var racket:Racket;
- private var racket_speed:int;
- private var score:Score;
- private var left_down:Boolean;
- private var right_down:Boolean;
- private var bs:Array;
- public function Main():void
- {
- if (stage) init();
- else addEventListener(Event.ADDED_TO_STAGE, init);
- }
- private function init(e:Event = null):void
- {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- // entry point
- //add bullet to stage
- bullet = new Bullet(stage.stageWidth/2, stage.stageHeight/2, 6, 1, 5);
- addChild(bullet.b);
- //draw the framework
- framework = new Framework(stage.stageWidth, stage.stageHeight);
- addChild(framework.f);
- //add racket to stage
- racket = new Racket(stage.stageWidth / 2, stage.stageHeight);
- racket_speed = 8;
- addChild(racket.r);
- //Add score to stage
- score = new Score(0);
- addChild(score.TF_score);
- //add bricks to stage and create array containing the bricks
- var i:int; var m:int;
- bs = new Array;
- for (m = 20; m <= 120; m += 25)
- {
- for (i = 0; i < 12; i++)
- {
- var brick:Bricks;
- brick = new Bricks(i + 28 + (i * 70), m);
- addChild(brick.br);
- bs.push(brick);
- }
- }
- //add the EventListeners
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
- stage.addEventListener(KeyboardEvent.KEY_UP, keyUpListener);
- stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
- }
- public function keyDownListener(e:KeyboardEvent):void
- {
- if (e.keyCode == Keyboard.LEFT) left_down = true;
- if (e.keyCode == Keyboard.RIGHT) right_down = true;
- }
- public function keyUpListener(e:KeyboardEvent):void
- {
- if (e.keyCode == Keyboard.LEFT) left_down = false;
- if (e.keyCode == Keyboard.RIGHT) right_down = false;
- }
- public function onEnterFrame(e:Event):void
- {
- //move the bullet and check for collision with racket
- bullet.move_bullet(bullet, stage.stageWidth, stage.stageHeight, racket, right_down, left_down);
- //check for collision of bricks with bullet
- var x:int; var y:int = 60;
- for (x = 0; x < bs.length; x++)
- {
- if (bs[x].collision_sides(bullet, bs[x]))
- {
- bullet.speed_x *= -1;
- removeChild(bs[x].br);
- bs.splice(x, 1); //delete brick from array
- //update score
- score.points += 10;
- score.update_score();
- }
- if (bs[x].collision_topdown(bullet, bs[x]))
- {
- bullet.speed_y *= -1;
- removeChild(bs[x].br);
- bs.splice(x, 1); //delete brick from array
- //update score
- score.points += 10;
- score.update_score();
- }
- }
- //move the racket if key is down
- if (left_down)
- {
- racket.racket_move(racket, -racket_speed, stage.stageWidth);
- }
- if (right_down)
- {
- racket.racket_move(racket, racket_speed, stage.stageWidth);
- }
- racket_speed = Math.abs(bullet.speed_x) + Math.abs(bullet.speed_y) - Math.sqrt(Math.abs(bullet.speed_y)) +1;
- }
- }
- }
- package Entities
- {
- import flash.display.Sprite;
- /**
- * ...
- * @author Jonas Jaszkowic
- */
- public class Racket
- {
- public var x:int;
- public var r:Sprite;
- //construct the racket
- public function Racket(x:int, height:int)
- {
- r = new Sprite();
- r.graphics.beginFill(0x708090);
- r.graphics.drawRect(x, height-20, 60, 8);
- r.graphics.endFill();
- this.x = x;
- }
- public function racket_move(r:Racket, speed:int, width:int):void
- {
- if (!(r.x + speed >= width - 70 || r.x + speed <= 10))
- {
- r.r.x += speed
- r.x += speed;
- }
- }
- }
- }
- package Entities
- {
- import flash.display.Sprite;
- import flash.display.Graphics;
- /**
- * ...
- * @author Jonas Jaszkowic
- */
- public class Bullet
- {
- public var x:int;
- public var y:int;
- public var radius:int;
- public var speed_x:int;
- public var speed_y:int;
- public var b:Sprite;
- //constructor for the bullet
- public function Bullet(x:int, y:int, radius:int, speed_x:int, speed_y:int)
- {
- b = new Sprite();
- b.graphics.beginFill(0xff0000);
- b.graphics.drawCircle(x, y, radius);
- b.graphics.endFill();
- this.x = x;
- this.y = y;
- this.speed_x = speed_x;
- this.speed_y = speed_y;
- this.radius = radius;
- }
- //move the bullet and check for collisions
- public function move_bullet(b:Bullet, width:int, height:int, rk:Racket, left_down:Boolean, right_down:Boolean):void
- {
- b.b.x += b.speed_x;
- b.x += b.speed_x;
- b.b.y += b.speed_y;
- b.y += b.speed_y;
- if (b.x >= width-13 || b.x <= 13)
- {
- b.speed_x *= -1;
- }
- if (b.y >= height-13 || b.y <= 13)
- {
- b.speed_y *= -1;
- }
- //change speed of bullet when collision with moving racket
- if (b.collision_racket(b, rk, height))
- {
- b.speed_y *= -1;
- if (right_down) b.speed_x -= 3;
- if (left_down) b.speed_x += 3;
- }
- }
- //check for collision
- public function collision_racket(b:Bullet, r:Racket, height:int):Boolean
- {
- return (b.y >= height-23 && (b.x >= r.x && b.x <= r.x + 60));
- }
- }
- }
- package Entities
- {
- import flash.display.Sprite;
- import flash.display.Graphics;
- /**
- * ...
- * @author Jonas Jaszkowic
- */
- public class Framework
- {
- public var f:Sprite;
- //construct the black framework
- public function Framework(width:int, height:int)
- {
- f = new Sprite();
- f.graphics.beginFill(0x000000);
- f.graphics.drawRect(0, 0, 10, height);
- f.graphics.drawRect(width - 10, 0, 10, height);
- f.graphics.drawRect(10, 0, width - 20, 10);
- f.graphics.drawRect(10, height - 10, width - 20, 10);
- f.graphics.endFill();
- }
- }
- }
- package Entities
- {
- import flash.display.Sprite;
- import flash.display.Graphics;
- /**
- * ...
- * @author Jonas Jaszkowic
- */
- public class Bricks
- {
- public var x:int;
- public var y:int;
- public var br:Sprite;
- //constructor for one brick
- public function Bricks(x:int, y:int)
- {
- br = new Sprite();
- br.graphics.beginFill(0x00ff00);
- br.graphics.drawRect(x, y, 60, 20);
- br.graphics.endFill();
- this.x = x;
- this.y = y;
- }
- public function collision_sides(b:Bullet, br:Bricks):Boolean
- {
- return ((b.x == br.x || b.x == br.x+60) && (b.y >= br.y && b.y <= br.y +20));
- }
- public function collision_topdown(b:Bullet, br:Bricks):Boolean
- {
- return ((b.x > br.x && b.x < br.x +60)&&(b.y >= br.y && b.y <= br.y +20));
- }
- }
- }
- package Entities
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- /**
- * ...
- * @author Jonas Jaszkowic
- */
- public class Score
- {
- public var points:int;
- public var TF_score:TextField;
- public function Score(points:int)
- {
- this.points = points;
- TF_score = new TextField;
- TF_score.text = String(this.points);
- TF_score.x = 12;
- TF_score.y = 370;
- }
- public function update_score():void
- {
- TF_score.text = String(this.points);
- }
- }
- }
Add Comment
Please, Sign In to add comment