Guest
Public paste!

Rohan Prabhu

By: a guest | Jul 1st, 2010 | Syntax: JavaScript | Size: 7.74 KB | Hits: 579 | Expires: Never
Copy text to clipboard
  1. /*
  2.  * KWin Pong
  3.  * -------------------------------
  4.  *         [rohan@rohanprabhu.com]
  5.  *         (c) Rohan Prabhu
  6.  * ---- R46F --- 37 --- R46F -----
  7.  */
  8.  
  9. function viewportHeight() {
  10.     return (workspace.dimensions().h)/(workspace.desktopGridSize().h);
  11. }
  12.    
  13. function viewportWidth() {
  14.     return (workspace.dimensions().w)/(workspace.desktopGridSize().w);
  15. }
  16.  
  17. function findCenter(canvas_size, rect_size) {
  18.     var ret = new Object();
  19.     ret.x = (canvas_size.w - rect_size.w)/2;
  20.     ret.y = (canvas_size.h - rect_size.h)/2;
  21.     return ret;
  22. }
  23.  
  24. var playerHandle;
  25. var ballHandle;
  26. var computerHandle;
  27.  
  28. var p_score = 0;
  29. var c_score = 0;
  30.  
  31. // Set this value a little higher if the game looks sluggish
  32. // all a little lower if the game looks choppy
  33. var timer_res = 30;
  34.  
  35. var stumble = 0;
  36.  
  37. var ft_iter = 0;
  38.  
  39. var firstTimer = new QTimer();
  40. var pollTimer = new QTimer();
  41.  
  42. function startGame() {
  43.     if(ft_iter == 0) {
  44.         firstTimer.timeout.connect(startGame);
  45.         firstTimer.start(1000);
  46.     }
  47.    
  48.     // A tribute to Warcraft
  49.     if(ft_iter < 3) {
  50.         print("Game starting in " + (3 - ft_iter) + " seconds ...");
  51.         ft_iter++;
  52.     }
  53.    
  54.     if(ft_iter == 3) {
  55.         firstTimer.timeout.disconnect(startGame);
  56.         print("GAME STARTING");
  57.        
  58.         pollTimer.timeout.connect(moveBall);
  59.         pollTimer.timeout.connect(computerJi);
  60.         pollTimer.start(timer_res);
  61.     }
  62. }
  63.  
  64. function computerJi() {
  65.     // For those who don't understand why this function
  66.     // is names so, try a hint
  67.     // 1. Be from India
  68.     // 2. A famous show hosted by Amitabh Bachchan
  69.    
  70.     // Sample algorithm taken from: http://tinyurl.com/396leup
  71.     // Thanks to [SoulRed12]. Used under Fair Use qualification
  72.    
  73.     var cLoc = computerHandle.pos();
  74.     var loc = ballHandle.pos();
  75.    
  76.     var dist = Math.abs(loc.y - cLoc.y);
  77.    
  78.     if((loc.y + (75/2)) > cLoc.y && dist > computerHandle.vel) {
  79.         computerHandle.move(cLoc.x, Math.min(cLoc.y + computerHandle.vel*(timer_res/1000), viewportHeight() - 400));
  80.     } else if((loc.y + (75/2)) < cLoc.y && dist > computerHandle.vel) {
  81.         computerHandle.move(cLoc.x, Math.min(cLoc.y - computerHandle.vel*(timer_res/1000), viewportHeight() - 400));
  82.     }
  83. }
  84.  
  85. function checkCollision() {
  86.     var loc = ballHandle.pos();
  87.     var pLoc = playerHandle.pos();
  88.     var cLoc = computerHandle.pos();
  89.    
  90.     // No code here for upper and lower bat
  91.     // boundaries. Not yet atleast
  92.    
  93.     if((loc.x <= (pLoc.x + 100)) && (loc.y > (pLoc.y - 75)) && (loc.y < (pLoc.y + 400))) {
  94.         return { col: 1, type: 0, bat: 1};
  95.     } else if((loc.x >= (cLoc.x - 75)) && (loc.y > (cLoc.y - 75)) && (loc.y < (cLoc.y + 400))) {
  96.         return { col: 1, type: 2, bat: 1};
  97.     }
  98.    
  99.     if(loc.x > (viewportWidth() - 75)) {
  100.         return { col: 1, type: 2, bat: -1};
  101.     } else if(loc.x <= 0) {
  102.         return { col: 1, type: 0, bat: -1};
  103.     } else if(loc.y > (viewportHeight() - 75)) {
  104.         return { col: 1, type: 3, bat: -1};
  105.     } else if(loc.y <= 0) {
  106.         return { col: 1, type: 1, bat: -1};
  107.     } else {
  108.         return { col: 0, type: -1, bat: -1};
  109.     }
  110. }
  111.  
  112. function reset_pong() {
  113.     playerHandle.resize(100, 400);
  114.     playerHandle.move(0, 0);
  115.     computerHandle.resize(100, 400);
  116.     computerHandle.move(viewportWidth() - 100, 0);
  117.     ballHandle.resize(75, 75);
  118.     ballHandle.move(findCenter({w: viewportWidth(), h: viewportHeight()}, {w: 150, h: 150}));
  119.     ballHandle.dx = -1;
  120.     ballHandle.dy = 1;
  121. }
  122.  
  123. function resume() {
  124.     pollTimer.start(timer_res);
  125. }
  126.  
  127. function score(who) {
  128.     if(who == 0) {
  129.         p_score++;
  130.     } else if(who == 1) {
  131.         c_score++;
  132.     }
  133.    
  134.     pollTimer.stop();
  135.     reset_pong();
  136.    
  137.     var message = "";
  138.    
  139.     if(who == 0) {
  140.         message += "######################\n";
  141.         message += "### PLAYER1 SCORED ###\n";
  142.         message += "######################\n";
  143.     } else {
  144.         message += "#######################\n";
  145.         message += "### COMPUTER SCORED ###\n";
  146.         message += "#######################\n";
  147.     }
  148.    
  149.     message += "SCORES [PL1 : " + p_score + " | CMP: " + c_score + " ... atari PONG";
  150.     print(message);
  151.    
  152.     firstTimer.timeout.connect(resume);
  153.     firstTimer.start(1000);
  154. }
  155.  
  156. function moveBall() {
  157.     var location = ballHandle.pos();
  158.     var _x = (ballHandle.dx)*(ballHandle.vel)*(timer_res/1000);
  159.     var _y = (ballHandle.dy)*(ballHandle.vel)*(timer_res/1000);
  160.    
  161.     ballHandle.move(location.x + _x, location.y + _y);
  162.     var coll = checkCollision();
  163.    
  164.     if(coll.col == 1) {
  165.         if(coll.type == 0) {
  166.             if(coll.bat == -1) {
  167.                 score(1);
  168.             }
  169.            
  170.             ballHandle.dx = 1;
  171.         } else if(coll.type == 1) {
  172.             ballHandle.dy = 1;
  173.         } else if(coll.type == 2) {
  174.             if(coll.bat == -1) {
  175.                 score(0);
  176.             }
  177.             ballHandle.dx = -1;
  178.         } else if(coll.type == 3) {
  179.             ballHandle.dy = -1;
  180.         }
  181.     }
  182. }
  183.  
  184. function constrainPlayer() {
  185.     var location = playerHandle.pos();
  186.     playerHandle.resize(100, 400);
  187.     playerHandle.move(0, Math.min(location.y, viewportHeight() - 400));
  188. }
  189.  
  190. function getPlayerFromFocus(client) {
  191.     if(stumble == 0) {
  192.         stumble = 1;
  193.         return;
  194.     }
  195.    
  196.     playerHandle = client;
  197.     playerHandle.resize(100, 400);
  198.     playerHandle.move(0, 0);
  199.     playerHandle.clientMoved.connect(constrainPlayer);
  200.    
  201.     print("<-----------------------------\\");
  202.     print(" > Player will be controlling [" + playerHandle.caption() + "]");
  203.     print("<-----------------------------/"); print("");
  204.     playerHandle.setCaption("PLAYER1");
  205.     workspace.clientActivated.disconnect(getPlayerFromFocus);
  206.     selectBall();
  207. }
  208.  
  209. function getBallFromFocus(client) {
  210.     ballHandle = client;
  211.     ballHandle.resize(75, 75);
  212.     ballHandle.move(findCenter({w: viewportWidth(), h: viewportHeight()}, {w: 150, h: 150}));
  213.    
  214.     // Initial kickoff direction
  215.     ballHandle.dx = -1;
  216.     ballHandle.dy = -1;
  217.     ballHandle.vel = 250; // speed in pixels per second
  218.    
  219.     print("<----------------------------\\");
  220.     print("The ball being used would be [" + ballHandle.caption() + "]");
  221.     print("<----------------------------/"); print("");
  222.    
  223.     ballHandle.setCaption("BALL");
  224.    
  225.     workspace.clientActivated.disconnect(getBallFromFocus);
  226.     selectComputerHandle();
  227. }
  228.  
  229. function getComputerFromFocus(client) {
  230.     computerHandle = client;
  231.     computerHandle.resize(100, 400);
  232.     computerHandle.move(viewportWidth() - 100, 0);
  233.     computerHandle.vel = 160;
  234.    
  235.     print("<-------------------------------\\");
  236.     print(" > Computer will be controlling [" + computerHandle.caption() + "]");
  237.     print("<-------------------------------/"); print("");
  238.     computerHandle.setCaption("COMPUTER");
  239.     workspace.clientActivated.disconnect(getComputerFromFocus);
  240.     selectScoreBoard();
  241. }
  242.  
  243. function getScoreBoardFromFocus(client) {
  244.     sbHandle = client;
  245.     sbHandle.resize(400, 150);
  246.     sbHandle.move(findCenter({w: viewportWidth(), h: viewportHeight()}, {w: 400, h: 150}).x, viewportHeight() - 150);
  247.     startGame();
  248. }
  249.  
  250. function selectScoreBoard() {
  251.     print("Please focus the client which houses the terminal for the current output");
  252.     workspace.clientActivated.connect(getScoreBoardFromFocus);
  253. }
  254.  
  255. function selectComputerHandle() {
  256.     print("Please focus the client you wish to use as the computer handle");
  257.     workspace.clientActivated.connect(getComputerFromFocus);
  258. }
  259.  
  260. function selectBall() {
  261.     print("Please focus the client you wish to use as the ball");
  262.     workspace.clientActivated.connect(getBallFromFocus);
  263. }
  264.  
  265. function selectPlayerHandle() {
  266.     print("Please focus the client you wish to use as a player handle");
  267.     workspace.clientActivated.connect(getPlayerFromFocus);
  268. }
  269.  
  270. if(startPong == 1) {
  271.     print("#################");
  272.     print("### KWIN PONG ###");
  273.     print("#################");
  274.     print("1 Player");
  275.     selectPlayerHandle();
  276. }