Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package com.sincek.renato;
  2.  
  3. import com.badlogic.gdx.ApplicationAdapter;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.audio.Music;
  7. import com.badlogic.gdx.audio.Sound;
  8. import com.badlogic.gdx.graphics.GL20;
  9. import com.badlogic.gdx.graphics.OrthographicCamera;
  10. import com.badlogic.gdx.graphics.Texture;
  11. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  12. import com.badlogic.gdx.math.MathUtils;
  13. import com.badlogic.gdx.utils.Array;
  14. import com.badlogic.gdx.utils.TimeUtils;
  15.  
  16. import java.awt.Rectangle;
  17. import java.util.Iterator;
  18.  
  19. public class MyGdxGame extends ApplicationAdapter {
  20.  
  21. private Texture dropImage;
  22. private Texture bucketImage;
  23. private Sound dropSound;
  24. private Music rainMusic;
  25.  
  26. private OrthographicCamera camera;
  27.  
  28. private SpriteBatch spriteBatch;
  29.  
  30. private Rectangle bucket;
  31. private Array<Rectangle> raindrops;
  32.  
  33. private long lastDropTime;
  34.  
  35.  
  36. @Override
  37. public void create () {
  38. dropImage = new Texture(Gdx.files.internal("droplet.png"));
  39. bucketImage = new Texture(Gdx.files.internal("bucket.png"));
  40. dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
  41. rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
  42.  
  43. rainMusic.setLooping(true);
  44. rainMusic.play();
  45.  
  46. camera = new OrthographicCamera();
  47. camera.setToOrtho(false,800,480);
  48.  
  49. spriteBatch = new SpriteBatch();
  50.  
  51. bucket = new Rectangle();
  52. bucket.x = 800/2 - 64/2;
  53. bucket.y = 20;
  54. bucket.width = 64;
  55. bucket.height = 64;
  56.  
  57. raindrops = new Array<Rectangle>();
  58.  
  59.  
  60. }
  61.  
  62. @Override
  63. public void render () {
  64. Gdx.gl.glClearColor(0, 0, 0.2f, 1);
  65. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  66. camera.update();
  67. spriteBatch.setProjectionMatrix(camera.combined);
  68.  
  69. spriteBatch.begin();
  70. spriteBatch.draw(bucketImage,bucket.x,bucket.y);
  71. for (Rectangle raindrop: raindrops){
  72. spriteBatch.draw(dropImage,raindrop.x,raindrop.y);
  73. }
  74.  
  75. spriteBatch.end();
  76.  
  77. if (Gdx.input.isTouched()){
  78. bucket.x = Gdx.input.getX() - 64/2;
  79. }
  80. if (Gdx.input.isKeyPressed(Input.Keys.LEFT)){
  81. bucket.x -= 200 * Gdx.graphics.getDeltaTime();
  82. }
  83. if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
  84. bucket.x -= 200 * Gdx.graphics.getDeltaTime();
  85. }
  86. if (bucket.x < 0){
  87. bucket.x = 0;
  88. }
  89. if (bucket.x > 800 - 64){
  90. bucket.x = 800 - 64;
  91. }
  92.  
  93. if (TimeUtils.nanoTime() - lastDropTime > 1000000000){
  94. spawnRaindrop();
  95. }
  96.  
  97. Iterator<Rectangle> iterator = raindrops.iterator();
  98.  
  99. while (iterator.hasNext()){
  100.  
  101. Rectangle raindrop = iterator.next();
  102. raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
  103. if (raindrop.y + 64 < 0 ){
  104. iterator.remove();
  105. }
  106. if (raindrop.)
  107.  
  108. }
  109.  
  110.  
  111. }
  112.  
  113. @Override
  114. public void dispose () {
  115.  
  116.  
  117.  
  118. }
  119.  
  120. private void spawnRaindrop(){
  121.  
  122. Rectangle raindrop = new Rectangle();
  123. raindrop.x = MathUtils.random(0,800 - 64);
  124. raindrop.y = 480;
  125. raindrop.width = 64;
  126. raindrop.height = 64;raindrops.add(raindrop);
  127. lastDropTime = TimeUtils.nanoTime();
  128.  
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement