Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 #107

Open
310019032 opened this issue Dec 18, 2023 · 0 comments
Open

1 #107

310019032 opened this issue Dec 18, 2023 · 0 comments

Comments

@310019032
Copy link

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class MyGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture ballTexture;
    Vector2 ballPosition;
    Vector2 ballVelocity;

    @Override
    public void create() {
        batch = new SpriteBatch();
        ballTexture = new Texture("ball.png");
        ballPosition = new Vector2(100, 100);
        ballVelocity = new Vector2(3, 3);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        
        updateBallPosition();
        
        batch.begin();
        batch.draw(ballTexture, ballPosition.x, ballPosition.y);
        batch.end();
    }
    
    private void updateBallPosition() {
        ballPosition.x += ballVelocity.x;
        ballPosition.y += ballVelocity.y;
        
        if (ballPosition.x < 0 || ballPosition.x > Gdx.graphics.getWidth() - ballTexture.getWidth()) {
            ballVelocity.x *= -1;
        }
        
        if (ballPosition.y < 0 || ballPosition.y > Gdx.graphics.getHeight() - ballTexture.getHeight()) {
            ballVelocity.y *= -1;
        }
    }
    
    @Override
    public void dispose() {
        batch.dispose();
        ballTexture.dispose();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant