Created
March 9, 2016 21:20
-
-
Save EdwardDowling/a6addbeb6fb15d4a8eab to your computer and use it in GitHub Desktop.
Pong
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mygdx.game; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.Input; | |
import com.badlogic.gdx.InputProcessor; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.g2d.Sprite; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.math.Vector2; | |
public class MyGdxGame extends ApplicationAdapter{ | |
SpriteBatch batch; | |
Texture texture; | |
Sprite Ball; | |
Sprite Paddle; | |
Sprite Paddle2; | |
int PaddleSpeed; | |
float Paddle2Speed; | |
Vector2 ballVelocity; | |
Sprite[] Sprites; | |
@Override | |
public void create () { | |
batch = new SpriteBatch(); | |
texture = new Texture("ball.png"); | |
Ball = new Sprite(texture); | |
Ball.setSize(16,16); | |
Ball.setPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2); | |
Paddle = new Sprite(texture); | |
Paddle.setPosition(0, Paddle.getHeight()/2); | |
Paddle.setSize(8,128); | |
Paddle2 = new Sprite(texture); | |
Paddle2.setPosition(Gdx.graphics.getWidth() - Paddle.getWidth(),0); | |
Paddle2.setSize(8,128); | |
ballVelocity = new Vector2(-3,1); | |
Sprites = new Sprite[]{Ball,Paddle,Paddle2}; | |
} | |
@Override | |
public void render () { | |
Gdx.gl.glClearColor(0, 0, 0, 0); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
updateBall(); | |
updatePaddle(); | |
updatePaddle2(); | |
batch.begin(); | |
for(int i = 0;i < Sprites.length;i++){ | |
Sprites[i].draw(batch); | |
} | |
batch.end(); | |
} | |
void updatePaddle(){ | |
if(Gdx.input.isKeyPressed(Input.Keys.A)){ | |
PaddleSpeed = 3; | |
} | |
if(Gdx.input.isKeyPressed(Input.Keys.Z)){ | |
PaddleSpeed = -3; | |
} | |
if(!Gdx.input.isKeyPressed(Input.Keys.A) && !Gdx.input.isKeyPressed(Input.Keys.Z)) | |
PaddleSpeed = 0; | |
Paddle.setY(Paddle.getY() + PaddleSpeed); | |
} | |
void updateBall(){ | |
if(overlapPaddle()){ | |
ballVelocity.x *= -1; | |
ballVelocity.x += 1/ballVelocity.x; | |
} | |
Ball.setPosition(Ball.getX() + ballVelocity.x,Ball.getY() + ballVelocity.y); | |
if(Ball.getY() + ballVelocity.y < 0 || Ball.getY() + ballVelocity.y > Gdx.graphics.getHeight()){ | |
ballVelocity.y *= -1; | |
} | |
if(Ball.getX() < 0 || Ball.getX() + Ball.getWidth() > Gdx.graphics.getWidth()){ | |
Ball.setPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2); | |
ballVelocity.x = -3; | |
} | |
if(ballVelocity.y > 5 )ballVelocity.y--; | |
} | |
boolean overlapPaddle(){ | |
if(Ball.getX() <= Paddle.getWidth()){ | |
if(Ball.getY() + Ball.getHeight() >= Paddle.getY() && Ball.getY() <= Paddle.getY() + Paddle.getHeight()){ | |
ballVelocity.y += PaddleSpeed/2; | |
return true; | |
} | |
} else if(Ball.getX() + Ball.getWidth() + ballVelocity.x >= Gdx.graphics.getWidth() - Paddle2.getWidth()){ | |
if(Ball.getY() + Ball.getHeight() >= Paddle2.getY() && Ball.getY() <= Paddle2.getY() + Paddle2.getHeight()){ | |
ballVelocity.y += Paddle2Speed/2; | |
return true; | |
} | |
} | |
return false; | |
} | |
void updatePaddle2(){ | |
if(Ball.getY() +3 >= Paddle2.getY() + Paddle2.getHeight()/2){ | |
Paddle2Speed = 0.1f*((Ball.getY() + Ball.getHeight()/2)-(Paddle2.getY() + Paddle2 .getHeight()/2)); | |
}else if( Ball.getY() -3 <= Paddle2.getY() + Paddle2.getHeight()/2){ | |
Paddle2Speed = 0.1f*((Ball.getY() + Ball.getHeight()/2)-(Paddle2.getY() + Paddle2 .getHeight()/2)); | |
} | |
Paddle2.setPosition(Paddle2.getX(),Paddle2.getY() + Paddle2Speed); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment