local pong done, start multiplayer implementation
This commit is contained in:
60
src/client/gameLoop.ts
Normal file
60
src/client/gameLoop.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import * as g from "./pong.js"
|
||||
import * as d from "./draw.js";
|
||||
import {random} from "./utils.js";
|
||||
import {handleInput} from "./handleInput.js";
|
||||
|
||||
let ballInPlay = true;
|
||||
|
||||
function gameLoop()
|
||||
{
|
||||
/*
|
||||
// I try to clear only what need to be update.
|
||||
// Will revert to clear() all if not satisfactory.
|
||||
pong.clear();
|
||||
*/
|
||||
handleInput();
|
||||
|
||||
if (ballInPlay)
|
||||
{
|
||||
g.ball.moveAndBounce([g.wall_top, g.wall_bottom, g.player1, g.player2]);
|
||||
if (g.ball.pos.x > g.pong.canvas.width) {
|
||||
ballInPlay = false;
|
||||
g.score1.clear();
|
||||
++g.score1.value;
|
||||
setTimeout(newRound, 1500);
|
||||
}
|
||||
else if (g.ball.pos.x < 0 - g.ball.width) {
|
||||
ballInPlay = false;
|
||||
g.score2.clear();
|
||||
++g.score2.value;
|
||||
setTimeout(newRound, 1500);
|
||||
}
|
||||
}
|
||||
|
||||
d.draw();
|
||||
}
|
||||
|
||||
function newRound()
|
||||
{
|
||||
// https://fr.wikipedia.org/wiki/Tennis_de_table#Nombre_de_manches
|
||||
if (g.score1.value >= 11
|
||||
|| g.score2.value >= 11)
|
||||
{
|
||||
if (Math.abs(g.score1.value - g.score2.value) >= 2)
|
||||
{
|
||||
if (g.score1.value > g.score2.value) {
|
||||
alert("Player 1 WIN");
|
||||
}
|
||||
else {
|
||||
alert("Player 2 WIN");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
g.ball.pos.x = g.pong.canvas.width/2;
|
||||
g.ball.pos.y = (g.pong.canvas.height * 0.1) + Math.floor(random() * (g.pong.canvas.height * 0.8));
|
||||
g.ball.speed = g.ball.baseSpeed;
|
||||
ballInPlay = true;
|
||||
}
|
||||
|
||||
export {gameLoop}
|
||||
Reference in New Issue
Block a user