Advertisement
Guest User

Untitled

a guest
May 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. windowHeight = document.documentElement.clientHeight * 3;
  2. windowWidth = document.documentElement.clientWidth;
  3. gravity = 200;
  4. camera = windowHeight;
  5. PLATFORM_WIDTH = 50;
  6. PLATFORM_HEIGHT = 10;
  7. PLATFORMS_DISTANCE = 150;
  8. CAMERA_MOVE = PLATFORMS_DISTANCE - 20;
  9.  
  10. Crafty.init(windowWidth, windowHeight, document.getElementById('game'));
  11.  
  12.  
  13. lvlOne();
  14.  
  15.  
  16.  
  17.  
  18.  
  19. //lvls
  20. function lvlOne(){
  21. Crafty.e('Floor, 2D, Canvas, Color')
  22. .attr({x: 0, y: windowHeight-20, w: windowWidth, h: 20})
  23. .color('green');
  24.  
  25. hero = Crafty.e('2D, Canvas, Color, Twoway, Gravity')
  26. .attr({x: 0, y: windowHeight-30, w: 50, h: 50})
  27. .color('#F00')
  28. .twoway(gravity)
  29. .gravity('Floor');
  30.  
  31. for(i = 0; i < windowHeight; i+=PLATFORMS_DISTANCE){
  32. createPlatform(i);
  33. createPlatform(i);
  34. createPlatform(i);
  35. createPlatform(i);
  36. }
  37.  
  38. $('body').keyup( function(e) {
  39. if(e.keyCode == 87 || e.keyCode == 38){
  40. $( "#game" ).animate({ "top": "+=" + CAMERA_MOVE+ "px" }, "slow" );
  41. camera-=(CAMERA_MOVE);
  42. }
  43.  
  44. setTimeout(isAlive, 1000);
  45.  
  46. });
  47. }
  48.  
  49.  
  50. //__________________________________________________
  51. //usefull functions
  52. function getRandomArbitrary(min, max) {
  53. return Math.random() * (max - min) + min;
  54. }
  55.  
  56. function createPlatform(y) {
  57. Crafty.e('Floor, 2D, Canvas, Color')
  58. .attr({x:getRandomArbitrary(0, windowWidth - PLATFORM_WIDTH), y: y, w: PLATFORM_WIDTH, h: PLATFORM_HEIGHT})
  59. .color('green');
  60. }
  61.  
  62. isAlive = function(){
  63. if(hero.y > camera)
  64. alert("DIE");
  65. }
  66. //move camera to start
  67. function scrollToBottom(){
  68. window.scrollTo(0,camera);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement