Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.07 KB | None | 0 0
  1. package com.zaidazadkiel.phong;
  2.  
  3. import com.badlogic.gdx.ApplicationAdapter;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.Color;
  6. import com.badlogic.gdx.graphics.GL20;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.graphics.Texture;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  11. import com.badlogic.gdx.math.Vector2;
  12. import com.badlogic.gdx.utils.TimeUtils;
  13. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  14.  
  15. import java.awt.*;
  16. import java.util.Random;
  17.  
  18. public class libGdxGame extends ApplicationAdapter {
  19.     SpriteBatch batch;
  20.     Texture img;
  21.     OrthographicCamera camera;
  22.  
  23.     int screenHeight;
  24.     int screenWidth;
  25.  
  26.     float ballX      = 100.0f;
  27.     float ballY      = 100.0f;
  28.     float ballSpeedX = 1.50f;
  29.     float ballSpeedY = 1.50f;
  30.     int   paddleX    = 100;
  31.  
  32.     BitmapFont    font;
  33.     ShapeRenderer sr;
  34.     Rectangle     ballRectangle = new Rectangle();
  35.    
  36.     class Block{
  37.         int x, y, width;
  38.         int points;
  39.         Rectangle pixelRect;
  40.         Block(int x, int y, int width){
  41.             // set position
  42.             points=1;
  43.             this.x = x; this.y = y; this.width = width;
  44.             pixelRect = new Rectangle(x*32, screenHeight-(y*32), 32*width, 32);
  45.         }
  46.     }
  47.     Block blocks[] = new Block[100];
  48.  
  49.     long frames = 0;
  50.     long lastsecond=0;
  51.  
  52.     long oldTime=0;
  53.     long fps = 0;
  54.     long getTime(){
  55.         //show the time ellapsed since the last frame (call)
  56.         //60fps = 16ms ?
  57.         long oldold = oldTime;
  58. //      return TimeUtils.timeSinceMillis(oldTime) - oldold;
  59.         oldTime = TimeUtils.millis();
  60.  
  61.         frames++;
  62.         if(oldTime - lastsecond > 1000) {
  63. //          System.out.println("fps: " + frames + ", " + lastsecond);
  64.             lastsecond=oldTime;
  65.             fps = frames;
  66.             frames=0;
  67.         }
  68.         return oldTime - oldold;
  69.     }
  70.  
  71.     @Override
  72.     public void create () {
  73.         batch = new SpriteBatch();
  74.         img   = new Texture("badlogic.png");
  75.         screenHeight = Gdx.graphics.getHeight();
  76.         screenWidth  = Gdx.graphics.getWidth();
  77.         System.out.println("width: " + screenWidth + ", height: " + screenHeight);
  78.  
  79.         ballX = com.badlogic.gdx.math.MathUtils.random(screenWidth - 60)+30;
  80.  
  81.         camera = new OrthographicCamera(screenWidth, screenHeight);
  82.         camera.position.set( camera.viewportWidth/2f, camera.viewportHeight/2f, 0);
  83.         camera.update();
  84.  
  85.         font = new BitmapFont();
  86. //      System.out.println("font is " + font.getLineHeight());
  87.         sr = new ShapeRenderer();
  88.         sr.setColor(Color.YELLOW);
  89. //      sr.setProjectionMatrix(camera.combined);
  90.  
  91.  
  92.  
  93.         int c = 0;
  94.         for(int by = 0; by != 4; by++){
  95.             for(int bx = 0; bx != 20; bx++){
  96.                 blocks[c++] = new Block(bx, by, 1);
  97.                 //blocks[0] = new Block(by*32 % (screenWidth), screenHeight-((by%20)*32), 1);
  98.             }
  99.         }
  100.         blocks[69] = new Block(10, 4,1);
  101.     }
  102.  
  103.     public void drawBall() {
  104.         batch.draw(img, ballX, ballY, 0,4*32, 32, 32);
  105.     }
  106.  
  107.     public void drawBlocks(){
  108.         for(int i = 0; i != blocks.length; i++){
  109.             Block b = blocks[i];
  110.             if(b==null) continue;
  111.             if(b.points>0){
  112.                 //magic number: single width block starts at x=4*32
  113.                 batch.draw(img, b.pixelRect.x, b.pixelRect.y, 3*32, 0, 32*1, 32);
  114. //              font.draw(batch, ""+i, b.x, b.y);
  115.             }
  116.         }
  117.     }
  118.  
  119.     public void drawPaddle(){
  120.         batch.draw(img, paddleX-32, 20, 200, 130, 32*3, 15);
  121.     }
  122.  
  123.     float angle;
  124.     Vector2 ballVec=new Vector2();
  125.     Vector2 blockVec=new Vector2();
  126.     public void calcBall(){
  127.         float time = getTime()/10;
  128. //      new position = speed per seconds / millis elapsed
  129.  
  130.         ballX+=(ballSpeedX/time);
  131.         ballY+=(ballSpeedY/time);
  132.  
  133.         if((ballX+32) >= screenWidth  || ballX <= 0) ballSpeedX*=-1;
  134.         if((ballY+32) >= screenHeight || ballY <= 0) ballSpeedY*=-1;
  135.  
  136.         glred=0.2f;
  137. //      if(ballX >= blocks[69].x && ballX <= blocks[69].x+32 ) glred=1.0f
  138.         ballRectangle.x = (int) ballX;
  139.         ballRectangle.y = (int) ballY;
  140.         ballRectangle.width  = 32;
  141.         ballRectangle.height = 32;
  142.  
  143.         for(int i = 0; i < blocks.length; i++) {
  144.             Block b = blocks[i];
  145.             if(b!= null && b.points>0 && b.pixelRect.intersects(ballRectangle) ){
  146.                 glred = 0.5f;
  147.                 blocks[i].points--;
  148.                 float invX = ballSpeedX > 0 ? ballSpeedX+0.1f : ballSpeedX-0.1f;
  149.                 float invY = ballSpeedY > 0 ? ballSpeedY+0.1f : ballSpeedY-0.1f;
  150. //
  151. //              if( (ballX+16) > (b.x+16) ) ballSpeedX = invX;
  152. //              if( (ballY+16) < (b.pixelRect.y+16) ) ballSpeedY = invY;
  153. //              System.out.println(b.x + " , " + b.pixelRect.x);
  154. //              ballVec.set((ballX+16 - b.x+16), (ballY+16 - b.y+16));
  155. //              System.out.println(ballVec);
  156.                 blockVec.set(b.pixelRect.x+16, b.pixelRect.y+16);
  157.                 ballVec .set(ballX+16,  ballY+16);
  158.                 ballVec.sub(blockVec);
  159.                 System.out.println("block: " + blockVec);
  160.                 System.out.println("ball: " + ballVec);
  161.  
  162.                 ballSpeedY= ballVec.x < 0 ? -invY : invY;
  163.                 ballSpeedX= ballVec.y < 0 ? -invX : invX;
  164. //              ballSpeedY=blockVec.y/10;
  165. //              ballSpeedX=blockVec.x/10;
  166. //              angle = ballVec.angle();//angleRad(blockVec);
  167. //              System.out.println("angle: " + angle);
  168. ////                ballVec.set(ballSpeedX, ballSpeedY);
  169. ////                ballVec.setAngleRad(angle);
  170. //              System.out.println("x: " + (ballX-ballVec.x) + ", y: " + (ballY-ballVec.y) );
  171. //              ballSpeedX = ballVec.x;
  172. //              ballSpeedY = ballVec.y;
  173.  
  174. //              float invX = ballVec.x < 0 ?  ballVec.x+0.1f : ballVec.x-0.1f;
  175. //              float invY = ballVec.y < 0 ?  ballVec.y+0.1f : ballVec.y-0.1f;
  176. //              ballSpeedY=invY;
  177. //              ballSpeedX=invY;
  178.                 //              ballSpeedY = ballVec.y; ballSpeedX=ballVec.x;
  179. //              if(angle > -20f){
  180. //                  ballSpeedY = (ballY+16.0f) > b.y+16 ? -invY : ballSpeedY+0.1f;
  181. //              }
  182. //                  ballSpeedX = (ballX+16.0f) > b.x+16 ? -invX : ballSpeedX+0.1f;
  183. //                  ballSpeedY = (ballY+16.0f) > b.y+16 ? -invY : ballSpeedY+0.1f;
  184.  
  185.                 break;
  186.             }
  187.         }
  188.  
  189.     }
  190.     float glred = 0.2f;
  191.  
  192.     void calcPaddle(){
  193. //      if(ballX + 16 > paddleX) paddleX = (int)ballX + 16;
  194. //      if(ballX + 16 < paddleX)
  195.         paddleX = (int)ballX + 16;
  196.         if(ballY + ballSpeedY <= 20.0f) ballSpeedY = -ballSpeedY;
  197.     }
  198.  
  199.     @Override
  200.     public void render () {
  201.         calcBall();
  202.         calcPaddle();
  203.  
  204. //      Gdx.gl.glClearColor(glred, 0.2f, 0.2f, 0.1f);
  205. //      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  206.         Gdx.gl.glEnable(GL20.GL_BLEND);
  207.         Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  208.  
  209.         sr.setColor(0f,0f,0f,0.25f);
  210.         sr.setProjectionMatrix(camera.combined);
  211.         sr.begin(ShapeRenderer.ShapeType.Filled);
  212.         sr.rect(0,0,screenWidth,screenHeight);
  213. //      sr.setColor(0f, 1f, 1f, 1f);
  214. //      sr.rect( blocks[69].pixelRect.x ,blocks[69].pixelRect.y, blocks[69].pixelRect.width, blocks[69].pixelRect.height);
  215. //      sr.rect( ballRectangle.x, ballRectangle.y, ballRectangle.width, ballRectangle.height); //ballX, ballY, 32, 63);
  216. //      ballVec.set(ballSpeedX*10,ballSpeedY*10);
  217. //      ballVec.rotateRad(angle);
  218. //      sr.rectLine(ballX+16.0f, ballY+16.0f, ballX+16.0f+ballVec.x, ballY+16.0f+ballVec.y, 1);
  219.         sr.end();
  220.  
  221.         batch.begin();
  222.  
  223.         drawBlocks();
  224.         drawBall();
  225.         drawPaddle();
  226.  
  227.         font.draw(batch, "fps: " + fps, 0, screenHeight);
  228. //      font.draw(batch, "bally" + ballSpeedY, 0, screenHeight-19);
  229. //      font.draw(batch, "x" + ballX, 150, screenHeight);
  230. //      font.draw(batch, "y" + ballY, 150, screenHeight-19);
  231.  
  232.         batch.end();
  233.  
  234.  
  235.     }
  236.    
  237.     @Override
  238.     public void dispose () {
  239.         batch.dispose();
  240.         img.dispose();
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement