hey guys, I am pretty new to LibGDX and also to Over ALL programming yet i consider my self intermediate in programming. I started exploring LibGDX a few days ago, and read theory, along with some practice of creating shapes and running some loops. So I decided to code a simple PONG game to see how things work. I have created a Ball using Pixmap, and its randomly moving on the screen colliding with the ends of the screen etc, so i tried to make a Paddle using fill rectangle and new Pixmap but its not working, i mean if i ma increasing its width its increasing fine but if i am increasing its height its not working.
Is it okay to use 2 Pixmaps in a game? I know pixmaps a pretty slow but for a beginner like me isn’t it a good way to know the basics?
is there any other way i can draw shapes more efficiently ?
PS: Pardon me for my bad english, Its not my first language. Thanks.
Below is the code: Any suggestions about improving the writing style would also be appreciated
package com.me.mygdxgame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
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.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
public class MyGdxGame implements ApplicationListener {
public static int SCREEN_WIDTH = 800;
public static int SCREEN_HEIGHT = 480;
SpriteBatch batch;
Texture texture;
Pixmap pixmap;
Vector2 Ball_pos;
int SpeedX = 5;
int SpeedY = 5;
int rev = -1;
Texture T2;
Pixmap Paddle;
Vector2 Paddle_pos;
ShapeRenderer shape;
@Override
public void create() {
SCREEN_WIDTH = Gdx.graphics.getWidth();
SCREEN_HEIGHT = Gdx.graphics.getHeight();
batch = new SpriteBatch();
pixmap = new Pixmap(256,256,Pixmap.Format.RGBA8888);
texture = new Texture(pixmap);
Paddle = new Pixmap(256,256,Pixmap.Format.RGBA8888);
T2 = new Texture(Paddle);
Ball_pos = new Vector2(SCREEN_WIDTH/2-25,SCREEN_HEIGHT-190);
Paddle_pos = new Vector2(10, SCREEN_HEIGHT/2);
shape = new ShapeRenderer();
drawBall();
drawPaddle();
}
public void drawBall(){
Gdx.app.log("MyGdxGame", "drawSmily()");
pixmap.setColor(1, 1, 0, 1);
pixmap.fillCircle(256/2, 256/2, 20);
texture.draw(pixmap, 0, 0);
texture.bind();
}
public void drawPaddle(){
Gdx.app.log("Simple Pong", "Paddle()");
Paddle.setColor(Color.WHITE);
Paddle.fillRectangle((int)Paddle_pos.x, (int)Paddle_pos.y, 20, 122);
T2.draw(Paddle, 0, 0);
T2.bind();
}
@Override
public void dispose() {
Gdx.app.log("MyGdxGame", "Dispose()");
batch.dispose();
texture.dispose();
pixmap.dispose();
Paddle.dispose();
}
@Override
public void render() {
float deltatime = Gdx.graphics.getDeltaTime();
GLCommon gl = Gdx.gl;
gl.glClearColor(0, 0, 0,1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT | GL10.GL_STENCIL_BUFFER_BIT);
batch.begin();
batch.draw(texture, Ball_pos.x, Ball_pos.y);
batch.draw(T2, Paddle_pos.x, Paddle_pos.y);
batch.end();
Ball_pos.x += SpeedX;
Ball_pos.y += SpeedY;
if(Ball_pos.x + 120 > SCREEN_WIDTH ){
Ball_pos.x = SCREEN_WIDTH - 120;
SpeedX *= rev;
}
else if (Ball_pos.x < -90){
SpeedX *= rev;
}
if (Ball_pos.y + 120 > SCREEN_HEIGHT){
Ball_pos.y = SCREEN_HEIGHT - 120;
SpeedY *= rev;
}
else if(Ball_pos.y < -90){
//smily_pos.y = 0 + 100;
SpeedY *= rev;
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
Gdx.app.log("MyGdxGame", "Game.pause()");
}
@Override
public void resume() {
}
}