package com.humborstad.GDXTest;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
public class GDXTest implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture ballTexture;
private Sprite ballSprite;
private Texture paddleTexture;
private Sprite[] paddleSprite = new Sprite[2];
private Rectangle ballRectangle;
private Rectangle[] paddleRectangle = new Rectangle[2];
//We're twoplayers
private int[] player = new int[2];
private float ballXMoveSpeed = 300;
private float ballYMoveSpeed = 300;
private float w;
private float h;
private FPSLogger log = new FPSLogger();
@Override
public void create() {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, w, h);
batch = new SpriteBatch();
ballTexture = new Texture(Gdx.files.internal("img/ball.png"));
ballTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
paddleTexture = new Texture(Gdx.files.internal("img/paddle.png"));
paddleTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion ballRegion = new TextureRegion(ballTexture, 0, 0, 16, 16);
TextureRegion paddleRegion = new TextureRegion(paddleTexture, 0, 0, 16, 64);
ballSprite = new Sprite(ballRegion);
//ballSprite.setSize(0.9f, 0.9f * ballSprite.getHeight() / ballSprite.getWidth());
// create a Rectangle to logically represent the bucket
ballRectangle = new Rectangle();
ballRectangle.x = w/2; // center the bucket horizontally
ballRectangle.y = h/2; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
ballRectangle.width = 16;
ballRectangle.height = 16;
ballSprite.setOrigin(ballSprite.getWidth()/2, ballSprite.getHeight()/2);
ballSprite.setPosition(ballRectangle.x, ballRectangle.y);
paddleSprite[0] = new Sprite(paddleRegion);
paddleSprite[1] = new Sprite(paddleRegion);
//ballSprite.setSize(0.9f, 0.9f * ballSprite.getHeight() / ballSprite.getWidth());
// create a Rectangle to logically represent the bucket
paddleRectangle[0] = new Rectangle();
paddleRectangle[0].width = 16;
paddleRectangle[0].height = 64;
paddleRectangle[0].x = 30; // center the bucket horizontally
paddleRectangle[0].y = h/2; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
paddleRectangle[1] = new Rectangle();
paddleRectangle[1].width = 16;
paddleRectangle[1].height = 64;
paddleRectangle[1].x = w - 30 - paddleRectangle[1].width; // center the bucket horizontally
paddleRectangle[1].y = h/2; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
paddleSprite[0].setOrigin(paddleSprite[0].getWidth()/2, paddleSprite[0].getHeight()/2);
paddleSprite[0].setPosition(paddleRectangle[0].x, paddleRectangle[0].y);
for(int i = 0; i < player.length; i++){
player[i] = 0;
}
}
@Override
public void dispose() {
batch.dispose();
ballTexture.dispose();
paddleTexture.dispose();
}
@Override
public void render() {
// clear the screen with a dark blue color. The
// arguments to glClearColor are the red, green
// blue and alpha component in the range [0,1]
// of the color to be used to clear the screen.
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// tell the camera to update its matrices.
camera.update();
// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
batch.setProjectionMatrix(camera.combined);
if(Gdx.input.isKeyPressed(Keys.S)) paddleRectangle[0].y -= 400 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.W)) paddleRectangle[0].y += 400 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.DOWN)) paddleRectangle[1].y -= 400 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Keys.UP)) paddleRectangle[1].y += 400 * Gdx.graphics.getDeltaTime();
//Uh, yay for readability.
for(Rectangle i : paddleRectangle){
if(i.y < 0) i.y = 0;
if(i.y > h - i.height) i.y = h - i.height;
}
if(ballRectangle.x + ballRectangle.width < 0 ){
player[1]++;
ballRectangle.x = w - w/4;
//ballXMoveSpeed =- ballXMoveSpeed;
} else if(ballRectangle.x + ballRectangle.width > w){
player[0]++;
ballRectangle.x = w/4;
//ballXMoveSpeed =- ballXMoveSpeed;
} else if (ballRectangle.y < 0 || ballRectangle.y + ballRectangle.height > h){
ballYMoveSpeed =- ballYMoveSpeed;
}
for(Rectangle i : paddleRectangle){
if(ballRectangle.overlaps(i)){
ballXMoveSpeed =- ballXMoveSpeed;
}
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
ballSprite.setPosition(ballRectangle.x, ballRectangle.y);
ballSprite.draw(batch);
//This is a bit sketchy. We don't certainly know if paddleSprite.length == paddleRectangle.length
for(int i = 0; i < paddleSprite.length; i ++){
BitmapFont font = new BitmapFont();
font.draw(batch, String.valueOf(player[1]), w - 50, h - 50);
font.draw(batch, String.valueOf(player[0]), 50, h - 50);
paddleSprite[i].setPosition(paddleRectangle[i].x, paddleRectangle[i].y);
paddleSprite[i].draw(batch);
}
log.log();
batch.end();
ballRectangle.x += ballXMoveSpeed * Gdx.graphics.getDeltaTime();
ballRectangle.y += ballYMoveSpeed * Gdx.graphics.getDeltaTime();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
Comments
0 B
|👍
/👎