/*
* KWin Pong
* -------------------------------
* [rohan@rohanprabhu.com]
* (c) Rohan Prabhu
* ---- R46F --- 37 --- R46F -----
*/
function viewportHeight() {
return (workspace.dimensions().h)/(workspace.desktopGridSize().h);
}
function viewportWidth() {
return (workspace.dimensions().w)/(workspace.desktopGridSize().w);
}
function findCenter(canvas_size, rect_size) {
var ret = new Object();
ret.x = (canvas_size.w - rect_size.w)/2;
ret.y = (canvas_size.h - rect_size.h)/2;
return ret;
}
var playerHandle;
var ballHandle;
var computerHandle;
var p_score = 0;
var c_score = 0;
// Set this value a little higher if the game looks sluggish
// all a little lower if the game looks choppy
var timer_res = 30;
var stumble = 0;
var ft_iter = 0;
var firstTimer = new QTimer();
var pollTimer = new QTimer();
function startGame() {
if(ft_iter == 0) {
firstTimer.timeout.connect(startGame);
firstTimer.start(1000);
}
// A tribute to Warcraft
if(ft_iter < 3) {
print("Game starting in " + (3 - ft_iter) + " seconds ...");
ft_iter++;
}
if(ft_iter == 3) {
firstTimer.timeout.disconnect(startGame);
print("GAME STARTING");
pollTimer.timeout.connect(moveBall);
pollTimer.timeout.connect(computerJi);
pollTimer.start(timer_res);
}
}
function computerJi() {
// For those who don't understand why this function
// is names so, try a hint
// 1. Be from India
// 2. A famous show hosted by Amitabh Bachchan
// Sample algorithm taken from: http://tinyurl.com/396leup
// Thanks to [SoulRed12]. Used under Fair Use qualification
var cLoc = computerHandle.pos();
var loc = ballHandle.pos();
var dist = Math.abs(loc.y - cLoc.y);
if((loc.y + (75/2)) > cLoc.y && dist > computerHandle.vel) {
computerHandle.move(cLoc.x, Math.min(cLoc.y + computerHandle.vel*(timer_res/1000), viewportHeight() - 400));
} else if((loc.y + (75/2)) < cLoc.y && dist > computerHandle.vel) {
computerHandle.move(cLoc.x, Math.min(cLoc.y - computerHandle.vel*(timer_res/1000), viewportHeight() - 400));
}
}
function checkCollision() {
var loc = ballHandle.pos();
var pLoc = playerHandle.pos();
var cLoc = computerHandle.pos();
// No code here for upper and lower bat
// boundaries. Not yet atleast
if((loc.x <= (pLoc.x + 100)) && (loc.y > (pLoc.y - 75)) && (loc.y < (pLoc.y + 400))) {
return { col: 1, type: 0, bat: 1};
} else if((loc.x >= (cLoc.x - 75)) && (loc.y > (cLoc.y - 75)) && (loc.y < (cLoc.y + 400))) {
return { col: 1, type: 2, bat: 1};
}
if(loc.x > (viewportWidth() - 75)) {
return { col: 1, type: 2, bat: -1};
} else if(loc.x <= 0) {
return { col: 1, type: 0, bat: -1};
} else if(loc.y > (viewportHeight() - 75)) {
return { col: 1, type: 3, bat: -1};
} else if(loc.y <= 0) {
return { col: 1, type: 1, bat: -1};
} else {
return { col: 0, type: -1, bat: -1};
}
}
function reset_pong() {
playerHandle.resize(100, 400);
playerHandle.move(0, 0);
computerHandle.resize(100, 400);
computerHandle.move(viewportWidth() - 100, 0);
ballHandle.resize(75, 75);
ballHandle.move(findCenter({w: viewportWidth(), h: viewportHeight()}, {w: 150, h: 150}));
ballHandle.dx = -1;
ballHandle.dy = 1;
}
function resume() {
pollTimer.start(timer_res);
}
function score(who) {
if(who == 0) {
p_score++;
} else if(who == 1) {
c_score++;
}
pollTimer.stop();
reset_pong();
var message = "";
if(who == 0) {
message += "######################\n";
message += "### PLAYER1 SCORED ###\n";
message += "######################\n";
} else {
message += "#######################\n";
message += "### COMPUTER SCORED ###\n";
message += "#######################\n";
}
message += "SCORES [PL1 : " + p_score + " | CMP: " + c_score + " ... atari PONG";
print(message);
firstTimer.timeout.connect(resume);
firstTimer.start(1000);
}
function moveBall() {
var location = ballHandle.pos();
var _x = (ballHandle.dx)*(ballHandle.vel)*(timer_res/1000);
var _y = (ballHandle.dy)*(ballHandle.vel)*(timer_res/1000);
ballHandle.move(location.x + _x, location.y + _y);
var coll = checkCollision();
if(coll.col == 1) {
if(coll.type == 0) {
if(coll.bat == -1) {
score(1);
}
ballHandle.dx = 1;
} else if(coll.type == 1) {
ballHandle.dy = 1;
} else if(coll.type == 2) {
if(coll.bat == -1) {
score(0);
}
ballHandle.dx = -1;
} else if(coll.type == 3) {
ballHandle.dy = -1;
}
}
}
function constrainPlayer() {
var location = playerHandle.pos();
playerHandle.resize(100, 400);
playerHandle.move(0, Math.min(location.y, viewportHeight() - 400));
}
function getPlayerFromFocus(client) {
if(stumble == 0) {
stumble = 1;
return;
}
playerHandle = client;
playerHandle.resize(100, 400);
playerHandle.move(0, 0);
playerHandle.clientMoved.connect(constrainPlayer);
print("<-----------------------------\\");
print(" > Player will be controlling [" + playerHandle.caption() + "]");
print("<-----------------------------/"); print("");
playerHandle.setCaption("PLAYER1");
workspace.clientActivated.disconnect(getPlayerFromFocus);
selectBall();
}
function getBallFromFocus(client) {
ballHandle = client;
ballHandle.resize(75, 75);
ballHandle.move(findCenter({w: viewportWidth(), h: viewportHeight()}, {w: 150, h: 150}));
// Initial kickoff direction
ballHandle.dx = -1;
ballHandle.dy = -1;
ballHandle.vel = 250; // speed in pixels per second
print("<----------------------------\\");
print("The ball being used would be [" + ballHandle.caption() + "]");
print("<----------------------------/"); print("");
ballHandle.setCaption("BALL");
workspace.clientActivated.disconnect(getBallFromFocus);
selectComputerHandle();
}
function getComputerFromFocus(client) {
computerHandle = client;
computerHandle.resize(100, 400);
computerHandle.move(viewportWidth() - 100, 0);
computerHandle.vel = 160;
print("<-------------------------------\\");
print(" > Computer will be controlling [" + computerHandle.caption() + "]");
print("<-------------------------------/"); print("");
computerHandle.setCaption("COMPUTER");
workspace.clientActivated.disconnect(getComputerFromFocus);
selectScoreBoard();
}
function getScoreBoardFromFocus(client) {
sbHandle = client;
sbHandle.resize(400, 150);
sbHandle.move(findCenter({w: viewportWidth(), h: viewportHeight()}, {w: 400, h: 150}).x, viewportHeight() - 150);
startGame();
}
function selectScoreBoard() {
print("Please focus the client which houses the terminal for the current output");
workspace.clientActivated.connect(getScoreBoardFromFocus);
}
function selectComputerHandle() {
print("Please focus the client you wish to use as the computer handle");
workspace.clientActivated.connect(getComputerFromFocus);
}
function selectBall() {
print("Please focus the client you wish to use as the ball");
workspace.clientActivated.connect(getBallFromFocus);
}
function selectPlayerHandle() {
print("Please focus the client you wish to use as a player handle");
workspace.clientActivated.connect(getPlayerFromFocus);
}
if(startPong == 1) {
print("#################");
print("### KWIN PONG ###");
print("#################");
print("1 Player");
selectPlayerHandle();
}