1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
package {
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
import flash.geom.Rectangle;
import flash.text.TextField;
public class PaddleBall extends MovieClip {
// environment constants
private const ballRadius:Number = 9;
private const wallTop:Number = 18;
private const wallLeft:Number = 18;
private const wallRight:Number = 532;
private const paddleY:Number = 380;
private const paddleWidth:Number = 90;
private const ballSpeed:Number = .2;
private const paddleCurve:Number = .005;
private const paddleHeight:Number = 18;
// key objects
private var paddle:Paddle;
private var ball:Ball;
// bricks
private var bricks:Array;
// ball velocity
private var ballDX:Number;
private var ballDY:Number;
// animation timer
private var lastTime:uint;
// number of balls left
private var balls:Number;
public function startPaddleBall() {
// create paddle
paddle = new Paddle();
paddle.y = paddleY;
addChild(paddle);
// create bricks
makeBricks();
balls = 3;
gameMessage.text = "Click To Start";
// set up animation
lastTime = 0;
addEventListener(Event.ENTER_FRAME,moveObjects);
stage.addEventListener(MouseEvent.CLICK,newBall);
}
// make collection of bricks
public function makeBricks() {
bricks = new Array();
// create a grid of bricks, 5 vertical by 8 horizontal
for(var y:uint=0;y<5;y++) {
for(var x:uint=0;x<8;x++) {
var newBrick:Brick = new Brick();
// space them nicely
newBrick.x = 60*x+65;
newBrick.y = 20*y+50;
addChild(newBrick);
bricks.push(newBrick);
}
}
}
public function newBall(event:Event) {
// don't go here if there is already a ball
if (ball != null) return;
gameMessage.text = "";
// create ball, center it
ball = new Ball();
ball.x = (wallRight-wallLeft)/2+wallLeft;
ball.y = 200;//(paddleY-wallTop)/2+wallTop;
addChild(ball);
// ball velocity
ballDX = 0;
ballDY = ballSpeed;
// use up a ball
balls--;
ballsLeft.text = "Balls: "+balls;
// reset animation
lastTime = 0;
}
public function moveObjects(event:Event) {
movePaddle();
moveBall();
}
public function movePaddle() {
// match horizontal value with the mouse
var newX:Number = Math.min(wallRight-paddleWidth/2,
Math.max(wallLeft+paddleWidth/2,
mouseX));
paddle.x = newX;
}
public function moveBall() {
// don't go here if in between balls
if (ball == null) return;
// get new location for ball
if (lastTime == 0) lastTime = getTimer();
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
var newBallX = ball.x + ballDX*timePassed;
var newBallY = ball.y + ballDY*timePassed;
var oldBallRect = new Rectangle(ball.x-ballRadius, ball.y-ballRadius, ballRadius*2, ballRadius*2);
var newBallRect = new Rectangle(newBallX-ballRadius, newBallY-ballRadius, ballRadius*2, ballRadius*2);
var paddleRect = new Rectangle(paddle.x-paddleWidth/2, paddle.y-paddleHeight/2, paddleWidth, paddleHeight);
// collision with paddle
if (newBallRect.bottom >= paddleRect.top) {
if (oldBallRect.bottom < paddleRect.top) {
if (newBallRect.right > paddleRect.left) {
if (newBallRect.left < paddleRect.right) {
// bounce back
newBallY -= 2*(newBallRect.bottom - paddleRect.top);
ballDY *= -1;
// decide new angle
ballDX = (newBallX-paddle.x)*paddleCurve;
}
}
} else if (newBallRect.top > 400) {
removeChild(ball);
ball = null;
if (balls > 0) {
gameMessage.text = "Click For Next Ball";
} else {
endGame();
}
return;
}
}
// collision with top wall
if (newBallRect.top < wallTop) {
newBallY += 2*(wallTop - newBallRect.top);
ballDY *= -1;
}
// collision with left wall
if (newBallRect.left < wallLeft) {
newBallX += 2*(wallLeft - newBallRect.left);
ballDX *= -1;
}
// collision with right wall
if (newBallRect.right > wallRight) {
newBallX += 2*(wallRight - newBallRect.right);
ballDX *= -1;
}
// collision with bricks
for(var i:int=bricks.length-1;i>=0;i--) {
// get brick rectangle
var brickRect:Rectangle = bricks[i].getRect(this);
// is there a brick collision
if (brickRect.intersects(newBallRect)) {
// ball hitting left or right side
if (oldBallRect.right < brickRect.left) {
newBallX += 2*(brickRect.left - oldBallRect.right);
ballDX *= -1;
} else if (oldBallRect.left > brickRect.right) {
newBallX += 2*(brickRect.right - oldBallRect.left);
ballDX *= -1;
}
// ball hitting top or bottom
if (oldBallRect.top > brickRect.bottom) {
ballDY *= -1;
newBallY += 2*(brickRect.bottom-newBallRect.top);
} else if (oldBallRect.bottom < brickRect.top) {
ballDY *= -1;
newBallY += 2*(brickRect.top - newBallRect.bottom);
}
// remove the brick
removeChild(bricks[i]);
bricks.splice(i,1);
if (bricks.length < 1) {
endGame();
return;
}
}
}
// set new position
ball.x = newBallX;
ball.y = newBallY;
}
function endGame() {
// remove paddle and bricks
removeChild(paddle);
for(var i:int=bricks.length-1;i>=0;i--) {
removeChild(bricks[i]);
}
paddle = null;
bricks = null;
// remove ball
if (ball != null) {
removeChild(ball);
ball = null;
}
// remove listeners
removeEventListener(Event.ENTER_FRAME,moveObjects);
stage.removeEventListener(MouseEvent.CLICK,newBall);
gotoAndStop("gameover");
}
}
}
|