Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.16 KB | None | 0 0
  1. package com.orhantozan.game;
  2.  
  3. import java.util.Random;
  4.  
  5. import com.badlogic.gdx.ApplicationAdapter;
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.Input.Keys;
  8. import com.badlogic.gdx.audio.Music;
  9. import com.badlogic.gdx.audio.Sound;
  10. import com.badlogic.gdx.graphics.FPSLogger;
  11. import com.badlogic.gdx.graphics.GL20;
  12. import com.badlogic.gdx.graphics.Texture;
  13. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  14. import com.badlogic.gdx.math.MathUtils;
  15. import com.badlogic.gdx.math.Rectangle;
  16. import com.badlogic.gdx.math.Vector2;
  17. import com.badlogic.gdx.utils.Array;
  18.  
  19. public class PongMain extends ApplicationAdapter {
  20.     SpriteBatch batch;
  21.    
  22.     Texture paddleTexture;
  23.     Texture ballTexture;
  24.     Texture hitmarkerTexture;
  25.     Texture woodTexture;
  26.     Texture dewTexture;
  27.    
  28.     float hitWidthHalf;
  29.     float hitHeightHalf;
  30.     float ballWidthHalf;
  31.     float ballHeightHalf;
  32.    
  33.     int screenWidth;
  34.     int screenHeight;
  35.  
  36.     int screenCenterX;
  37.     int screenCenterY;
  38.    
  39.    
  40.     Vector2 paddlePosition = new Vector2();
  41.     Vector2 ballPosition;
  42.     Vector2 ballSpeed;
  43.    
  44.     Array<Dew> dewList;
  45.    
  46.     FPSLogger fps;
  47.    
  48.     int ballWidth;
  49.     int ballHeight;
  50.    
  51.     Sound ow;
  52.     Sound omg;
  53.    
  54.     Sound tr;
  55.     Sound ns;
  56.     Sound noo;
  57.    
  58.     Sound hitmarker;
  59.     Music darude;
  60.    
  61.     Random random;
  62.    
  63.     @Override
  64.     public void create() {
  65.         random = new Random();
  66.        
  67.         screenWidth = Gdx.graphics.getWidth();
  68.         screenHeight = Gdx.graphics.getHeight();
  69.         ballSpeed = new Vector2(500,500);
  70.        
  71.         ow = Gdx.audio.newSound(Gdx.files.internal("ow.mp3"));
  72.         omg = Gdx.audio.newSound(Gdx.files.internal("omg.mp3"));
  73.         tr = Gdx.audio.newSound(Gdx.files.internal("tr.mp3"));
  74.         ns = Gdx.audio.newSound(Gdx.files.internal("ns.mp3"));
  75.         noo = Gdx.audio.newSound(Gdx.files.internal("noo.mp3"));
  76.         hitmarker = Gdx.audio.newSound(Gdx.files.internal("hitmarker.wav"));
  77.         darude = Gdx.audio.newMusic(Gdx.files.internal("darude.mp3"));
  78.  
  79.         dewList = new Array<Dew>();
  80.        
  81.         ballWidth = 15;
  82.         ballHeight = 15;
  83.        
  84.         fps = new FPSLogger();
  85.        
  86.         batch = new SpriteBatch();
  87.         paddleTexture = new Texture(Gdx.files.internal("paddle.png"));
  88.         ballTexture = new Texture(Gdx.files.internal("ball.png"));
  89.         hitmarkerTexture = new Texture(Gdx.files.internal("hitmarker.png"));
  90.         woodTexture = new Texture(Gdx.files.internal("wood.jpg"));
  91.         dewTexture = new Texture(Gdx.files.internal("dew.png"));
  92.        
  93.         ballPosition = new Vector2(screenCenterX, screenCenterY);
  94.         Gdx.input.setCursorCatched(true);
  95.         hitWidthHalf = hitmarkerTexture.getWidth() / 2;
  96.         hitHeightHalf = hitmarkerTexture.getHeight() / 2;
  97.         ballWidthHalf = ballTexture.getWidth() / 2;
  98.         ballHeightHalf = ballTexture.getHeight() / 2;
  99.         darude.setLooping(true);
  100.         darude.play();
  101.         hideCursor();
  102.     }
  103.    
  104.     public void updateInput() {
  105.         paddlePosition.x = Gdx.input.getX() - paddleTexture.getWidth() / 2;
  106.         if(Gdx.input.isKeyJustPressed(Keys.SPACE)){
  107.             Dew dewObj = new Dew(MathUtils.random(0,800), 200);
  108.             dewList.add(dewObj);
  109.         }
  110.     }
  111.    
  112.     public void checkBallCollision() {
  113.         ballPosition.x += ballSpeed.x * Gdx.graphics.getDeltaTime();
  114.         ballPosition.y += ballSpeed.y * Gdx.graphics.getDeltaTime();
  115.        
  116.         int rng = random.nextInt(6);
  117.        
  118.         if (ballPosition.y + ballTexture.getHeight() > screenHeight){
  119.             if (rng >= 0 && rng < 5){
  120.                 ow.play();     
  121.             }
  122.             else if(rng >= 5){
  123.                 omg.play();
  124.             }
  125.             ballPosition.y = screenHeight - ballTexture.getHeight();
  126.             ballSpeed.y *= -1;
  127.             batch.begin();
  128.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  129.             batch.end();
  130.             hitmarker.play();
  131.         }
  132.         if (ballPosition.y < 0){
  133.             if (rng >= 0 && rng < 5){
  134.                 ow.play();
  135.             }
  136.             else if(rng >= 5){
  137.                 omg.play();
  138.             }
  139.             ballPosition.y = 0;
  140.             ballSpeed.y *= -1;
  141.             batch.begin();
  142.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  143.             batch.end();
  144.             hitmarker.play();
  145.         }
  146.         if (ballPosition.x + ballTexture.getWidth() > screenWidth){
  147.             if (rng >= 0 && rng < 5){
  148.                 ow.play();
  149.             }
  150.             else if(rng >= 5){
  151.                 omg.play();
  152.             }
  153.             ballPosition.x = screenWidth - ballTexture.getWidth();
  154.             ballSpeed.x *= -1;
  155.             batch.begin();
  156.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  157.             batch.end();
  158.             hitmarker.play();
  159.         }
  160.         if (ballPosition.x < 0){
  161.             if (rng >= 0 && rng < 5){
  162.                 ow.play();
  163.             }
  164.             else if(rng >= 5){
  165.                 omg.play();
  166.             }
  167.             ballPosition.x = 0;
  168.             ballSpeed.x *= -1;
  169.             batch.begin();
  170.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  171.             batch.end();
  172.             hitmarker.play();
  173.         }
  174.        
  175.         int rng2 = random.nextInt(2);
  176.         if (ballPosition.y <= paddlePosition.y + paddleTexture.getHeight() && (ballPosition.x >= paddlePosition.x && ballPosition.x <= paddlePosition.x + paddleTexture.getWidth())&& ballSpeed.y < 0){
  177.             if (rng2 == 0){
  178.                 tr.play();
  179.             }
  180.             else if (rng2 == 1){
  181.                 ns.play();
  182.             }
  183.             ballPosition.y = paddlePosition.y + paddleTexture.getHeight();
  184.             ballSpeed.y *= -1;
  185.             batch.begin();
  186.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  187.             batch.end();
  188.             hitmarker.play();
  189.         }
  190.     }
  191.    
  192.     public void checkPaddle(){
  193.         if (paddlePosition.x < 0){
  194.             paddlePosition.x = 0;
  195.         }
  196.         if (paddlePosition.x + paddleTexture.getWidth() > screenWidth){
  197.             paddlePosition.x = screenWidth - paddleTexture.getWidth();
  198.         }
  199.     }
  200.    
  201.     public void hideCursor() {
  202.         if (Gdx.input.isCursorCatched() && Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
  203.             Gdx.input.setCursorCatched(false);
  204.         }
  205.         else if (!(Gdx.input.isCursorCatched()) && Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
  206.             Gdx.input.setCursorCatched(true);
  207.         }
  208.     }
  209.  
  210.     @Override
  211.     public void render () {
  212.         Gdx.gl.glClearColor(0, 0, 0, 1);
  213.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  214.         batch.begin();
  215.         batch.draw(woodTexture, 0, 0);
  216.         batch.draw(paddleTexture, paddlePosition.x, paddlePosition.y);
  217.         batch.draw(ballTexture, ballPosition.x, ballPosition.y, ballWidth, ballHeight);
  218.         for (int i = 0; i < dewList.size; i++){
  219.             Dew currentDew = dewList.get(i);
  220.             batch.draw(dewTexture, currentDew.x, currentDew.y, 75, 100);
  221.         }
  222.         batch.end();
  223.         fps.log();
  224.         updateInput();
  225.         checkPaddle();
  226.         checkBallCollision();
  227.         hideCursor();
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement