Anonim / 1 year, 10 months ago | Download | Plaintext | Odpowiedz |

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<head>
<title>Cartman</title>
<style>
* {margin: 0; padding:0; font: 0.8em verdana}
body {text-align: center; padding-top: 20px;}
#game {border: 1px solid black;}
#all {width: 802px; margin: 20px auto;}
#coordinates {margin-bottom: 20px;}
</style>
</head>
<body>
<div id="all">
  <div id="coordinates"></div>
  <canvas id="game" width="800" height="500"></canvas>
</div>
<script type="text/javascript">

//--------------------------------------------
// REQUIRED VARIABLES
//--------------------------------------------

  var coordinates=document.getElementById('coordinates');
  var game = document.getElementById('game');
  var c=game.getContext('2d');
  
  //figure
  var widthFigure=50;
  var heightFigure=78;
  var x_figure=190;
  var y_figure=100;  
  var jump=0;
  var stopJump=0;
  
  //speed
  var speed=0;
  
  //Jump
  var heightJump=220;
  
  //land
  var oY=new Array();
  var oX=new Array(); 
  var widthBlock=150;
  var numberBlocksLand=50; 
  var numbersHeightArrayBlock=[450,400,350]; // array of blocks selected for the draw

  var p=0;
  var z=0; 
  for(p=0;p<numberBlocksLand;p++)
  {
    oY.push(numbersHeightArrayBlock[Math.floor(Math.random() * numbersHeightArrayBlock.length)]);
    oX.push(z);
    z+=widthBlock ; 
  }
    
  coordinates.innerHTML='x: '+x_figure+' | y: '+y_figure;


//--------------------------------------------
// FUNCTION ACTIVE
//--------------------------------------------
  
  function run(){
    c.clearRect(0,0,game.width,game.height); 
   
    c.fillStyle="green";
    for(p=0;p<numberBlocksLand;p++)
    {
      if(p==2 || p==13 || p==25 || p==21) 
      { 
        c.fillRect((oX[p]+speed),game.height,widthBlock,0);
        oY[p]=game.height; 
      }
      else 
      {
        c.fillRect((oX[p]+speed),oY[p],widthBlock,(game.height-oY[p]));
      }
    }
    
    // JUMP FIGURE 
    if(jump>0)
    {
      y_figure-=4;
      jump-=4;
    }
    else 
    {  
      for(p=0;p<numberBlocksLand;p++)
      {          
        if(x_figure>=(oX[p]+speed) && x_figure<=(oX[p]+widthBlock+speed))
        {
          if(y_figure<(oY[p]-heightFigure))
          {
            y_figure+=2;
            if(y_figure==(oY[p]-heightFigure)) stopJump=0;
          }
        }
      }
    }
    
    // FIGURE
    c.fillStyle="red";
    c.fillRect(x_figure,y_figure,widthFigure,heightFigure);     
        
    
    // HEADER COORDINATE
    coordinates.innerHTML='x/speed: '+x_figure+'/'+speed+' | y: '+(y_figure+heightFigure)+' | jump: '+jump+' | stopJump: '+stopJump;
  }
  
  setInterval('run()',1);

//--------------------------------------------
// FUNCTION KEY
//--------------------------------------------
  
  function KeyDown(e) 
  {
    switch(e.keyCode) 
    {
      case 37:
      speed+=7;
      break;
      
      case 39:
      speed-=7;   
      break;
    }
    
    if(e.keyCode==38) 
    {
      if(stopJump==0)
      {
        jump=heightJump;
        stopJump=1; // Blocking the action for the jump button until you settle
      }
    }
  }

//--------------------------------------------
// FUNCTION EVENT
//--------------------------------------------
  
  document.addEventListener("keydown", KeyDown, false);
  window.addEventListener("load", noMove, false);
 
</script>
</body>
</html>