Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
316
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 = new Random();
  62.    
  63.     @Override
  64.     public void create () {
  65.         screenWidth = Gdx.graphics.getWidth();
  66.         screenHeight = Gdx.graphics.getHeight();
  67.         ballSpeed = new Vector2(500,500);
  68.        
  69.         ow = Gdx.audio.newSound(Gdx.files.internal("ow.mp3"));
  70.         omg = Gdx.audio.newSound(Gdx.files.internal("omg.mp3"));
  71.         tr = Gdx.audio.newSound(Gdx.files.internal("tr.mp3"));
  72.         ns = Gdx.audio.newSound(Gdx.files.internal("ns.mp3"));
  73.         noo = Gdx.audio.newSound(Gdx.files.internal("noo.mp3"));
  74.         hitmarker = Gdx.audio.newSound(Gdx.files.internal("hitmarker.wav"));
  75.         darude = Gdx.audio.newMusic(Gdx.files.internal("darude.mp3"));
  76.  
  77.        
  78.         ballWidth = 15;
  79.         ballHeight = 15;
  80.        
  81.         fps = new FPSLogger();
  82.        
  83.         batch = new SpriteBatch();
  84.         paddleTexture = new Texture(Gdx.files.internal("paddle.png"));
  85.         ballTexture = new Texture(Gdx.files.internal("ball.png"));
  86.         hitmarkerTexture = new Texture(Gdx.files.internal("hitmarker.png"));
  87.         woodTexture = new Texture(Gdx.files.internal("wood.jpg"));
  88.         dewTexture = new Texture(Gdx.files.internal("dew.png"));
  89.        
  90.         ballPosition = new Vector2(screenCenterX, screenCenterY);
  91.         Gdx.input.setCursorCatched(true);
  92.         hitWidthHalf = hitmarkerTexture.getWidth() / 2;
  93.         hitHeightHalf = hitmarkerTexture.getHeight() / 2;
  94.         ballWidthHalf = ballTexture.getWidth() / 2;
  95.         ballHeightHalf = ballTexture.getHeight() / 2;
  96.         darude.setLooping(true);
  97.         darude.play();
  98.         hideCursor();
  99.     }
  100.    
  101.     public void updateInput() {
  102.         paddlePosition.x = Gdx.input.getX() - paddleTexture.getWidth() / 2;
  103.         if(Gdx.input.isKeyJustPressed(Keys.SPACE)){
  104.             Dew dewObj = new Dew(MathUtils.random(0,800), 200);
  105.             dewList.add(dewObj);
  106.         }
  107.     }
  108.    
  109.     public void checkBallCollision() {
  110.         ballPosition.x += ballSpeed.x * Gdx.graphics.getDeltaTime();
  111.         ballPosition.y += ballSpeed.y * Gdx.graphics.getDeltaTime();
  112.        
  113.         int rng = random.nextInt(6);
  114.        
  115.         if (ballPosition.y + ballTexture.getHeight() > screenHeight){
  116.             if (rng >= 0 && rng < 5){
  117.                 ow.play();     
  118.             }
  119.             else if(rng >= 5){
  120.                 omg.play();
  121.             }
  122.             ballPosition.y = screenHeight - ballTexture.getHeight();
  123.             ballSpeed.y *= -1;
  124.             batch.begin();
  125.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  126.             batch.end();
  127.             hitmarker.play();
  128.         }
  129.         if (ballPosition.y < 0){
  130.             if (rng >= 0 && rng < 5){
  131.                 ow.play();
  132.             }
  133.             else if(rng >= 5){
  134.                 omg.play();
  135.             }
  136.             ballPosition.y = 0;
  137.             ballSpeed.y *= -1;
  138.             batch.begin();
  139.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  140.             batch.end();
  141.             hitmarker.play();
  142.         }
  143.         if (ballPosition.x + ballTexture.getWidth() > screenWidth){
  144.             if (rng >= 0 && rng < 5){
  145.                 ow.play();
  146.             }
  147.             else if(rng >= 5){
  148.                 omg.play();
  149.             }
  150.             ballPosition.x = screenWidth - ballTexture.getWidth();
  151.             ballSpeed.x *= -1;
  152.             batch.begin();
  153.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  154.             batch.end();
  155.             hitmarker.play();
  156.         }
  157.         if (ballPosition.x < 0){
  158.             if (rng >= 0 && rng < 5){
  159.                 ow.play();
  160.             }
  161.             else if(rng >= 5){
  162.                 omg.play();
  163.             }
  164.             ballPosition.x = 0;
  165.             ballSpeed.x *= -1;
  166.             batch.begin();
  167.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  168.             batch.end();
  169.             hitmarker.play();
  170.         }
  171.        
  172.         int rng2 = random.nextInt(2);
  173.         if (ballPosition.y <= paddlePosition.y + paddleTexture.getHeight() && (ballPosition.x >= paddlePosition.x && ballPosition.x <= paddlePosition.x + paddleTexture.getWidth())&& ballSpeed.y < 0){
  174.             if (rng2 == 0){
  175.                 tr.play();
  176.             }
  177.             else if (rng2 == 1){
  178.                 ns.play();
  179.             }
  180.             ballPosition.y = paddlePosition.y + paddleTexture.getHeight();
  181.             ballSpeed.y *= -1;
  182.             batch.begin();
  183.             batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
  184.             batch.end();
  185.             hitmarker.play();
  186.         }
  187.     }
  188.    
  189.     public void checkPaddle(){
  190.         if (paddlePosition.x < 0){
  191.             paddlePosition.x = 0;
  192.         }
  193.         if (paddlePosition.x + paddleTexture.getWidth() > screenWidth){
  194.             paddlePosition.x = screenWidth - paddleTexture.getWidth();
  195.         }
  196.     }
  197.    
  198.     public void hideCursor() {
  199.         if (Gdx.input.isCursorCatched() && Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
  200.             Gdx.input.setCursorCatched(false);
  201.         }
  202.         else if (!(Gdx.input.isCursorCatched()) && Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
  203.             Gdx.input.setCursorCatched(true);
  204.         }
  205.     }
  206.  
  207.     @Override
  208.     public void render () {
  209.         Gdx.gl.glClearColor(0, 0, 0, 1);
  210.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  211.         batch.begin();
  212.         batch.draw(woodTexture, 0, 0);
  213.         batch.draw(paddleTexture, paddlePosition.x, paddlePosition.y);
  214.         batch.draw(ballTexture, ballPosition.x, ballPosition.y, ballWidth, ballHeight);
  215.         //batch.draw(dewTexture,0,0, 75, 100);
  216.         for (int i = 0; i < dewList.size; i++){
  217.             Dew currentDew = dewList.get(i);
  218.             batch.draw(dewTexture, currentDew.x, currentDew.y, 75, 100);
  219.         }
  220.         batch.end();
  221.         fps.log();
  222.         updateInput();
  223.         checkPaddle();
  224.         checkBallCollision();
  225.         hideCursor();
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement