Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.54 KB | None | 0 0
  1. var game;
  2.  
  3. var SCREEN_W = 500;
  4. var SCREEN_H = 340;
  5.  
  6. var bootState = {
  7. preload: function() {
  8. game.load.image('progressBar', 'assets/progressBar.png');
  9. },
  10. create: function() {
  11. game.stage.backgroundColor = "#000000";
  12. game.physics.startSystem(Phaser.Physics.ARCADE);
  13.  
  14. game.state.start('load');
  15.  
  16. if(!game.device.desktop)
  17. {
  18. game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  19. document.body.style.backgroundColor = "#000000";
  20. game.scale.minWidth = 250;
  21. game.scale.minHeight = 170;
  22. game.scale.maxWidth = 1000;
  23. game.maxHeight = 680;
  24. game.scale.pageAlignVertically = true;
  25. }
  26. }
  27. };
  28. var loadState = {
  29. preload: function() {
  30. var loadingText = game.add.text(game.world.centerX, 150, 'Loading...', { font: '30px Roboto', fill: '#FFFFFF' });
  31. loadingText.anchor.setTo(.5, .5);
  32. var progressBar = game.add.sprite(game.world.centerX, 200, 'progressBar');
  33. progressBar.anchor.setTo(.5, .5);
  34. game.load.setPreloadSprite(progressBar);
  35. //game.load.image('mario', 'assets/mario.png');
  36. game.load.image('wallV', 'assets/wallVertical.png');
  37. game.load.image('wallH', 'assets/wallHorizontal.png');
  38. game.load.image('coin', 'assets/uCoin.png');
  39. //game.load.image('enemy', 'assets/enemy.png');
  40. game.load.spritesheet('mario', 'assets/marioSS.png', 16, 16);
  41. game.load.spritesheet('enemy', 'assets/greenShell.png', 20, 28);
  42. game.load.image('background', 'assets/background.png');
  43. game.load.audio('jump', ['assets/jump.ogg', 'assets/jump.mp3']);
  44. game.load.audio('coin', ['assets/coin.ogg', 'assets/coin.mp3']);
  45. game.load.audio('dead', ['assets/dead.ogg', 'assets/dead.mp3']);
  46. game.load.image('jumpButton', 'assets/jumpButton.png');
  47. game.load.image('rightButton','asset/rightButton.png');
  48. game.load.image('leftButton', 'assets/leftButton.png');
  49. },
  50. create: function() {
  51. game.state.start('menu');
  52. }
  53. };
  54. var menuState = {
  55. create: function() {
  56. var text;
  57. if(game.device.desktop)
  58. text = 'Press the Up Arrow to Start!';
  59. else
  60. text = 'Touch the Screen to Start!';
  61.  
  62. var startLabel = game.add.text(game.world.centerX, game.world.height - 80, text, {font: '25px Roboto', fill: '#FFFFFF'});
  63. startLabel.anchor.setTo(.5, .5);
  64.  
  65. game.add.image(0, 0, 'background');
  66.  
  67. if (!localStorage.getItem('bestScore'))
  68. {
  69. localStorage.setItem('bestScore', 0);
  70. }
  71. if (game.global.score > localStorage.getItem('bestScore'))
  72. {
  73. localStorage.setItem('bestScore', game.global.score);
  74. }
  75.  
  76. var nameLabel = game.add.text(game.world.centerX, 80, 'Super Ucode Game!', { font: '50px Arial', fill: '#FFFFFF' });
  77. nameLabel.anchor.setTo(.5, .5);
  78. game.add.tween(nameLabel).to({ y: 80 }, 1000).easing(Phaser.Easing.Bounce.Out).start();
  79.  
  80. var scoreText = 'Score: ' + game.global.score + '\nBest Score: ' + localStorage.getItem('bestScore')
  81. var scoreLabel = game.add.text(game.world.centerX, game.world.centerY, scoreText , {font: '25px Roboto', fill: '#FFFFFF'});
  82. scoreLabel.anchor.setTo(.5, .5);
  83.  
  84. scoreLabel.anchor.setTo(.5, .5);
  85.  
  86. var otherLabel = game.add.text(36, 250, text, { font: '30px Arial', fill: '#FFFFFF' });
  87. otherLabel.anchor.setTo(.01, .5);
  88.  
  89. var upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
  90. upKey.onDown.addOnce(this.startGame, this);
  91.  
  92. if(game.device.desktop)
  93. {
  94. var upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
  95. upKey.onDown.addOnce(this.startGame, this);
  96. }
  97. else
  98. {
  99. game.input.onTap.addOnce(this.startGame, this);
  100. }
  101.  
  102. },
  103. startGame: function() {
  104. game.state.start('main');
  105. }
  106. };
  107. var mainState = {
  108. create: function() {
  109. //this.helloText = game.add.text(250, 170, 'Hello World!', {font: '20px Roboto', fill: '#FF0000' } );
  110. this.scoreText = game.add.text(30, 30, 'Score: 0', { font: '18px Roboto', fill: '#FFFFFF' });
  111. //this.helloText.anchor.setTo(0.5,0.5)
  112. this.sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'mario');
  113. this.sprite.anchor.setTo(.5, 0.5);
  114. //game.physics.startSystem(Phaser.Physics.ARCADE);
  115. game.physics.arcade.enable(this.sprite);
  116. this.sprite.body.gravity.y = 500;
  117. this.cursor = game.input.keyboard.createCursorKeys();
  118. this.createWorld();
  119. this.coin = game.add.sprite(60, 140, 'coin');
  120. this.coin.anchor.setTo(.5, .5);
  121. game.physics.arcade.enable(this.coin);
  122.  
  123. //this.score = 0;
  124. game.global.score = 0;
  125. this.enemies = game.add.group();
  126. this.enemies.enableBody = true;
  127. this.enemies.createMultiple(10, 'enemy');
  128. game.time.events.loop(2000, this.spawnEnemy, this);
  129. this.coinSound = game.add.audio('coin');
  130. this.deadSound = game.add.audio('dead');
  131. this.jumpSound = game.add.audio('jump');
  132. // this.sprite = game.add.sprite(250, 170, 'mario');
  133. this.sprite.anchor.setTo(.5, .5);
  134. this.sprite.animations.add('right', [4, 5, 6, 7], 8, true);
  135. this.sprite.animations.add('left', [3, 2, 1, 0], 8, true);
  136. if(!game.device.desktop)
  137. this.addMobileInputs();
  138. },
  139. update: function() {
  140. game.physics.arcade.collide(this.sprite, this.walls);
  141. //this.sprite.angle++;
  142. this.movePlayer();
  143. if (!this.sprite.inWorld) {
  144. this.restart();
  145. }
  146. game.physics.arcade.collide(this.enemies, this.walls);
  147. game.physics.arcade.overlap(this.sprite, this.coin, this.takeCoin, null, this);
  148. game.physics.arcade.overlap(this.sprite, this.enemies, this.restart, null, this);
  149. },
  150. movePlayer: function() {
  151.  
  152. if (this.cursor.left.isDown || this.moveLeft) {
  153. this.sprite.body.velocity.x = -200;
  154. this.sprite.animations.play('left');
  155. }
  156. else if (this.cursor.right.isDown || this.moveRight) {
  157. this.sprite.body.velocity.x = 200;
  158. this.sprite.animations.play('right');
  159. }
  160. else {
  161. this.sprite.body.velocity.x = 0;
  162. this.sprite.animations.stop();
  163. this.sprite.frame = 4;
  164. }
  165. if (this.cursor.up.isDown && this.sprite.body.touching.down) {
  166. this.sprite.body.velocity.y = -320;
  167. this.jumpSound.play();
  168. this.jumpPlayer()
  169. }
  170. },
  171. createWorld: function() {
  172. this.walls = game.add.group();
  173. this.walls.enableBody = true;
  174. game.add.sprite(0, 0, 'wallV', 0, this.walls);
  175. game.add.sprite(480, 0, 'wallV', 0, this.walls);
  176. game.add.sprite(0, 0, 'wallH', 0, this.walls);
  177. game.add.sprite(300, 0, 'wallH', 0, this.walls);
  178. game.add.sprite(0, 320, 'wallH', 0, this.walls);
  179. game.add.sprite(300, 320, 'wallH', 0, this.walls);
  180. game.add.sprite(-100, 160, 'wallH', 0, this.walls);
  181. game.add.sprite(400, 160, 'wallH', 0, this.walls);
  182.  
  183. //Please check to see if the following set of code belongs in the creatWorld object.
  184. var middleTop = game.add.sprite(100, 80, 'wallH', 0, this.walls);
  185. middleTop.scale.setTo(1.5, 1);
  186. var middleBottom = game.add.sprite(100, 240, 'wallH', 0, this.walls);
  187. middleBottom.scale.setTo(1.5, 1);
  188.  
  189. this.walls.setAll('body.immovable', true);
  190. },
  191. restart: function() {
  192. //game.state.start('main');
  193. if (!this.sprite.alive)
  194. return;
  195. this.sprite.kill();
  196. this.deadSound.play();
  197.  
  198.  
  199. game.state.start('menu');
  200.  
  201. game.time.events.add(1000, game.state.start('menu'), this);
  202.  
  203.  
  204. },
  205. takeCoin: function() {
  206. //this.coin.kill();
  207. this.moveCoin();
  208. //this.score += 5;
  209. this.scoreText.text = 'Score: ' + this.score;
  210. this.coinSound.play();
  211.  
  212. game.add.tween(this.sprite.scale).to({ x: 1.5, y: 1.5 }, 50).to({ x: 1, y: 1 }, 150).start();
  213.  
  214. game.global.score += 10;
  215. this.scoreText.text = 'Score: ' + game.global.score;
  216. },
  217. moveCoin: function() {
  218. var coinPosition = [{ x: 140, y: 60 }, { x: 360, y: 60 },
  219. { x: 60, y: 140 }, { x: 440, y: 140 },
  220. { x: 130, y: 300 }, { x: 370, y: 300 }
  221. ];
  222. for (var i = 0; i < coinPosition.length; i++) {
  223. if (coinPosition[i].x == this.coin.x) {
  224. coinPosition.splice(i, 1);
  225. }
  226. }
  227.  
  228. var newPosition = coinPosition[game.rnd.integerInRange(0, coinPosition.length - 1)];
  229.  
  230. this.coin.reset(newPosition.x, newPosition.y);
  231.  
  232. },
  233. spawnEnemy: function() {
  234. var enemy = this.enemies.getFirstDead();
  235. if (!enemy)
  236.  
  237. return;
  238. enemy.animations.add('spin', [0, 1, 2], 8, true);
  239.  
  240.  
  241. enemy.anchor.setTo(.5, 1);
  242. enemy.reset(game.world.centerX, 0);
  243. enemy.body.gravity.y = 500;
  244. enemy.body.velocity.x = 100 * Phaser.Math.randomSign();
  245. enemy.body.bounce.x = 1;
  246. enemy.checkWorldBounds = true;
  247. enemy.outOfBoundsKill = true;
  248. enemy.animations.play('spin');
  249. },
  250. addMobileInputs: function()
  251. {
  252. this.moveLeft = false;
  253. this.moveRight = false;
  254.  
  255. this.jumpButton = game.add.sprite(350, 247, 'jumpButton');
  256. this.jumpButton.inputEnabled = true;
  257. this.jumpButton.alpha = .5;
  258. this.jumpButton.events.onInputDown.add(this.jumpPlayer, this);
  259.  
  260. this.leftButton = game.add.sprite(50, 247, 'leftButton');
  261. this.leftButton.inputEnabled = true;
  262. this.leftButton.alpha = .5;
  263. this.leftButton.events.onInputUp.add(function(){this.moveLeft = false; this.leftButton.alpha = .5;}, this);
  264. this.leftButton.events.onInputDown.add(function(){this.moveLeft = true; this.leftButton.alpha = 1;}, this);
  265. this.leftButton.events.onInputOut.add(function(){this.moveLeft = false; this.leftButton.alpha = .5;}, this);
  266. this.leftButton.events.onInputOver.add(function(){this.moveLeft = true; this.leftButton.alpha = 1;}, this);
  267.  
  268. this.rightButton = game.add.sprite(130, 247, 'rightButton');
  269. this.rightButton.inputEnabled = true;
  270. this.rightButton.alpha = .5;
  271. this.rightButton.events.onInputOver.add(function(){this.moveRight = true; this.rightButton.alpha = 1}, this);
  272. this.rightButton.events.onInputOut.add(function(){this.moveRight = false; this.rightButton.alpha = .5;}, this);
  273. this.rightButton.events.onInputDown.add(function(){this.moveRight = true; this.rightButton.alpha = 1;}, this);
  274. this.rightButton.events.onInputUp.add(function(){this.moveRight = false; this.rightButton.alpha = .5;}, this);
  275. },
  276. jumpPlayer: function()
  277. {
  278. if(this.sprite.body.touching.down)
  279. {
  280. this.sprite.body.velocity.y = -320;
  281. this.jumpSound.play();
  282. }
  283.  
  284. }
  285. };
  286.  
  287. game = new Phaser.Game(SCREEN_W, SCREEN_H, Phaser.AUTO, 'game');
  288. game.state.add('boot', bootState);
  289. game.state.add('load', loadState);
  290. game.state.add('menu', menuState);
  291. game.state.add('main', mainState);
  292.  
  293. game.global = {
  294. score: 0
  295. };
  296.  
  297. game.state.start('boot');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement