Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package is.ru.tgra;
- import java.awt.geom.Point2D;
- import java.nio.FloatBuffer;
- import java.util.Vector;
- import com.badlogic.gdx.ApplicationListener;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.GL11;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.utils.BufferUtils;
- import java.nio.FloatBuffer;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Vector;
- import java.math.*;
- public class Skil1 implements ApplicationListener {
- //Board:
- ObjectReference boxxy; //IS BOXXY YOU SEEEEE!
- float boxxy_x = 320;
- float boxxy_y = 30;
- float boxxy_height = 30;
- float boxxy_length = 200;
- //Bricks:
- ObjectReference brick; //IS BRICKS YOU SEEEE!
- float brick_x = 40;
- float brick_y = 450;
- float brick_length = 50;
- float brick_height = 30;
- ArrayList<float[]> brickLocation = new ArrayList<float[]>();
- int delayCounter = 0;
- int bounceCounter = 0;
- //Ball:
- ObjectReference ball;
- float ball_radius = 10;
- float ball_x = 320;
- float ball_y = 100;
- float ball_speed = 250;
- boolean ball_up = false;
- boolean ball_right =true;
- //World:
- float world_left;
- float world_right;
- float world_bottom;
- float world_top;
- //Collision variables
- double NW_corner = 0;
- double NE_corner = 0;
- double SW_corner = 0;
- double SE_corner = 0;
- boolean noDelay = true;
- boolean canBounce = true;
- //is the game running?
- boolean running = false;
- boolean gameOver = false;
- //Colours
- float[] brickColours = new float[3];
- float[] ballColours = new float[3];
- private Random random = new Random();
- //lives
- int lives = 4;
- //text
- private SpriteBatch spriteBatch;
- private BitmapFont font;
- //buffer
- FloatBuffer vertexBuffer;
- public Skil1() {
- // TODO Auto-generated constructor stub
- }
- @Override
- public void create() {
- spriteBatch = new SpriteBatch();
- font = new BitmapFont();
- Gdx.gl11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
- Gdx.graphics.getDeltaTime();
- Vector<Point2D> vertexList = new Vector<Point2D>();
- boxxy = createBox(boxxy_length, 20, vertexList);
- brick = createBox(50,30,vertexList);
- ball = createCircle(ball_radius, 39, vertexList);
- int floatBufferSize = vertexList.size() * 2;
- vertexBuffer = BufferUtils.newFloatBuffer(floatBufferSize);
- float[] array = new float[floatBufferSize];
- for(int i = 0; i < vertexList.size(); i++)
- {
- array[i*2] = vertexList.get(i).x;
- array[i*2+1] = vertexList.get(i).y;
- }
- vertexBuffer.put(array);
- vertexBuffer.rewind();
- Gdx.gl11.glVertexPointer(2, GL11.GL_FLOAT, 0, vertexBuffer);
- brickLocations();
- for (int i = 0; i < 3; i++)
- {
- brickColours[i] = 1.0f;
- ballColours[i] = 1.0f;
- }
- }
- @Override
- public void dispose() {
- // TODO Auto-generated method stub
- }
- @Override
- public void pause() {
- // TODO Auto-generated method stub
- }
- @Override
- public void render() {
- display();
- update();
- }
- private void ballTravel()
- {
- if (!gameOver)
- {
- float deltaTime = Gdx.graphics.getDeltaTime();
- if(ball_right == true){
- ball_x += ball_speed*deltaTime;
- if(ball_x > Gdx.graphics.getWidth()-ball_radius)
- ball_right = false;
- }
- if(ball_right ==false){
- ball_x -= ball_speed*deltaTime;
- if(ball_x < ball_radius)
- ball_right = true;
- }
- if(ball_up == true){
- ball_y += ball_speed*deltaTime;
- if(ball_y > Gdx.graphics.getHeight()-ball_radius)
- ball_up = false;
- }
- if(ball_up == false){
- ball_y -= ball_speed*deltaTime;
- if(ball_y < ball_radius && lives == 1)
- {
- gameOver = true;
- running = false;
- System.out.println("Game Over");
- }
- else if(ball_y < ball_radius)
- {
- lives -= 1;
- restartGame();
- }
- }
- ball_speed += 0.05;
- }
- }
- private void restartGame()
- {
- boxxy_x = 320;
- boxxy_y = 30;
- //Bricks:
- brick_x = 40;
- brick_y = 450;
- //Ball:
- ball_x = 320;
- ball_y = 100;
- ball_speed = 250;
- ball_up = false;
- ball_right =true;
- running = false;
- }
- private void startOver()
- {
- boxxy_x = 320;
- boxxy_y = 30;
- //Bricks:
- brick_x = 40;
- brick_y = 450;
- //Ball:
- ball_x = 320;
- ball_y = 100;
- ball_speed = 250;
- ball_up = false;
- ball_right =true;
- lives = 4;
- running = false;
- gameOver = false;
- brickLocation.clear();
- brickLocations();
- }
- private void ballCollision()
- {
- //The delay checker. We want to have a slight delay between bouncing off bricks, we can't bump off bricks unless noDelay == true
- if (!noDelay)
- {
- delayCounter++;
- if (delayCounter == 2)
- {
- delayCounter = 0;
- noDelay = true;
- }
- }
- //The delay checker for the board. We need a slight delay after the ball hits the board since otherwise the ball freaks out
- //after hitting a corner of the board if we're moving the board towards the ball
- if (!canBounce)
- {
- bounceCounter++;
- if (bounceCounter == 50)
- {
- bounceCounter = 0;
- canBounce = true;
- }
- }
- //Board collision
- //The formula for calculating whether or not we've hit one of the upper corners of the board
- NW_corner = Math.sqrt(
- ((ball_x - (boxxy_x - boxxy_length/2)) * (ball_x - (boxxy_x - boxxy_length/2)))
- + ((ball_y - (boxxy_y + boxxy_height/2)) * (ball_y - (boxxy_y + boxxy_height/2)))
- );
- NE_corner = Math.sqrt(
- ((ball_x - (boxxy_x + boxxy_length/2)) * (ball_x - (boxxy_x + boxxy_length/2)))
- + ((ball_y - (boxxy_y + boxxy_height/2)) * (ball_y - (boxxy_y + boxxy_height/2)))
- );
- if ((NW_corner <= ball_radius/2
- || NE_corner <= ball_radius/2)
- && canBounce)
- {
- //If we hit a corner of the board
- ball_right = !ball_right;
- ball_up = !ball_up;
- canBounce = false;
- }
- else if (ball_x + ball_radius >= boxxy_x - boxxy_length/2 && ball_x + ball_radius < boxxy_x + boxxy_length/2)
- {
- if (ball_x < boxxy_x - boxxy_length/2 && ball_x > boxxy_x + boxxy_length/2)
- {
- if (((ball_y + ball_radius <= boxxy_y + boxxy_height/2 && ball_y + ball_radius >= boxxy_y - boxxy_height/2)
- || (ball_y - ball_radius >= boxxy_y - boxxy_height/2 && ball_y - ball_radius <= boxxy_y + boxxy_height/2))
- && canBounce)
- {
- //Ball has hit the left side of the board
- ball_right = !ball_right;
- canBounce = false;
- }
- }
- else if (ball_y - ball_radius <= boxxy_y + boxxy_height/2 && ball_y - ball_radius >= boxxy_y- boxxy_height/2 && canBounce)
- {
- //Bottom of ball has hit the top side of the board, coming from the left
- ball_up = !ball_up;
- canBounce = false;
- }
- }
- else if (ball_x - ball_radius > boxxy_x - boxxy_length/2 && ball_x - ball_radius <= boxxy_x + boxxy_length/2)
- {
- if (ball_x < boxxy_x - boxxy_length/2 && ball_x > boxxy_x + boxxy_length/2)
- {
- if (((ball_y + ball_radius <= boxxy_y + boxxy_height/2 && ball_y + ball_radius >= boxxy_y - boxxy_height/2)
- || (ball_y - ball_radius >= boxxy_y - boxxy_height/2 && ball_y - ball_radius <= boxxy_y + boxxy_height/2))
- && canBounce )
- {
- //Ball has hit the right side of the board
- ball_right = !ball_right;
- canBounce = false;
- }
- }
- else if (ball_y - ball_radius <= boxxy_y + boxxy_height/2 && ball_y - ball_radius >= boxxy_y - boxxy_height/2 && canBounce)
- {
- //Bottom of ball has hit the top side of the board, coming from the right
- ball_up = !ball_up;
- canBounce = false;
- }
- }
- //Brick collision
- for(float[] locations : brickLocation){ //For all bricks
- if(locations[2] ==1) //That have not been destroyed yet
- { //Same formula as above, calculate all corners of the brick in question
- NW_corner = Math.sqrt((ball_x - (locations[0] - brick_length/2)) * (ball_x - (locations[0] - brick_length/2))
- + (ball_y - (locations[1] + brick_height/2)) * (ball_y - (locations[1] + brick_height/2)));
- NE_corner = Math.sqrt((ball_x - (locations[0] + brick_length/2)) * (ball_x - (locations[0] + brick_length/2))
- + (ball_y - (locations[1] + brick_height/2)) * (ball_y - (locations[1] + brick_height/2)));
- SW_corner = Math.sqrt((ball_x - (locations[0] - brick_length/2)) * (ball_x - (locations[0] - brick_length/2))
- + (ball_y - (locations[1] - brick_height/2)) * (ball_y - (locations[1] - brick_height/2)));
- SE_corner = Math.sqrt((ball_x - (locations[0] + brick_length/2)) * (ball_x - (locations[0] + brick_length/2))
- + (ball_y - (locations[1] - brick_height/2)) * ((ball_y - locations[1] - brick_height/2)));
- if ((NW_corner <= ball_radius/2
- || NE_corner <= ball_radius/2
- || SW_corner <= ball_radius/2
- || SE_corner <= ball_radius/2)
- && noDelay )
- {
- if(locations[3] > 1)
- locations[3] -= 1;
- else
- locations[2] = 0;
- //If we hit a corner
- ball_right = !ball_right;
- ball_up = !ball_up;
- ball_speed += 1;
- noDelay = false;
- changeColour();
- }//If right side of ball is within the brick's x-coordinates
- else if (ball_x + ball_radius >= locations[0] - brick_length/2 && ball_x + ball_radius < locations[0] + brick_length/2)
- { //If the center of the ball is not within the brick
- if (ball_x < locations[0] - brick_length/2 || ball_x > locations[0] + brick_length/2)
- { //If either the top or the bot of the ball is within the y-cords of the brick
- if ((ball_y + ball_radius <= locations[1] + brick_height/2 && ball_y + ball_radius >= locations[1] - brick_height/2)
- || (ball_y - ball_radius >= locations[1] - brick_height/2 && ball_y - ball_radius <= locations[1] + brick_height/2)
- && noDelay)
- {
- //Ball has hit the left side of the box
- ball_right = !ball_right;
- if(locations[3] > 1)
- locations[3] -= 1;
- else
- locations[2] = 0;
- ball_speed += 1;
- noDelay = false;
- changeColour();
- }
- } //If the bottom of the ball is within the brick
- else if (ball_y - ball_radius <= locations[1] + brick_height/2 && ball_y - ball_radius >= locations[1] - brick_height/2 && noDelay)
- {
- //Bottom of ball has hit the top side of the box
- ball_up = !ball_up;
- if(locations[3] > 1)
- locations[3] -= 1;
- else
- locations[2] = 0;
- ball_speed += 1;
- noDelay = false;
- changeColour();
- } //If the top of the ball is within the brick
- else if (ball_y + ball_radius >= locations[1] - brick_height/2 && ball_y + ball_radius <= locations[1] + brick_height/2 && noDelay)
- {
- //Top of ball has hit the bottom of the box
- ball_up = !ball_up;
- if(locations[3] > 1)
- locations[3] -= 1;
- else
- locations[2] = 0;
- ball_speed += 1;
- noDelay = false;
- changeColour();
- }
- } //If the left side of the ball is within the brick's x-coordinates
- else if (ball_x - ball_radius > locations[0] - brick_length/2 && ball_x - ball_radius <= locations[0] + brick_length/2)
- { //If the center of the ball is not within the brick
- if (ball_x < locations[0] - brick_length/2 || ball_x > locations[0] + brick_length/2)
- { //If either the top or the bot of the ball is within the y-cords of the brick
- if ((ball_y + ball_radius <= locations[1] + brick_height/2 && ball_y + ball_radius >= locations[1] - brick_height/2)
- || (ball_y - ball_radius >= locations[1] - brick_height/2 && ball_y - ball_radius <= locations[1] + brick_height/2) && noDelay)
- {
- //Ball has hit the right side of the box
- ball_right = !ball_right;
- if(locations[3] > 1)
- locations[3] -= 1;
- else
- locations[2] = 0;
- ball_speed += 1;
- noDelay = false;
- changeColour();
- }
- } //If the bottom of the ball is within the brick
- else if (ball_y - ball_radius <= locations[1] + brick_height/2 && ball_y - ball_radius >= locations[1] - brick_height/2 && noDelay)
- {
- //Bottom of ball has hit the top side of the box
- ball_up = !ball_up;
- if(locations[3] > 1)
- locations[3] -= 1;
- else
- locations[2] = 0;
- ball_speed += 1;
- noDelay = false;
- changeColour();
- } //If the top of the ball is within the brick
- else if (ball_y + ball_radius >= locations[1] - brick_height/2 && ball_y + ball_radius <= locations[1] + brick_height/2 && noDelay)
- {
- //Top of ball has hit the bottom of the box
- ball_up = !ball_up;
- if(locations[3] > 1)
- locations[3] -= 1;
- else
- locations[2] = 0;
- ball_speed += 1;
- noDelay = false;
- changeColour();
- }
- }
- }
- isTheGameOver();
- }
- }
- private void isTheGameOver()
- {
- gameOver = true;
- for(float[] locations : brickLocation)
- {
- if(lives == 1)
- {
- break;
- }
- if (locations[2] != 0)
- {
- gameOver = false;
- break;
- }
- }
- }
- private void update(){
- if(running)
- ballTravel();
- if(Gdx.input.isKeyPressed(Input.Keys.SPACE)){
- running = true;
- }
- if(gameOver == true){
- if(Gdx.input.isKeyPressed(Input.Keys.ENTER)){
- startOver();
- }
- }
- ballCollision();
- if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
- if(boxxy_x > boxxy_length/2)
- boxxy_x = boxxy_x -5;
- }
- if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
- if(boxxy_x < Gdx.graphics.getWidth()-boxxy_length/2)
- boxxy_x = boxxy_x +5;
- }
- world_left = 0;
- world_right = Gdx.graphics.getWidth();
- world_bottom = 0;
- world_top = Gdx.graphics.getHeight();
- }
- private void drawObject(ObjectReference or)
- {
- Gdx.gl11.glDrawArrays(or.openGLPrimitiveType, or.firstIndex, or.vertexCount);
- }
- private ObjectReference createCircle(float radius, int slices, Vector<Point2D> vertexList) {
- ObjectReference or = new ObjectReference(vertexList.size(), slices, GL11.GL_TRIANGLE_FAN);
- for(float f = 0; f < 2*Math.PI; f += (float)2*Math.PI / (float)slices) {
- vertexList.add(new Point2D((float)Math.cos(f) * radius, (float)Math.sin(f) * radius));
- }
- return or;
- }
- private void display(){
- Gdx.gl11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- Gdx.gl11.glClear(GL11.GL_COLOR_BUFFER_BIT);
- Gdx.gl11.glMatrixMode(GL11.GL_MODELVIEW);
- Gdx.gl11.glLoadIdentity();
- Gdx.glu.gluOrtho2D(Gdx.gl10, world_left, world_right, world_bottom, world_top);
- if(!gameOver)
- {
- int currentlives = lives -1;
- spriteBatch.begin();
- font.setColor(1, 1, 1, 1f);
- font.draw(spriteBatch, String.format("Lives: " + currentlives), 10, 20);
- spriteBatch.end();
- }
- else
- {
- spriteBatch.begin();
- font.setColor(1, 1, 1, 1f);
- font.draw(spriteBatch, String.format("GAME OVER "), 260, 200);
- font.draw(spriteBatch, String.format("To start a new game press Enter "), 210, 185);
- spriteBatch.end();
- }
- Gdx.gl11.glMatrixMode(GL11.GL_MODELVIEW);
- Gdx.gl11.glLoadIdentity();
- Gdx.gl11.glVertexPointer(2, GL11.GL_FLOAT, 0, vertexBuffer);
- Gdx.gl11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
- Gdx.gl11.glPushMatrix();
- Gdx.gl11.glTranslatef(boxxy_x, boxxy_y, 0);
- drawObject(boxxy);
- Gdx.gl11.glPopMatrix();
- Gdx.gl11.glPushMatrix();
- Gdx.gl11.glColor4f(ballColours[0], ballColours[1], ballColours[2], 1f);
- Gdx.gl11.glTranslatef(ball_x, ball_y, 0);
- drawObject(ball);
- Gdx.gl11.glPopMatrix();
- for(float[] locations : brickLocation){
- if(locations[2] == 1)
- {
- if(locations[3] == 2) //Is it a hardbrick
- {
- Gdx.gl11.glPushMatrix();
- Gdx.gl11.glColor4f(0.0f, 0.0f, 0.93f, 1);
- Gdx.gl11.glTranslatef(locations[0],locations[1], 0);
- drawObject(brick);
- Gdx.gl11.glPopMatrix();
- }
- else
- {
- Gdx.gl11.glPushMatrix();
- Gdx.gl11.glColor4f(brickColours[0], brickColours[1], brickColours[2], 1f);
- Gdx.gl11.glTranslatef(locations[0],locations[1], 0);
- drawObject(brick);
- Gdx.gl11.glPopMatrix();
- }
- }
- }
- }
- private void changeColour()
- {
- float sumBricks = 0;
- float sumBall = 0;
- for (int i = 0; i < 3; i++)
- {
- brickColours[i] = this.random.nextFloat();
- ballColours[i] = this.random.nextFloat();
- sumBricks += brickColours[i];
- sumBall += ballColours[i];
- }
- sumBricks = sumBricks/3;
- sumBall = sumBall/3;
- if (sumBricks < 0.5 || sumBall < 0.5) //Check whether either of the colours is too dark against the black background
- {
- changeColour(); //If so, we change the colours again
- }
- }
- private void brickLocations()
- {
- boolean makeHardBricks = true;
- for(float x = brick_x; x <= 590; x+=55)
- {
- for(float y= brick_y; y >= 300; y-=35)
- {
- if(makeHardBricks)
- {
- //The array contains 3 elements, x, y and destroyed boolean variable.
- float[] pos = {x, y, 1, 2};
- brickLocation.add(pos);
- }
- else
- {
- float[] pos = {x, y, 1, -1};
- brickLocation.add(pos);
- }
- makeHardBricks = !makeHardBricks;
- }
- makeHardBricks = !makeHardBricks;
- }
- }
- @Override
- public void resize(int arg0, int arg1) {
- // TODO Auto-generated method stub
- }
- @Override
- public void resume() {
- // TODO Auto-generated method stub
- }
- private ObjectReference createBox(float width, float height,Vector<Point2D> vertexList)
- {
- ObjectReference or = new ObjectReference(vertexList.size(), 4,
- GL11.GL_TRIANGLE_STRIP);
- vertexList.add(new Point2D(-width/2.0f, -height/2.0f));
- vertexList.add(new Point2D(-width/2.0f, height/2.0f));
- vertexList.add(new Point2D(width/2.0f, -height/2.0f));
- vertexList.add(new Point2D(width/2.0f, height/2.0f));
- return or;
- }
- public class Point2D
- {
- public float x;
- public float y;
- public Point2D(float xx, float yy)
- {
- x = xx;
- y = yy;
- }
- }
- public class ObjectReference
- {
- public int firstIndex;
- public int vertexCount;
- public int openGLPrimitiveType;
- public ObjectReference(int fi, int vc, int pt)
- {
- firstIndex = fi;
- vertexCount = vc;
- openGLPrimitiveType = pt;
- }
- }
- }
Add Comment
Please, Sign In to add comment