Ajax Loader
HTML
<!DOCTYPE HTML>
1
<!DOCTYPE HTML>
2
<html lang="en">
3
<head>
4
  <meta charset="UTF-8">
5
  <title></title>
6
</head>
7
<body>
8
  <canvas id="c"></canvas>
9
</body>
10
</html>
 
CSS
body {margin:0px; padding:0px; text-align: center}
1
body {margin:0px; padding:0px; text-align: center}
2
canvas {outline:0; border:1px solid #000; margin: 0 auto}
 
JavaScript
var width = 320, 
1
var width = 320, 
2
  height = 500,
3
  gLoop,
4
  points = 0,
5
  state = true,
6
  c = document.getElementById('c'), 
7
  ctx = c.getContext('2d');
8
 
9
  c.width = width;
10
  c.height = height;
11
 
12
 
13
var clear = function(){
14
  ctx.fillStyle = '#d0e7f9';
15
  ctx.clearRect(0, 0, width, height);
16
  ctx.beginPath();
17
  ctx.rect(0, 0, width, height);
18
  ctx.closePath();
19
  ctx.fill();
20
}
21
 
22
var howManyCircles = 10, circles = [];
23
 
24
for (var i = 0; i < howManyCircles; i++) 
25
  circles.push([Math.random() * width, Math.random() * height, Math.random() * 100, Math.random() / 2]);
26
 
27
var DrawCircles = function(){
28
  for (var i = 0; i < howManyCircles; i++) {
29
    ctx.fillStyle = 'rgba(255, 255, 255, ' + circles[i][3] + ')';
30
    ctx.beginPath();
31
    ctx.arc(circles[i][0], circles[i][1], circles[i][2], 0, Math.PI * 2, true);
32
    ctx.closePath();
33
    ctx.fill();
34
  }
35
};
36
 
37
var MoveCircles = function(e){
38
  for (var i = 0; i < howManyCircles; i++) {
39
    if (circles[i][1] - circles[i][2] > height) {
40
      circles[i][0] = Math.random() * width;
41
      circles[i][2] = Math.random() * 100;
42
      circles[i][1] = 0 - circles[i][2];
43
      circles[i][3] = Math.random() / 2;
44
    }
45
    else {
46
      circles[i][1] += e;
47
    }
48
  }
49
};
50
 
51
var player = new (function(){
52
  var that = this;
53
  that.image = new Image();
54
 
55
  that.image.src = "http://images.virtualdesign.pl/images/99444maluszek.png"
56
  that.width = 65;
57
  that.height = 95;
58
  that.frames = 1;
59
  that.actualFrame = 0;
60
  that.X = 0;
61
  that.Y = 0; 
62
 
63
  that.isJumping = false;
64
  that.isFalling = false;
65
  that.jumpSpeed = 0;
66
  that.fallSpeed = 0;
67
 
68
    that.jump = function() {
69
    if (!that.isJumping && !that.isFalling) {
70
      that.fallSpeed = 0;
71
      that.isJumping = true;
72
      that.jumpSpeed = 17;
73
    }
74
  }
75
 
76
  that.checkJump = function() {
77
    //a lot of changes here
78
 
79
    if (that.Y > height*0.4) {
80
      that.setPosition(that.X, that.Y - that.jumpSpeed);    
81
    }
82
    else {
83
      if (that.jumpSpeed > 10) 
84
        points++;
85
      // if player is in mid of the gamescreen
86
      // dont move player up, move obstacles down instead
87
      MoveCircles(that.jumpSpeed * 0.5);
88
 
89
      platforms.forEach(function(platform, ind){
90
        platform.y += that.jumpSpeed;
91
 
92
        if (platform.y > height) {
93
          var type = ~~(Math.random() * 5);
94
          if (type == 0) 
95
            type = 1;
96
          else 
97
            type = 0;
98
 
99
          platforms[ind] = new Platform(Math.random() * (width - platformWidth), platform.y - height, type);
100
        }
101
      });
102
    }
103
 
104
 
105
    that.jumpSpeed--;
106
    if (that.jumpSpeed == 0) {
107
      that.isJumping = false;
108
      that.isFalling = true;
109
      that.fallSpeed = 1;
110
    }
111
 
112
  }
113
 
114
  that.fallStop = function(){
115
    that.isFalling = false;
116
    that.fallSpeed = 0;
117
    that.jump();  
118
  }
119
 
120
  that.checkFall = function(){
121
    if (that.Y < height - that.height) {
122
      that.setPosition(that.X, that.Y + that.fallSpeed);
123
      that.fallSpeed++;
124
    } else {
125
      if (points == 0) 
126
        that.fallStop();
127
      else 
128
        GameOver();
129
    }
130
  }
131
 
132
  that.moveLeft = function(){
133
    if (that.X > 0) {
134
      that.setPosition(that.X - 5, that.Y);
135
    }
136
  }
137
 
138
  that.moveRight = function(){
139
    if (that.X + that.width < width) {
140
      that.setPosition(that.X + 5, that.Y);
141
    }
142
  }
143
 
144
 
145
  that.setPosition = function(x, y){
146
    that.X = x;
147
    that.Y = y;
148
  }
149
 
150
  that.interval = 0;
151
  that.draw = function(){
152
    try {
153
      ctx.drawImage(that.image, 0, that.height * that.actualFrame, that.width, that.height, that.X, that.Y, that.width, that.height);
154
    } 
155
    catch (e) {
156
    };
157
 
158
    if (that.interval == 4 ) {
159
      if (that.actualFrame == that.frames) {
160
        that.actualFrame = 0;
161
      }
162
      else {
163
        that.actualFrame++;
164
      }
165
      that.interval = 0;
166
    }
167
    that.interval++;    
168
  }
169
})();
170
 
171
 
172
player.setPosition(~~((width-player.width)/2), height - player.height);
173
player.jump();
174
 
175
document.onmousemove = function(e){
176
  if (player.X + c.offsetLeft > e.pageX) {
177
    player.moveLeft();
178
  } else if (player.X + c.offsetLeft < e.pageX) {
179
    player.moveRight();
180
  }
181
 
182
}
183
  var nrOfPlatforms = 7, 
184
    platforms = [],
185
    platformWidth = 70,
186
    platformHeight = 20;
187
 
188
  var Platform = function(x, y, type){
189
    var that=this;
190
 
191
    that.firstColor = '#FF8C00';
192
    that.secondColor = '#EEEE00';
193
    that.onCollide = function(){
194
      player.fallStop();
195
    };
196
 
197
    if (type === 1) {
198
      that.firstColor = '#AADD00';
199
      that.secondColor = '#698B22';
200
      that.onCollide = function(){
201
        player.fallStop();
202
        player.jumpSpeed = 50;
203
      };
204
    }
205
 
206
 
207
 
208
    that.x = ~~ x;
209
    that.y = y;
210
    that.type = type;
211
 
212
    //NEW IN PART 5
213
    that.isMoving = ~~(Math.random() * 2);
214
    that.direction= ~~(Math.random() * 2) ? -1 : 1;
215
 
216
    that.draw = function(){
217
      ctx.fillStyle = 'rgba(255, 255, 255, 1)';
218
      var gradient = ctx.createRadialGradient(that.x + (platformWidth/2), that.y + (platformHeight/2), 5, that.x + (platformWidth/2), that.y + (platformHeight/2), 45);
219
      gradient.addColorStop(0, that.firstColor);
220
      gradient.addColorStop(1, that.secondColor);
221
      ctx.fillStyle = gradient;
222
      ctx.fillRect(that.x, that.y, platformWidth, platformHeight);
223
    };
224
 
225
    return that;
226
  };
227
 
228
  var generatePlatforms = function(){
229
    var position = 0, type;
230
    for (var i = 0; i < nrOfPlatforms; i++) {
231
      type = ~~(Math.random()*5);
232
      if (type == 0) 
233
        type = 1;
234
      else 
235
        type = 0;
236
      platforms[i] = new Platform(Math.random() * (width - platformWidth), position, type);
237
      if (position < height - platformHeight) 
238
        position += ~~(height / nrOfPlatforms);
239
    }
240
  }();
241
 
242
  var checkCollision = function(){
243
  platforms.forEach(function(e, ind){
244
    if (
245
    (player.isFalling) && 
246
    (player.X < e.x + platformWidth) && 
247
    (player.X + player.width > e.x) && 
248
    (player.Y + player.height > e.y) && 
249
    (player.Y + player.height < e.y + platformHeight)
250
    ) {
251
      e.onCollide();
252
    }
253
  })
254
  }
255
 
256
var GameLoop = function(){
257
  clear();
258
  //MoveCircles(5);
259
  DrawCircles();
260
 
261
  if (player.isJumping) player.checkJump();
262
  if (player.isFalling) player.checkFall();
263
 
264
  player.draw();
265
 
266
  platforms.forEach(function(platform, index){
267
    if (platform.isMoving) {
268
      if (platform.x < 0) {
269
        platform.direction = 1;
270
      } else if (platform.x > width - platformWidth) {
271
        platform.direction = -1;
272
      }
273
        platform.x += platform.direction * (index / 2) * ~~(points / 100);
274
      }
275
    platform.draw();
276
  });
277
 
278
  checkCollision();
279
 
280
  ctx.fillStyle = "Black";
281
  ctx.fillText("POINTS:" + points, 10, height-10);
282
 
283
  if (state)
284
    gLoop = setTimeout(GameLoop, 1000 / 50);
285
}
286
 
287
  var GameOver = function(){
288
    state = false;
289
    clearTimeout(gLoop);
290
    setTimeout(function(){
291
      clear();
292
 
293
      ctx.fillStyle = "Black";
294
      ctx.font = "10pt Arial";
295
      ctx.fillText("GAME OVER", width / 2 - 60, height / 2 - 50);
296
      ctx.fillText("YOUR RESULT:" + points, width / 2 - 60, height / 2 - 30);
297
    }, 100);
298
 
299
  };
300
 
301
GameLoop();
 

Stairs to heaven

CSSDeck G+