Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.orhantozan.game;
- import java.util.Random;
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.audio.Music;
- import com.badlogic.gdx.audio.Sound;
- import com.badlogic.gdx.graphics.FPSLogger;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.MathUtils;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.utils.Array;
- public class PongMain extends ApplicationAdapter {
- SpriteBatch batch;
- Texture paddleTexture;
- Texture ballTexture;
- Texture hitmarkerTexture;
- Texture woodTexture;
- Texture dewTexture;
- float hitWidthHalf;
- float hitHeightHalf;
- float ballWidthHalf;
- float ballHeightHalf;
- int screenWidth;
- int screenHeight;
- int screenCenterX;
- int screenCenterY;
- Vector2 paddlePosition = new Vector2();
- Vector2 ballPosition;
- Vector2 ballSpeed;
- Array<Dew> dewList;
- FPSLogger fps;
- int ballWidth;
- int ballHeight;
- Sound ow;
- Sound omg;
- Sound tr;
- Sound ns;
- Sound noo;
- Sound hitmarker;
- Music darude;
- Random random;
- @Override
- public void create() {
- random = new Random();
- screenWidth = Gdx.graphics.getWidth();
- screenHeight = Gdx.graphics.getHeight();
- ballSpeed = new Vector2(500,500);
- ow = Gdx.audio.newSound(Gdx.files.internal("ow.mp3"));
- omg = Gdx.audio.newSound(Gdx.files.internal("omg.mp3"));
- tr = Gdx.audio.newSound(Gdx.files.internal("tr.mp3"));
- ns = Gdx.audio.newSound(Gdx.files.internal("ns.mp3"));
- noo = Gdx.audio.newSound(Gdx.files.internal("noo.mp3"));
- hitmarker = Gdx.audio.newSound(Gdx.files.internal("hitmarker.wav"));
- darude = Gdx.audio.newMusic(Gdx.files.internal("darude.mp3"));
- dewList = new Array<Dew>();
- ballWidth = 15;
- ballHeight = 15;
- fps = new FPSLogger();
- batch = new SpriteBatch();
- paddleTexture = new Texture(Gdx.files.internal("paddle.png"));
- ballTexture = new Texture(Gdx.files.internal("ball.png"));
- hitmarkerTexture = new Texture(Gdx.files.internal("hitmarker.png"));
- woodTexture = new Texture(Gdx.files.internal("wood.jpg"));
- dewTexture = new Texture(Gdx.files.internal("dew.png"));
- ballPosition = new Vector2(screenCenterX, screenCenterY);
- Gdx.input.setCursorCatched(true);
- hitWidthHalf = hitmarkerTexture.getWidth() / 2;
- hitHeightHalf = hitmarkerTexture.getHeight() / 2;
- ballWidthHalf = ballTexture.getWidth() / 2;
- ballHeightHalf = ballTexture.getHeight() / 2;
- darude.setLooping(true);
- darude.play();
- hideCursor();
- }
- public void updateInput() {
- paddlePosition.x = Gdx.input.getX() - paddleTexture.getWidth() / 2;
- if(Gdx.input.isKeyJustPressed(Keys.SPACE)){
- Dew dewObj = new Dew(MathUtils.random(0,800), 200);
- dewList.add(dewObj);
- }
- }
- public void checkBallCollision() {
- ballPosition.x += ballSpeed.x * Gdx.graphics.getDeltaTime();
- ballPosition.y += ballSpeed.y * Gdx.graphics.getDeltaTime();
- int rng = random.nextInt(6);
- if (ballPosition.y + ballTexture.getHeight() > screenHeight){
- if (rng >= 0 && rng < 5){
- ow.play();
- }
- else if(rng >= 5){
- omg.play();
- }
- ballPosition.y = screenHeight - ballTexture.getHeight();
- ballSpeed.y *= -1;
- batch.begin();
- batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
- batch.end();
- hitmarker.play();
- }
- if (ballPosition.y < 0){
- if (rng >= 0 && rng < 5){
- ow.play();
- }
- else if(rng >= 5){
- omg.play();
- }
- ballPosition.y = 0;
- ballSpeed.y *= -1;
- batch.begin();
- batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
- batch.end();
- hitmarker.play();
- }
- if (ballPosition.x + ballTexture.getWidth() > screenWidth){
- if (rng >= 0 && rng < 5){
- ow.play();
- }
- else if(rng >= 5){
- omg.play();
- }
- ballPosition.x = screenWidth - ballTexture.getWidth();
- ballSpeed.x *= -1;
- batch.begin();
- batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
- batch.end();
- hitmarker.play();
- }
- if (ballPosition.x < 0){
- if (rng >= 0 && rng < 5){
- ow.play();
- }
- else if(rng >= 5){
- omg.play();
- }
- ballPosition.x = 0;
- ballSpeed.x *= -1;
- batch.begin();
- batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
- batch.end();
- hitmarker.play();
- }
- int rng2 = random.nextInt(2);
- if (ballPosition.y <= paddlePosition.y + paddleTexture.getHeight() && (ballPosition.x >= paddlePosition.x && ballPosition.x <= paddlePosition.x + paddleTexture.getWidth())&& ballSpeed.y < 0){
- if (rng2 == 0){
- tr.play();
- }
- else if (rng2 == 1){
- ns.play();
- }
- ballPosition.y = paddlePosition.y + paddleTexture.getHeight();
- ballSpeed.y *= -1;
- batch.begin();
- batch.draw(hitmarkerTexture, ballPosition.x - ballTexture.getWidth(), ballPosition.y);
- batch.end();
- hitmarker.play();
- }
- }
- public void checkPaddle(){
- if (paddlePosition.x < 0){
- paddlePosition.x = 0;
- }
- if (paddlePosition.x + paddleTexture.getWidth() > screenWidth){
- paddlePosition.x = screenWidth - paddleTexture.getWidth();
- }
- }
- public void hideCursor() {
- if (Gdx.input.isCursorCatched() && Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
- Gdx.input.setCursorCatched(false);
- }
- else if (!(Gdx.input.isCursorCatched()) && Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
- Gdx.input.setCursorCatched(true);
- }
- }
- @Override
- public void render () {
- Gdx.gl.glClearColor(0, 0, 0, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- batch.begin();
- batch.draw(woodTexture, 0, 0);
- batch.draw(paddleTexture, paddlePosition.x, paddlePosition.y);
- batch.draw(ballTexture, ballPosition.x, ballPosition.y, ballWidth, ballHeight);
- for (int i = 0; i < dewList.size; i++){
- Dew currentDew = dewList.get(i);
- batch.draw(dewTexture, currentDew.x, currentDew.y, 75, 100);
- }
- batch.end();
- fps.log();
- updateInput();
- checkPaddle();
- checkBallCollision();
- hideCursor();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement