Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.86 KB | None | 0 0
  1. <canvas width="500" height="500" id="game"></canvas>
  2. <script>
  3. var canvas=document.getElementById('game')
  4. var ctx=canvas.getContext('2d')
  5. var movingRight=false;
  6. var movingLeft=false;
  7. var startTime="null"
  8. var tough=0;
  9. var jumping=false;
  10. you = {
  11.         x: 240,
  12.         y: 480,
  13.         width: 20,
  14.         height: 20,
  15.         maxSpeed: 8,
  16.         speed:7,
  17.         gravity:7,
  18.         velX: 0,
  19.         velY: 0,
  20.         health: 100,
  21.         jumping: false,
  22.         doubleJump: true,
  23.         canDoubleJump:false,
  24.         invulnerable: false,
  25.         invulnerableTimer:50,
  26.         image: new Image(),
  27.         imageReverse: new Image()
  28.     };
  29. you.image.src = "https://demogorgon22.files.wordpress.com/2015/06/little-guy-move-left-transflipped.gif";
  30. setInterval(function(){
  31. tough++
  32. //ctx.clearRect(0, 0, canvas.width, canvas.height);
  33. if(movingRight){
  34. you.x+=you.speed
  35. }
  36. if(movingLeft){
  37. you.x-=you.speed
  38. }
  39. if(jumping){
  40. if(startTime=="null"){
  41. startTime=tough
  42. }
  43. if(startTime+20!=tough){
  44. you.y-=10
  45. } else {
  46. jumping=false;
  47. startTime="null"
  48. }
  49. }
  50. you.y+=you.gravity
  51. if(you.x>480){
  52. you.x=480
  53. }
  54. if(you.x<0){
  55. you.x=0
  56. }
  57. if(you.y>480){
  58. you.y=480
  59. }
  60. if(you.y<0){
  61. you.y=0
  62. }
  63. render()
  64. },17);
  65.  
  66.  
  67.  
  68.  
  69. window.onload=function(){
  70. render()
  71. }
  72.  
  73. window.onkeyup = function (e){
  74. var code = e.keyCode ? e.keyCode : e.which;
  75. //alert(unicode + "up")
  76.  
  77.    if (code === 65) { //a key
  78.        movingLeft=false;
  79.    } else if (code === 68) { //d key
  80.     movingRight=false;
  81.    }
  82. }
  83.  
  84.  
  85. window.onkeydown = function (e) {
  86.     code = e.keyCode ? e.keyCode : e.which;
  87.    if (code === 87) { //w key
  88.     if(you.y==480){
  89.        jumping=true
  90.     }
  91.    } else if (code === 65) { //a key
  92.        movingLeft=true;
  93.    } else if (code === 68) { //d key
  94.     movingRight=true;
  95.    } else if(code === 27){//esc key
  96.     clearInterval(main)
  97.    }
  98. }
  99. function render(){
  100.  
  101. ctx.fillStyle="grey"
  102. //ctx.fillRect(0,0,canvas.width, canvas.height)
  103. ctx.drawImage(you.image, you.x, you.y);
  104. }
  105. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement