Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Phaser Game</title>
  5. <script type="text/javascript" src="js/phaser.js"></script>
  6. <link rel="stylesheet" type="text/css" href="css/main.css">
  7. </head>
  8. <body>
  9.  
  10. <div id='container'></div>
  11.  
  12. <script type="text/javascript">
  13.  
  14. var game = new Phaser.Game(800, 600, Phaser.AUTO, 'container', {preload: preload, create: create, update: update});
  15.  
  16. function preload(){
  17.  
  18. game.load.image('sky', 'assets/sky.png');
  19. game.load.image('ground', 'assets/ground.png');
  20. game.load.image('star', 'assets/star.png');
  21. game.load.spritesheet('dude', 'assets/dude.png', 32, 48);
  22.  
  23. game.load.spritesheet('baddie', 'assets/baddie.png', 32, 32);
  24. game.load.image('firstaid', 'assets/firstaid.png');
  25. game.load.image('diamond', 'assets/diamond.png');
  26. game.load.image('platform', 'assets/platform.png');
  27. }
  28.  
  29. var platforms;
  30. var player;
  31. var cursor;
  32. var stars;
  33. var baddie;
  34. var sky;
  35. var diamonds;
  36. var diamondSpawn = false;
  37.  
  38. var baddieCount = 0;
  39.  
  40. var score = 0;
  41. var scoreText;
  42.  
  43. var lives = 3;
  44. var liveText;
  45.  
  46. var camSpeed = 4;
  47.  
  48. function create(){
  49. init();
  50. }
  51.  
  52. function update(){
  53.  
  54. sky.tilePosition.x += 2;
  55. checkScore();
  56. followPlayer();
  57.  
  58. checkRemainingStars();
  59. //check if collide on top first, if its true; then we cant kill player and we kill
  60. //baddie, otherwise if its false, we can check overlap and if thats true, then we
  61. //take a life away
  62.  
  63. game.physics.arcade.collide(player, platforms);
  64. game.physics.arcade.collide(stars, platforms);
  65. game.physics.arcade.collide(baddie, platforms);
  66. game.physics.arcade.collide(diamonds, platforms);
  67. game.physics.arcade.overlap(player, stars, collectStar, null, this);
  68. game.physics.arcade.overlap(player, diamonds, collectDiamond, null, this);
  69.  
  70. if(game.physics.arcade.collide(player, baddie) == true){
  71. killBaddie();
  72. hasSpawn = false;
  73. }else if(game.physics.arcade.overlap(player, baddie, decreaseHealth, null, this) == true){
  74. console.log("player killed");
  75. }else{
  76. //do nothing
  77. }
  78.  
  79. //game.physics.arcade.overlap(player, baddie, decreaseHealth, null, this);
  80.  
  81. if(game.physics.arcade.collide(player, baddie)){
  82. killBaddie();
  83. hasSpawn = false;
  84. }
  85.  
  86. player.body.velocity.x = 0;
  87.  
  88. if(cursors.left.isDown){
  89. player.body.velocity.x = -150 * 3;
  90. player.animations.play('left');
  91. }else if(cursors.right.isDown){
  92. player.body.velocity.x = 150 * 3;
  93. player.animations.play('right');
  94. }else if(cursors.down.spacebar){
  95. console.log("test");
  96. }else{
  97. player.animations.stop();
  98. player.frame = 4;
  99. }
  100.  
  101. if(cursors.up.isDown && player.body.touching.down){
  102. player.body.velocity.y = -350;
  103. }
  104.  
  105. }
  106.  
  107. function decreaseHealth(player, baddie){
  108. player.kill();
  109. if(checkLives() == true){
  110. respawnPlayer();
  111. lives--;
  112. liveText.text = 'Lives: ' + lives;
  113. }else{
  114. console.log("game over!!");
  115. }
  116. }
  117.  
  118. function checkRemainingStars(){
  119. if(stars.total <= 0){
  120. //change background; maybe switch level
  121. }
  122. }
  123.  
  124. function checkLives(){
  125. if(lives > 0)
  126. return true;
  127. else
  128. return false;
  129. }
  130.  
  131. function respawnPlayer(){
  132. player = game.add.sprite(32, game.world.height - 150, 'dude');
  133.  
  134. // We need to enable physics on the player
  135. game.physics.arcade.enable(player);
  136.  
  137. // Player physics properties. Give the little guy a slight bounce.
  138. player.body.bounce.y = 0.2;
  139. player.body.gravity.y = 300;
  140. player.body.collideWorldBounds = true;
  141.  
  142. // Our two animations, walking left and right.
  143. player.animations.add('left', [0, 1, 2, 3], 10, true);
  144. player.animations.add('right', [5, 6, 7, 8], 10, true);
  145. }
  146.  
  147. function collectStar(player, star){
  148. star.kill();
  149. score += 10;
  150. scoreText.text = 'Score: ' + score;
  151.  
  152. }
  153.  
  154. function killBaddie(){
  155. baddie.kill();
  156.  
  157. if(diamondSpawn == false){
  158. createDiamond();
  159. diamondSpawn = true;
  160. }else{
  161. //do nothing
  162. }
  163. }
  164.  
  165. function collectDiamond(player, diamond){
  166. diamonds.kill();
  167. diamondSpawn = false;
  168. score += 50;
  169. scoreText.text = 'Score: ' + score;
  170. }
  171.  
  172. function createDiamond(){
  173.  
  174. var pos = getRandomPos();
  175.  
  176. diamonds = game.add.sprite(pos, game.world.height - 150, 'diamond');
  177. diamonds.enableBody = true;
  178. game.physics.arcade.enable(diamonds);
  179. diamonds.body.bounce.y = 0.2;
  180. diamonds.body.gravity.y = 300;
  181. diamonds.body.collideWorldBounds = true;
  182. }
  183.  
  184. function getRandomPos(){
  185. var x = 1600;
  186.  
  187. var pos = Math.floor(Math.random() * 1600);
  188. return pos;
  189. }
  190.  
  191. function init(){
  192.  
  193. game.physics.startSystem(Phaser.Physics.ARCADE);
  194.  
  195. game.world.setBounds(0,0,1600, 600);
  196.  
  197. sky = game.add.tileSprite(0,0,1600,600,'sky');
  198.  
  199. platforms = game.add.group();
  200. platforms.enableBody = true;
  201.  
  202. var ground = platforms.create(0, game.world.height - 26, 'ground');
  203. ground.body.immovable = true;
  204. ground.scale.setTo(8,1);
  205.  
  206.  
  207. for(var i = 0; i < 3; i++){
  208. generatePlatforms();
  209. }
  210.  
  211. player = game.add.sprite(32, game.world.height - 150, 'dude');
  212.  
  213. // We need to enable physics on the player
  214. game.physics.arcade.enable(player);
  215.  
  216. // Player physics properties. Give the little guy a slight bounce.
  217. player.body.bounce.y = 0.2;
  218. player.body.gravity.y = 300;
  219. player.body.collideWorldBounds = true;
  220.  
  221. // Our two animations, walking left and right.
  222. player.animations.add('left', [0, 1, 2, 3], 10, true);
  223. player.animations.add('right', [5, 6, 7, 8], 10, true);
  224.  
  225. game.camera.follow(player);
  226.  
  227. cursors = game.input.keyboard.createCursorKeys();
  228.  
  229. stars = game.add.group();
  230. stars.enableBody = true;
  231.  
  232. for(var i = 0; i < 15; i++){
  233.  
  234. var star = stars.create(i * 50, 0, 'star');
  235. star.body.gravity.y = 300;
  236. star.body.bounce.y = 0.7 + Math.random() * 0.2;
  237.  
  238. }
  239.  
  240. scoreText = game.add.text(16,16, 'Score: ' + score, {fontSize: '32px', fill: '#000'});
  241. liveText = game.add.text(16, 580, 'Lives: ' + lives, {fontSize: '15px', fill: '#000'});
  242. }
  243.  
  244. function generatePlatforms(){
  245. //need to create a platform at a random x/y but make sure it doesnt overlap with anything
  246. //already present in the same x/y area
  247.  
  248. var randX = getRandomX();
  249. var randY = getRandomY();
  250.  
  251. if(randX >= 1600 - 400){
  252. randX -= 400;
  253. }
  254.  
  255. if(checkIfOk(randX, randY)){
  256. var ledge = platforms.create(randX, randY, 'platform');
  257. ledge.body.immovable = true;
  258. }else{
  259. //generatePlatforms();
  260. }
  261. }
  262.  
  263. function checkIfOk(randX, randY){
  264. for(var i = 0; i < platforms.children.length; i++){
  265. console.log(randX);
  266. console.log(randY);
  267. console.log("platform: " + platforms.children[i].x);
  268. if(randX + 400 < platforms.children[i].x){
  269. console.log("valid so far");
  270. if(randY + 100 < game.world.height || randY - 100 > 0){
  271. console.log("complete true");
  272. return true;
  273. }else{
  274. return false;
  275. }
  276. }else{
  277. return false;
  278. }
  279. }
  280. }
  281.  
  282. function getRandomX(){
  283. var width = 1600;
  284. var pos;
  285.  
  286. pos = Math.floor(Math.random() * 1600);
  287. return pos;
  288. }
  289.  
  290. function getRandomY(){
  291. var height = 600;
  292. var pos;
  293.  
  294. pos = Math.floor(Math.random() * 600);
  295. return pos;
  296.  
  297. }
  298.  
  299. var hasSpawn = false;
  300.  
  301. function checkScore(){
  302. if(score >= 50){
  303. if(hasSpawn == false){
  304. spawnBaddie();
  305. hasSpawn = true;
  306. }
  307. }
  308. }
  309.  
  310. function spawnBaddie(){
  311.  
  312. baddie = game.add.sprite(200, game.world.height - 150, 'baddie');
  313. game.physics.arcade.enable(baddie);
  314. baddie.body.bounce.y = 0.2;
  315. baddie.body.gravity.y = 300;
  316. baddie.body.collideWorldBounds = true;
  317. baddie.animations.add('left', [0, 1], 10, true);
  318. baddie.animations.add('right', [2,3], 10, true);
  319. baddie.body.checkCollision.up = true;
  320. baddie.body.checkCollision.down = true;
  321. baddie.body.checkCollision.left = false;
  322. baddie.body.checkCollision.right = false;
  323. }
  324.  
  325. function followPlayer(){
  326. var x = player.x;
  327. var y = player.y;
  328.  
  329. if(hasSpawn == true){
  330. if(x > baddie.x + 10){
  331. baddie.body.velocity.x = 150;
  332. baddie.animations.play('right');
  333. }else if(x < baddie.x - 10){
  334. baddie.body.velocity.x = -150;
  335. baddie.animations.play('left');
  336. }else{
  337. baddie.frame = 4;
  338. }
  339. }
  340. }
  341.  
  342. </script>
  343.  
  344. </body>
  345. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement