Advertisement
az4521

[Done?] UpnextFM PeerToPeer Pong

Mar 27th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //create canvas
  3. function pong_add(){
  4.     var canv = $("<canvas id='pong' width=640 height=480 style='width:50%'>get a better browser</canvas>");
  5.     $("#pong").remove();
  6.     $("#leftpane").append(canv);
  7. }
  8. function pong_remove(){$("#pong").remove();}
  9. //P2P setup stuff
  10. client = 0;possible_peer="";peer=""
  11. $chat.on("send",function(e,data){
  12.     if(data.msg.toLowerCase().indexOf("/pong ")===0){
  13.             e.cancel();
  14.         if (client===0){
  15.             possible_peer = data.msg.replace("/pong ","").trim();
  16.             $chat.command(possible_peer,"init_pong_p2p");
  17.             $chat.notice("Pong request sent");
  18.         }
  19.         else {
  20.             if(data.msg.toLowerCase()=="/pong accept"){
  21.                 $chat.command(possible_peer,"accept_pong_p2p");pong_add();animate_pong(update_pong);peer=possible_peer;
  22.             }
  23.             if(data.msg.toLowerCase()=="/pong decline"){
  24.                 $chat.command(possible_peer,"decline_pong_p2p");
  25.             }
  26.         }
  27.     }
  28. });
  29. $chat.on("command",function(e,data){
  30.     if(data.data == "init_pong_p2p"){
  31.         client=1;
  32.         $chat.notice("Incoming pong request");
  33.         $chat.notice("Accept with /pong accept");
  34.         $chat.notice("Decline with /pong decline");
  35.         possible_peer = data.from;
  36.     }
  37.     if(data.data=="accept_pong_p2p"){
  38.         $chat.notice("Pong request accepted")
  39.         $chat.notice("Starting game...")
  40.         peer = data.from;client=0;possible_peer="";pong_add();animate_pong(update_pong);
  41.     }
  42.     if(data.data=="decline_pong_p2p"){
  43.         $chat.notice("Pong request denied")
  44.         possible_peer="";client=0
  45.     }
  46. });
  47. //get animation crap
  48. animate_pong=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(callback){window.setTimeout(callback, 1000/60)};
  49. //init variables
  50. ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1y=250;p2y=250;p1s=0;p2s=0;keys_down=[0,0];
  51. function update_pong(){ //key controller
  52.     document.onkeydown=function(e){switch(e.keyCode){
  53.             case 87:
  54.                 keys_down[0]=1;break;
  55.             case 83:
  56.                 keys_down[1]=1;break;}};
  57.     document.onkeyup=function(e){switch(e.keyCode){
  58.             case 87:
  59.                 keys_down[0]=0;break;
  60.             case 83:
  61.                 keys_down[1]=0;break;}};
  62.     if(keys_down[0]==1){p1y-=4;};
  63.     if(keys_down[1]==1){p1y+=4;};
  64.     if (p1y<100){p1y=100;};
  65.     if (p1y>400){p1y=400;};
  66.     if(client){//client commands
  67.         if (keys_down[0]||keys_down[1]){
  68.             $chat.command(peer,"cy"+p1y);
  69.         }
  70.         $chat.on("command",function(e,data){
  71.             if(data.data.indexOf("sy")===0){
  72.                 p2y=parseInt(data.data.replace("sy",""));
  73.             }
  74.             else if(data.data.indexOf("bx")===0){
  75.                 ballx=640-parseInt(data.data.replace("bx",""));
  76.             }
  77.             else if(data.data.indexOf("by")===0){
  78.                 bally=parseInt(data.data.replace("by",""));
  79.             }
  80.             else if(data.data.indexOf("ss")===0){
  81.                 p1s=parseInt(data.data.replace("ss",""));
  82.             }
  83.             else if(data.data.indexOf("cs")===0){
  84.                 p2s=parseInt(data.data.replace("cs",""));
  85.             }
  86.         });
  87.     } else {
  88.         //server commands
  89.         if (bally>=470||bally<=110){ballyspeed*=-1;}
  90.         ballx+=ballxspeed
  91.         bally+=ballyspeed
  92.         if (ballx<50 && bally>=p1y && bally<=p1y+80 && ballxspeed<0){ballxspeed*=-1;ballyspeed=((bally-p1y)/8)-5}
  93.         if (ballx>590 && bally>=p2y && bally<=(p2y+80) && ballxspeed>0){ballxspeed*=-1;ballyspeed=((bally-p2y)/8)-5}
  94.         $chat.command(peer,"bx"+(ballx));$chat.command(peer,"by"+(bally));
  95.         if (!(ballxspeed||ballyspeed)){
  96.             ballxspeed = Math.random() < 0.5 ? 1*4 : -1*4;
  97.             ballyspeed = 1.6*(Math.floor(Math.random()*(3+3+1))-3 );
  98.             $chat.command(peer,"bx"+(ballx));$chat.command(peer,"by"+(bally));
  99.         }
  100.         if (ballx>650){ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1s+=1;$chat.command(peer,"cs"+p1s);}
  101.         if (ballx<-10){ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p2s+=1;$chat.command(peer,"ss"+p2s);}
  102.         if(keys_down[0]||keys_down[1]){
  103.             $chat.command(peer,"sy"+p1y);
  104.         }
  105.         $chat.on("command",function(e,data){
  106.             if(data.data.indexOf("cy")===0){
  107.                 p2y=parseInt(data.data.replace("cy",""));
  108.             }
  109.         });
  110.     }
  111.     draw_pong(ballx,bally,p1y,p2y,p1s,p2s);
  112.     animate_pong(update_pong);
  113.     if (p1s>10){$chat.notice("You Win");ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1y=250;p2y=250;p1s=0;p2s=0;keys_down=[0,0];pong_remove()}
  114.         if (p2s>10){$chat.notice("You Lose");ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1y=250;p2y=250;p1s=0;p2s=0;keys_down=[0,0];pong_remove()}
  115. };
  116. function draw_pong(b_x,b_y,p1_y,p2_y,p1_s,p2_s) {
  117.     canvas=document.getElementById("pong");
  118.     canvas.tabIndex = 1;
  119.     ctx=canvas.getContext("2d");
  120.     ctx.font="90px Arial";
  121.     ctx.fillStyle="black";
  122.     ctx.fillRect(0,0,640,480);
  123.     ctx.fillStyle="white";
  124.     ctx.fillRect(20,p1_y,20,80);
  125.     ctx.fillRect(600,p2_y,20,80);
  126.     ctx.fillRect(b_x-10,b_y-10,20,20);
  127.     ctx.fillRect(0,80,640,20);
  128.     ctx.fillText(" "+p1_s+"                "+p2_s,40,70);
  129.     for(i=0;i<5;i++){
  130.         ctx.fillRect(315,i*80+111,10,40);
  131.     };
  132. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement