Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zaidazadkiel.phong;
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.utils.TimeUtils;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import java.awt.*;
- import java.util.Random;
- public class libGdxGame extends ApplicationAdapter {
- SpriteBatch batch;
- Texture img;
- OrthographicCamera camera;
- int screenHeight;
- int screenWidth;
- float ballX = 100.0f;
- float ballY = 100.0f;
- float ballSpeedX = 1.50f;
- float ballSpeedY = 1.50f;
- int paddleX = 100;
- BitmapFont font;
- ShapeRenderer sr;
- Rectangle ballRectangle = new Rectangle();
- class Block{
- int x, y, width;
- int points;
- Rectangle pixelRect;
- Block(int x, int y, int width){
- // set position
- points=1;
- this.x = x; this.y = y; this.width = width;
- pixelRect = new Rectangle(x*32, screenHeight-(y*32), 32*width, 32);
- }
- }
- Block blocks[] = new Block[100];
- long frames = 0;
- long lastsecond=0;
- long oldTime=0;
- long fps = 0;
- long getTime(){
- //show the time ellapsed since the last frame (call)
- //60fps = 16ms ?
- long oldold = oldTime;
- // return TimeUtils.timeSinceMillis(oldTime) - oldold;
- oldTime = TimeUtils.millis();
- frames++;
- if(oldTime - lastsecond > 1000) {
- // System.out.println("fps: " + frames + ", " + lastsecond);
- lastsecond=oldTime;
- fps = frames;
- frames=0;
- }
- return oldTime - oldold;
- }
- @Override
- public void create () {
- batch = new SpriteBatch();
- img = new Texture("badlogic.png");
- screenHeight = Gdx.graphics.getHeight();
- screenWidth = Gdx.graphics.getWidth();
- System.out.println("width: " + screenWidth + ", height: " + screenHeight);
- ballX = com.badlogic.gdx.math.MathUtils.random(screenWidth - 60)+30;
- camera = new OrthographicCamera(screenWidth, screenHeight);
- camera.position.set( camera.viewportWidth/2f, camera.viewportHeight/2f, 0);
- camera.update();
- font = new BitmapFont();
- // System.out.println("font is " + font.getLineHeight());
- sr = new ShapeRenderer();
- sr.setColor(Color.YELLOW);
- // sr.setProjectionMatrix(camera.combined);
- int c = 0;
- for(int by = 0; by != 4; by++){
- for(int bx = 0; bx != 20; bx++){
- blocks[c++] = new Block(bx, by, 1);
- //blocks[0] = new Block(by*32 % (screenWidth), screenHeight-((by%20)*32), 1);
- }
- }
- blocks[69] = new Block(10, 4,1);
- }
- public void drawBall() {
- batch.draw(img, ballX, ballY, 0,4*32, 32, 32);
- }
- public void drawBlocks(){
- for(int i = 0; i != blocks.length; i++){
- Block b = blocks[i];
- if(b==null) continue;
- if(b.points>0){
- //magic number: single width block starts at x=4*32
- batch.draw(img, b.pixelRect.x, b.pixelRect.y, 3*32, 0, 32*1, 32);
- // font.draw(batch, ""+i, b.x, b.y);
- }
- }
- }
- public void drawPaddle(){
- batch.draw(img, paddleX-32, 20, 200, 130, 32*3, 15);
- }
- float angle;
- Vector2 ballVec=new Vector2();
- Vector2 blockVec=new Vector2();
- public void calcBall(){
- float time = getTime()/10;
- // new position = speed per seconds / millis elapsed
- ballX+=(ballSpeedX/time);
- ballY+=(ballSpeedY/time);
- if((ballX+32) >= screenWidth || ballX <= 0) ballSpeedX*=-1;
- if((ballY+32) >= screenHeight || ballY <= 0) ballSpeedY*=-1;
- glred=0.2f;
- // if(ballX >= blocks[69].x && ballX <= blocks[69].x+32 ) glred=1.0f
- ballRectangle.x = (int) ballX;
- ballRectangle.y = (int) ballY;
- ballRectangle.width = 32;
- ballRectangle.height = 32;
- for(int i = 0; i < blocks.length; i++) {
- Block b = blocks[i];
- if(b!= null && b.points>0 && b.pixelRect.intersects(ballRectangle) ){
- glred = 0.5f;
- blocks[i].points--;
- float invX = ballSpeedX > 0 ? ballSpeedX+0.1f : ballSpeedX-0.1f;
- float invY = ballSpeedY > 0 ? ballSpeedY+0.1f : ballSpeedY-0.1f;
- //
- // if( (ballX+16) > (b.x+16) ) ballSpeedX = invX;
- // if( (ballY+16) < (b.pixelRect.y+16) ) ballSpeedY = invY;
- // System.out.println(b.x + " , " + b.pixelRect.x);
- // ballVec.set((ballX+16 - b.x+16), (ballY+16 - b.y+16));
- // System.out.println(ballVec);
- blockVec.set(b.pixelRect.x+16, b.pixelRect.y+16);
- ballVec .set(ballX+16, ballY+16);
- ballVec.sub(blockVec);
- System.out.println("block: " + blockVec);
- System.out.println("ball: " + ballVec);
- ballSpeedY= ballVec.x < 0 ? -invY : invY;
- ballSpeedX= ballVec.y < 0 ? -invX : invX;
- // ballSpeedY=blockVec.y/10;
- // ballSpeedX=blockVec.x/10;
- // angle = ballVec.angle();//angleRad(blockVec);
- // System.out.println("angle: " + angle);
- //// ballVec.set(ballSpeedX, ballSpeedY);
- //// ballVec.setAngleRad(angle);
- // System.out.println("x: " + (ballX-ballVec.x) + ", y: " + (ballY-ballVec.y) );
- // ballSpeedX = ballVec.x;
- // ballSpeedY = ballVec.y;
- // float invX = ballVec.x < 0 ? ballVec.x+0.1f : ballVec.x-0.1f;
- // float invY = ballVec.y < 0 ? ballVec.y+0.1f : ballVec.y-0.1f;
- // ballSpeedY=invY;
- // ballSpeedX=invY;
- // ballSpeedY = ballVec.y; ballSpeedX=ballVec.x;
- // if(angle > -20f){
- // ballSpeedY = (ballY+16.0f) > b.y+16 ? -invY : ballSpeedY+0.1f;
- // }
- // ballSpeedX = (ballX+16.0f) > b.x+16 ? -invX : ballSpeedX+0.1f;
- // ballSpeedY = (ballY+16.0f) > b.y+16 ? -invY : ballSpeedY+0.1f;
- break;
- }
- }
- }
- float glred = 0.2f;
- void calcPaddle(){
- // if(ballX + 16 > paddleX) paddleX = (int)ballX + 16;
- // if(ballX + 16 < paddleX)
- paddleX = (int)ballX + 16;
- if(ballY + ballSpeedY <= 20.0f) ballSpeedY = -ballSpeedY;
- }
- @Override
- public void render () {
- calcBall();
- calcPaddle();
- // Gdx.gl.glClearColor(glred, 0.2f, 0.2f, 0.1f);
- // Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- Gdx.gl.glEnable(GL20.GL_BLEND);
- Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
- sr.setColor(0f,0f,0f,0.25f);
- sr.setProjectionMatrix(camera.combined);
- sr.begin(ShapeRenderer.ShapeType.Filled);
- sr.rect(0,0,screenWidth,screenHeight);
- // sr.setColor(0f, 1f, 1f, 1f);
- // sr.rect( blocks[69].pixelRect.x ,blocks[69].pixelRect.y, blocks[69].pixelRect.width, blocks[69].pixelRect.height);
- // sr.rect( ballRectangle.x, ballRectangle.y, ballRectangle.width, ballRectangle.height); //ballX, ballY, 32, 63);
- // ballVec.set(ballSpeedX*10,ballSpeedY*10);
- // ballVec.rotateRad(angle);
- // sr.rectLine(ballX+16.0f, ballY+16.0f, ballX+16.0f+ballVec.x, ballY+16.0f+ballVec.y, 1);
- sr.end();
- batch.begin();
- drawBlocks();
- drawBall();
- drawPaddle();
- font.draw(batch, "fps: " + fps, 0, screenHeight);
- // font.draw(batch, "bally" + ballSpeedY, 0, screenHeight-19);
- // font.draw(batch, "x" + ballX, 150, screenHeight);
- // font.draw(batch, "y" + ballY, 150, screenHeight-19);
- batch.end();
- }
- @Override
- public void dispose () {
- batch.dispose();
- img.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement