Skip to content

Commit 96cb3e6

Browse files
committed
game is fiished
1 parent 4d4d1b5 commit 96cb3e6

File tree

2 files changed

+69
-39
lines changed

2 files changed

+69
-39
lines changed

game.js

+63-33
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//Settings:
22
var backgroundColor = "#000000";
33
var elementColor = "#FFFFFF";
4-
var initialBallSpeed = 20; //How many px should we move per frame?
5-
var randomV = Math.random() * (Math.PI * 2);
4+
var initialBallSpeed = 10; //How many px should we move per frame?
5+
var randomV = Math.random() * (Math.PI/2) - Math.PI/4;
66

77
var initialBallK = Math.tan(randomV);
88

@@ -188,72 +188,102 @@ Platform.prototype.move = function(){
188188
}
189189
Platform.prototype.checkHitWithBall = function(ball){
190190
var x = ball.x;
191-
var to = x + ball.width;
192-
193191
var oldX = ball.oldX;
194-
var oldTo = oldX + ball.width;
195192

196-
var smallestX = Math.min(x, oldX);
197-
var largestX = Math.max(x,oldX);
193+
var line1 = this.x - ball.width;
194+
var line2 = this.x + this.width;
198195

199-
// Has it passed one of the lines?
200-
if(smallestX<)
196+
var smallest = Math.min(x, oldX);
197+
var largest = Math.max(x, oldX);
201198
}
202199

203200
var start = null;
204201
var ball = new Ball(10, 10, width/2 - 5, height/2-5, canvas.width, canvas.height);
205202

206-
var platform1 = new Platform(
203+
var platformR = new Platform(
207204
100, // Height
208205
10, // Width
209-
30, // StartX: 30px from right edge of canvas
210-
height/2-50, // Start in the middle on y-axis
206+
canvas.width - 40, // StartX: 30px from right edge of canvas
207+
height/2 - 50, // Start in the middle on y-axis
211208
canvas.height // Only let it move so that we don't go outside of canvas
212209
);
213210

214-
var platform2 = new Platform(
215-
30, // Height
211+
var platformL = new Platform(
212+
100, // Height
216213
10, // Width
217-
canvas.width - 40, // StartX: 30px (becomes 40px, because we need to count in the width of the platform) from left edge of canvas
218-
height/2-15, // Start in the middle on y-axis
214+
30, // StartX: 30px (becomes 40px, because we need to count in the width of the platform) from left edge of canvas
215+
height/2-50, // Start in the middle on y-axis
219216
canvas.height // Only let it move so that we don't go outside of canvas)
220217
);
221218

222-
var platforms = [platform1, platform2];
223-
platform1.setVelocity(0);
224-
platform2.setVelocity(0);
219+
var platforms = [platformR, platformL];
220+
platformR.setVelocity(0);
221+
platformL.setVelocity(0);
225222
var running = true;
226223
var step = function(timestamp){
227-
ball.draw(ctx);
228-
ball.move();
224+
for(var i = 0; i < platforms.length; i++){
225+
platforms[i].move();
226+
platforms[i].draw(ctx);
227+
}
229228

230-
// Move platforms and check if ball has hit platform
229+
var bouncyOnPlatform = function(platform,ball){
230+
var gradperl = Math.PI/2 / platform.height;
231+
232+
var l = -(platform.y + platform.height/ 2 - ball.centerY);
233+
234+
var grad = gradperl * l;
235+
236+
var k = Math.tan(grad);
237+
238+
var changeDir = 0;
239+
if(ball.dX > 0){
240+
changeDir = -1
241+
}
242+
else {
243+
changeDir = 1;
244+
}
245+
246+
ball.setK(k);
247+
ball.dX = ball.dX * changeDir;
248+
}
231249

232-
/*platform1.draw(ctx);
233-
platform1.move();
250+
// Check for hits with ball
251+
var rightBound = platformR.x;
252+
var leftBound = platformL.x + platformL.width;
234253

235-
platform2.draw(ctx);
236-
platform2.move();*/
237-
for(var i = 0; i < platforms.length; i++){
238-
platforms[i].draw(ctx);
239-
platforms[i].move()
254+
// Hit with left platform
255+
if(ball.x < leftBound && ball.dX < 0){
256+
// change direction
240257

241-
platforms[i].checkHitWithBall(ball);
258+
if(ball.y + ball.height > platformL.y && ball.y < platformL.y + platformL.height){
259+
bouncyOnPlatform(platformL, ball);
260+
}
242261
}
243262

244-
if(running) window.requestAnimationFrame(step);
263+
// Hit with right platform
264+
if((ball.x + ball.width) > rightBound && ball.dX > 0){
265+
if(ball.y + ball.height > platformR.y && ball.y < platformR.y + platformR.height){
266+
bouncyOnPlatform(platformR, ball);
267+
}
268+
}
269+
270+
271+
ball.move();
272+
ball.draw(ctx);
273+
245274

246275
//Very advanced system for detection of winner. Please be careful when you
247276
//uncomment this. It could lead to a broken toe, or worse, a
248277
//pinple on your elbow. Please uncomment with cuation.
249-
/*
278+
250279
if(ball.centerX <= 0){
251280
alert("alliansen vann!")
252281
running = false;
253282
}
254283
if(ball.centerX >= ball.boundX){
255284
alert("Lefty vann!");
256285
running = false;
257-
}*/
286+
}
287+
if(running) window.requestAnimationFrame(step);
258288
};
259289
window.requestAnimationFrame(step);

keymaster.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ var key = function(){
1212

1313
case 38:
1414
case 40:
15-
platform1.setVelocity(0);
15+
platformR.setVelocity(0);
1616
break;
1717

1818
//Spelare 2:
1919
case 87:
2020
case 83:
21-
platform2.setVelocity(0);
21+
platformL.setVelocity(0);
2222
break;
2323
}
2424

@@ -30,18 +30,18 @@ var key = function(){
3030
switch(e.keyCode){
3131
//Spelare 1
3232
case 38:
33-
platform1.setVelocity(-speedConstant);
33+
platformR.setVelocity(-speedConstant);
3434
break;
3535
case 40:
36-
platform1.setVelocity(speedConstant);
36+
platformR.setVelocity(speedConstant);
3737
break;
3838

3939
//Spelare 2:
4040
case 87:
41-
platform2.setVelocity(-speedConstant);
41+
platformL.setVelocity(-speedConstant);
4242
break;
4343
case 83:
44-
platform2.setVelocity(speedConstant);
44+
platformL.setVelocity(speedConstant);
4545
break;
4646
}
4747
console.log("down " + e.keyCode);

0 commit comments

Comments
 (0)