Advertisement
Kazikodi

Untitled

May 8th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.46 KB | None | 0 0
  1. <?php include('server.php');  
  2.  
  3. ?>
  4.  
  5. <!doctype html>
  6. <html lang="en">
  7. <head>
  8.     <meta charset="UTF-8">
  9.     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  10.  
  11.     <meta name="apple-mobile-web-app-capable" content="yes">
  12.     <meta name="mobile-web-app-capable" content="yes">
  13.  
  14.     <title>FlappyJS</title>
  15.  
  16.     <script src="sprite.js"></script>
  17.  
  18.     <style>
  19.         @import url(http://fonts.googleapis.com/css?family=Exo:100,200,400);
  20. @import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:700,400,300);
  21.        
  22.     canvas {
  23.         display: block;
  24.         position: absolute;
  25.         margin: auto;
  26.         top: 0;
  27.         bottom: 0;
  28.         left: 0;
  29.         right: 0;
  30.     }
  31.        
  32.        
  33.      
  34.     </style>
  35. </head>
  36. <body>
  37. <noscript>
  38. <p>For full functionality of this page it is necessary to enable JavaScript.
  39. Here are the <a href="http://www.enable-javascript.com/" target="_blank">
  40. instructions how to enable JavaScript in your web browser</a>.</p>
  41. </noscript>
  42. <script>
  43. var
  44. // Game vars //
  45. canvas,
  46. ctx,
  47. width,
  48. height,
  49. won,
  50. dau = 200,
  51. fgpos = 0,
  52. frames = 0,
  53. score = 0,
  54. best = localStorage.getItem("best") || 0,
  55. // State vars //
  56. currentstate,
  57. states = {
  58.       Splash: 0, Game: 1,  Score: 2, Loading: 3, Selection: 4
  59. },
  60. // Game objects //
  61. /**
  62.  * Ok button initiated in main()
  63.  */
  64. okbtn,
  65. karumebtn,
  66. jerobtn,
  67. bukubtn,
  68. /**
  69.  * The bird
  70.  */
  71. bird = {
  72.     x: 60,
  73.     y: 100,
  74.     frame: 0,
  75.     velocity: 0,
  76.     animation: [0, 1, 2, 1], // animation sequence
  77.     rotation: 0,
  78.     radius: 12,
  79.     gravity: 0.25,
  80.     _jump: 4.6,
  81.     /**
  82.      * Makes the bird "flap" and jump
  83.      */
  84.     jump: function() {
  85.         this.velocity = -this._jump;
  86.     },
  87.     /**
  88.      * Update sprite animation and position of bird
  89.      */
  90.     update: function() {
  91.         // make sure animation updates and plays faster in gamestate
  92.         var n = currentstate === states.Splash ? 10 : 5;
  93.         this.frame += frames % n === 0 ? 1 : 0;
  94.         this.frame %= this.animation.length;
  95.         // in splash state make bird hover up and down and set
  96.         // rotation to zero
  97.         if (currentstate === states.Splash  ) {
  98.             this.y = height - 280 + 5*Math.cos(frames/10);
  99.             this.rotation = 0;
  100.         } else { // game and score state //
  101.             this.velocity += this.gravity;
  102.             this.y += this.velocity;
  103.             // change to the score state when bird touches the ground
  104.             if (this.y >= height - s_fg.height+40) {
  105.                 this.y = height - s_fg.height+40;
  106.                 if (currentstate === states.Game) {
  107.                     currentstate = states.Score;
  108.                 }
  109.                 // sets velocity to jump speed for correct rotation
  110.                 this.velocity = this._jump;
  111.             }
  112.             // when bird lack upward momentum increment the rotation
  113.             // angle
  114.             if (this.velocity >= this._jump) {
  115.                 this.frame = 1;
  116.                 this.rotation = Math.min(Math.PI/2, this.rotation + 0.3);
  117.             } else {
  118.                 this.rotation = -0.3;
  119.             }
  120.         }
  121.     },
  122.     /**
  123.      * Draws bird with rotation to canvas ctx
  124.      *
  125.      * @param  {CanvasRenderingContext2D} ctx the context used for
  126.      *                                        drawing
  127.      */
  128.     draw: function(ctx) {
  129.         ctx.save();
  130.         // translate and rotate ctx coordinatesystem
  131.         ctx.translate(this.x, this.y);
  132.         ctx.rotate(this.rotation);
  133.        
  134.         var n = this.animation[this.frame];
  135.         // draws the bird with center in origo
  136.         s_bird[n].draw(ctx, -s_bird[n].width/2, -s_bird[n].height/2);
  137.         ctx.restore();
  138.     }
  139. },
  140. /**
  141.  * The pipes
  142.  */
  143. pipes = {
  144.     _pipes: [],
  145.     // padding: 80, // TODO: Implement paddle variable
  146.     /**
  147.      * Empty pipes array
  148.      */
  149.     reset: function() {
  150.         this._pipes = [];
  151.     },
  152.     /**
  153.      * Create, push and update all pipes in pipe array
  154.      */
  155.     update: function() {
  156.         // add new pipe each 100 frames
  157.         if (frames % 100 === 0) {
  158.             // calculate y position
  159.             var _y = height - (s_pipeSouth.height+s_fg.height+120+200*Math.random());
  160.             // create and push pipe to array
  161.             this._pipes.push({
  162.                 x: 500,
  163.                 y: _y,
  164.                 width: s_pipeSouth.width,
  165.                 height: s_pipeSouth.height
  166.             });
  167.         }
  168.         for (var i = 0, len = this._pipes.length; i < len; i++) {
  169.             var p = this._pipes[i];
  170.            
  171.            
  172.            
  173.             if (i === 0) {
  174.                
  175.                
  176.              
  177.                    
  178.                     //score += p.x === (bird.x) ? 1 : 0;
  179.                     score += p.x === (bird.x + 2.5) ? 1 : 0;
  180.                     score += p.x === (bird.x + 3.5) ? 1 : 0;
  181.                     score += p.x === (bird.x + 4.5) ? 1 : 0;
  182.                     score += p.x === (bird.x + 5.5) ? 1 : 0;
  183.                     //score += p.x === (bird.x + 8) ? 1 : 0;
  184.                     //score += p.x === (bird.x + 7.5) ? 1 : 0;
  185.                    
  186.                  
  187.                
  188.                 // collision check, calculates x/y difference and
  189.                 // use normal vector length calculation to determine
  190.                 // intersection
  191.                 var cx  = Math.min(Math.max(bird.x, p.x), p.x+p.width);
  192.                 var cy1 = Math.min(Math.max(bird.y, p.y), p.y+p.height);
  193.                 var cy2 = Math.min(Math.max(bird.y, p.y+p.height+300), p.y+2*p.height+300);
  194.                 // closest difference
  195.                 var dx  = bird.x - cx;
  196.                 var dy1 = bird.y - cy1;
  197.                 var dy2 = bird.y - cy2;
  198.                 // vector length
  199.                 var d1 = dx*dx + dy1*dy1;
  200.                 var d2 = dx*dx + dy2*dy2;
  201.                 var r = bird.radius*bird.radius;
  202.                 // determine intersection
  203.                 if (r > d1 || r > d2) {
  204.                     currentstate = states.Score;
  205.                 }
  206.             }
  207.             // move pipe and remove if outside of canvas
  208.             p.x -= 1;
  209.             if (p.x < -p.width) {
  210.                 this._pipes.splice(i, 1);
  211.                 i++;
  212.                 len--;
  213.             }
  214.            
  215.             if (score <= 4) {p.x -= 2.5;
  216.             }else if (score >=5 && score <=9) {p.x -= 3.5;
  217.             }if (score >=10 && score <=14) {p.x -= 4.5;
  218.             }else if (score >=15 && score <=19) {p.x -= 5.5;
  219.             }else if (score >= 20 && score <= 24) {p.x -=5.5;
  220.             }if (score >=25 && score < 30){p.x -= 5.5;
  221.             }else if (score === 30){p.x -= 8;
  222.            
  223.                 currentstate = states.Score;
  224.                
  225.             }
  226.                
  227.            
  228.                
  229.            
  230.          }
  231.     },
  232.     /**
  233.      * Draw all pipes to canvas context.
  234.      *
  235.      * @param  {CanvasRenderingContext2D} ctx the context used for
  236.      *                                        drawing
  237.      */
  238.     draw: function(ctx) {
  239.         for (var i = 0, len = this._pipes.length; i < len; i++) {
  240.             var p = this._pipes[i];
  241.             s_pipeSouth.draw(ctx, p.x, p.y);
  242.             s_pipeNorth.draw(ctx, p.x, p.y+100+p.height);
  243.         }
  244.     }
  245. };
  246. /**
  247.  * Called on mouse or touch press. Update and change state
  248.  * depending on current game state.
  249.  *
  250.  * @param  {MouseEvent/TouchEvent} evt tho on press event
  251.  */
  252. function onpress(evt) {
  253.     switch (currentstate) {
  254.        
  255.            
  256.            
  257.         //Splash
  258.         case states.Splash:
  259.            
  260.            /* if(balance >= 200){
  261.            
  262.             oldblc = balance;
  263.            
  264.             newblc = balance - 200;
  265.            
  266.             insert newblc to database;
  267.            
  268.                 if (oldblc === balance){
  269.                    
  270.                     alert("Transaction failed.")
  271.                    
  272.                                        
  273.                 }else {*/
  274.                     currentstate = states.Game;
  275.             bird.jump();
  276.             break;
  277.                     /*
  278.                 }
  279.            
  280.         }else {
  281.            
  282.             alert("You don't have sufficient Balance.")
  283.         }
  284.             */
  285.            
  286.             currentstate = states.Game;
  287.             bird.jump();
  288.             break;
  289.         // update bird velocity
  290.         case states.Game:
  291.             bird.jump();
  292.             break;
  293.         // change state if event within okbtn bounding box
  294.         case states.Score:
  295.             // get event position
  296.             var mx = evt.offsetX, my = evt.offsetY;
  297.             if (mx == null || my == null) {
  298.                 mx = evt.touches[0].clientX;
  299.                 my = evt.touches[0].clientY;
  300.             }
  301.             // check if within
  302.             if (okbtn.x < mx && mx < okbtn.x + okbtn.width &&
  303.                 okbtn.y < my && my < okbtn.y + okbtn.height
  304.             ) {
  305.                 pipes.reset();
  306.                 window.location=('twoselection.php');
  307.                 score = 0;
  308.             }
  309.            
  310.            
  311.        
  312.     }
  313. }
  314. /**
  315.  * Starts and initiate the game
  316.  */
  317. function main() {
  318.     // create canvas and set width/height
  319.     canvas = document.createElement("canvas");
  320.     width = window.innerWidth;
  321.     height = window.innerHeight;
  322.     var evt = "touchstart";
  323.     if (width >= 500) {
  324.         width  = 320;
  325.         height = 480;
  326.         canvas.style.border = "1px solid #000";
  327.         evt = "mousedown";
  328.     }
  329.     // listen for input event
  330.     document.addEventListener(evt, onpress);
  331.     canvas.width = width;
  332.     canvas.height = height;
  333.     if (!(!!canvas.getContext && canvas.getContext("2d"))) {
  334.         alert("Your browser doesn't support HTML5, please update to latest version");
  335.     }
  336.     ctx = canvas.getContext("2d");
  337.    
  338.    
  339.     currentstate = states.Splash;
  340.     // append canvas to document
  341.     document.body.appendChild(canvas);
  342.     // initate graphics and karumebtn
  343.     var imge = new Image();
  344.     imge.onload = function() {
  345.         initSprites(this);
  346.         ctx.fillStyle = s_bg.color;
  347.         karumebtn = {
  348.             x: (width - s_buttons.Karume.width)/2,
  349.             y: height/2 - 100,
  350.             width: s_buttons.Karume.width,
  351.             height: s_buttons.Karume.height
  352.         }
  353.     run();
  354.     }
  355.    
  356.     // initate graphics and jerobtn
  357.     var imgee = new Image();
  358.     imgee.onload = function() {
  359.         initSprites(this);
  360.         ctx.fillStyle = s_bg.color;
  361.         jerobtn = {
  362.             x: (width - s_buttons.Jero.width)/2,
  363.             y: height/2 - S-buttons.jero.width/2,
  364.             width: s_buttons.Jero.width,
  365.             height: s_buttons.Jero.height
  366.         }
  367.         run();
  368.     }
  369.        
  370.     // initate graphics and bukubtn
  371.     var imgeee = new Image();
  372.     imgeee.onload = function() {
  373.         initSprites(this);
  374.         ctx.fillStyle = s_bg.color;
  375.         bukubtn = {
  376.             x: (width - s_buttons.Buku.width)/2,
  377.             y: height/2 + 50,
  378.             width: s_buttons.Buku.width,
  379.             height: s_buttons.Buku.height
  380.         }
  381.     run();
  382.     }
  383.        
  384.        
  385.     // initate graphics and okbtn
  386.     var img = new Image();
  387.     img.onload = function() {
  388.         initSprites(this);
  389.         ctx.fillStyle = s_bg.color;
  390.         okbtn = {
  391.             x: (width - s_buttons.Ok.width)/2,
  392.             y: height - 200,
  393.             width: s_buttons.Ok.width,
  394.             height: s_buttons.Ok.height
  395.         }
  396.        
  397.        
  398.         run();
  399.     }
  400.     img.src = "res/sheet.png";
  401. }
  402. /**
  403.  * Starts and update gameloop
  404.  */
  405. function run() {
  406.     var loop = function() {
  407.         update();
  408.         render();
  409.         window.requestAnimationFrame(loop, canvas);
  410.     }
  411.     window.requestAnimationFrame(loop, canvas);
  412. }
  413. /**
  414.  * Update forground, bird and pipes position
  415.  */
  416. function update() {
  417.     frames++;
  418.     if (currentstate !== states.Score) {
  419.         fgpos = (fgpos - 2) % 14;
  420.        
  421.        
  422.        
  423.     } else {
  424.         // set best score to maximum score
  425.         best = Math.max(best, score);
  426.         localStorage.setItem("best", best);
  427.        
  428.        
  429.     }
  430.    
  431.    
  432.    
  433.     if (currentstate === states.Game) {
  434.         pipes.update();
  435.        
  436.        
  437.     }
  438.     bird.update();
  439. }
  440. /**
  441.  * Draws bird and all pipes and assets to the canvas
  442.  */
  443. function render() {
  444.     // draw background color
  445.     ctx.fillRect(0, 0, width, height);
  446.     // draw background sprites
  447.     s_bg.draw(ctx, 0, 0);
  448.     s_bg.draw(ctx, s_bg.width - 5, 0);
  449.     pipes.draw(ctx);
  450.     bird.draw(ctx);
  451.     // draw forground sprites
  452.     s_fg.draw(ctx, fgpos, 430);
  453.     s_fg.draw(ctx, fgpos+s_fg.width, 430);
  454.     var width2 = width/2; // center of canvas
  455.     if (currentstate === states.Splash) {
  456.         // draw splash text and sprite to canvas
  457.         s_splash.draw(ctx, width2 - s_splash.width/2, height - 300);
  458.         //s_text.GetReady.draw(ctx, width2 - s_text.GetReady.width/2, height-380);
  459.        
  460.        
  461.    
  462.    
  463.     }
  464.    
  465.     //if (currentstate === states.Selection) {
  466.         // draw splash text and sprite to canvas
  467.        
  468.        // s_buttons.Karume.draw(ctx, karumebtn.x, karumebtn.y);
  469.         //s_buttons.Jero.draw(ctx, jerobtn.x, jerobtn.y);
  470.         //s_buttons.Buku.draw(ctx, bukubtn.x, bukubtn.y);
  471.        
  472.        
  473.     //}
  474.     if (score === 30) {
  475.                    
  476.                     currentstate = states.Score;
  477.                 }
  478.     if (currentstate === states.Score) {
  479.         // draw gameover text and score board
  480.         s_text.GameOver.draw(ctx, width2 - s_text.GameOver.width/2, height-400);
  481.         s_score.draw(ctx, width2 - s_score.width/2, height-340);
  482.         s_buttons.Ok.draw(ctx, okbtn.x, okbtn.y);
  483.        
  484.        
  485.         //failed
  486.         if (score <= 19) {
  487.         s_scoredf.draw(ctx, width2 - s_scoredf.width/2 - 30, height-327);
  488.         }
  489.        
  490.        
  491.         //Vyangu
  492.         //score >= 20 && <= 24
  493.         if (score >= 20) {
  494.         s_scored.draw(ctx, width2 - s_scored.width/2 - 30, height-330);
  495.            
  496.                
  497.             //Display amount won
  498.             if (dau === 200) {
  499.                
  500.                 if (score >= 20 && score <= 24) {
  501.                     won =dau * 10;
  502.  
  503.                 }
  504.  
  505.  
  506.                     //score >= 25 && <= 29
  507.                 if (score >= 25 && score <= 29) {
  508.                     won =dau * 20;
  509.  
  510.                 }
  511.  
  512.                 if (score === 30) {
  513.                     won =dau * 30;
  514.                     currentstate = states.Score;
  515.                 }
  516.                
  517.                
  518.                
  519.                
  520.                
  521.                
  522.                
  523.                
  524.                
  525.                
  526.                
  527.                
  528.                
  529.                
  530.                
  531.                     if (won === 2000) {
  532.                        
  533.                        
  534.     </script>
  535. <?php
  536.                      
  537.  
  538. $link=mysqli_connect("localhost", "root", "....");
  539.  
  540.    
  541. if(!$link){
  542.    
  543.     die ("No connection: ".mysqli_connect_error());
  544.    
  545. } else{
  546.    
  547. mysqli_select_db($link, "db");
  548.  
  549.  
  550.  
  551.    
  552. $res=mysqli_query($link, "select balance from registrations where username='$_SESSION[username]'");
  553.  
  554.   //$Balance = $res;
  555.    
  556.    
  557. $count=mysqli_num_rows($res);
  558.    
  559.  
  560.        
  561.        
  562.             if($count === 1){
  563.                
  564.          //   insert newblc to database;
  565.                
  566.                
  567.       mysqli_query($link, "UPDATE registrations SET balance = balance + won WHERE username = '$_SESSION[username]'");
  568.                
  569.      
  570.             }else {
  571.          
  572.                  ?>
  573.        
  574.          
  575.         <script type="text/javascript">
  576.  
  577.            
  578.             alert("Something went wrong.");
  579.         </script>
  580.        
  581.                <?php
  582.                
  583.                
  584.             }
  585.        
  586.  
  587.  
  588. }
  589.  
  590.  
  591. ?>
  592. <script>
  593.      
  594.  
  595.  
  596.  
  597.                        
  598.                        
  599.                        
  600.                        
  601.                                
  602.                                 s_won1.draw(ctx, width2 - s_won1.width/2 - 30, height-278);
  603.                    
  604.                     }
  605.  
  606.                     if (won === 4000) {
  607.                      
  608.                                
  609.                                 s_wonn2.draw(ctx, width2 - s_wonn2.width/2 - 30, height-278);
  610.                            
  611.  
  612.                     }
  613.  
  614.                     if (won === 6000) {
  615.                        
  616.                    
  617.                                
  618.                                 s_wonnn3.draw(ctx, width2 - s_wonnn3.width/2 - 30, height-278);
  619.                             }
  620.                        
  621.                        
  622.                            
  623.                     }
  624.             }
  625.            
  626.         }
  627.        
  628.        
  629.        
  630.        
  631.        
  632.         // draw score and best inside the score board
  633.         s_numberS.draw(ctx, width2-47, height-304, score, null, 10);
  634.         s_numberS.draw(ctx, width2-47, height-262, best, null, 10);
  635.     } else {
  636.        
  637.        
  638.        
  639.        
  640.        
  641.        
  642.        
  643.        
  644.         // draw score to top of canvas
  645.         s_numberB.draw(ctx, null, 20, score, width2);
  646.     }
  647. }
  648. // start and run the game
  649. main();
  650.     </script>
  651.    
  652.    
  653.    
  654. </body>
  655. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement