libgdx将分数发送到mysql服务器

p5cysglq  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(282)

我用libgdx库在androidstudio上制作了flappy bird游戏,现在我想把它连接到mysql数据库。我的意思是在球员死后把比分和名字发给他。我已经做了很多搜索,所以我最后的希望在这里,谢谢。
这是我的游戏屏幕:
公共类gameplayscreen扩展screenadapter{

public static final float PIPE_SPACING = 200f;
public static final int PIPE_SETS = 3;

protected OrthographicCamera camera;
protected FlappyGame game;

private Stage gameplayStage;
private Stage uiStage;

private Label scoreLabel;
private Label tapToRetry;
private Label best;
private Label tapToFlap;

private Image whitePixel;
private Image backgroundBuildings;

public int score;

private Bird bird;
private Array<PipePair> pipePairs;

private Ground ground;
private boolean justTouched;

private Color backgroundColor;

private State screenState = State.PREGAME;
private boolean allowRestart = false;

private enum State {PREGAME, PLAYING, DYING, DEAD}

;

public GameplayScreen(FlappyGame game) {
    this.game = game;

    camera = new OrthographicCamera(FlappyGame.WIDTH, FlappyGame.HEIGHT);
    gameplayStage = new Stage(new StretchViewport(FlappyGame.WIDTH, FlappyGame.HEIGHT, camera));
    uiStage = new Stage(new StretchViewport(FlappyGame.WIDTH, FlappyGame.HEIGHT));

    bird = new Bird();
    bird.setPosition(FlappyGame.WIDTH * .25f, FlappyGame.HEIGHT / 2, Align.center);
    bird.addAction(Utils.getFloatyAction());
    bird.setState(Bird.State.PREGAME);

    whitePixel = new Image(Assets.whitePixel);

    scoreLabel = new Label("0", new Label.LabelStyle(Assets.fontMedium, Color.WHITE));
    scoreLabel.setPosition(FlappyGame.WIDTH / 2, FlappyGame.HEIGHT * .9f, Align.center);
    uiStage.addActor(scoreLabel);

    tapToRetry = new Label("Tap To Retry!", new Label.LabelStyle(Assets.fontMedium, Color.WHITE));
    tapToRetry.setPosition(FlappyGame.WIDTH / 2, 0, Align.top);
    uiStage.addActor(tapToRetry);

    best = new Label("Best: ", new Label.LabelStyle(Assets.fontMedium, Color.WHITE));
    best.setPosition(FlappyGame.WIDTH / 2, 0, Align.top);
    uiStage.addActor(best);

    tapToFlap = new Label("Tap To Flap!", new Label.LabelStyle(Assets.fontMedium, Color.WHITE));
    tapToFlap.setPosition(FlappyGame.WIDTH / 2, FlappyGame.HEIGHT, Align.bottom);
    uiStage.addActor(tapToFlap);

    initBackgroundBuildings();

    pipePairs = new Array<PipePair>();

    ground = new Ground();
    ground.setPosition(0, 0);

    backgroundColor = Utils.getRandomBackgroundColor();

    gameplayStage.addActor(ground);
    gameplayStage.addActor(backgroundBuildings);
    gameplayStage.addActor(bird);

    initInputProcessor();
}

private void initBackgroundBuildings() {
    backgroundBuildings = new Image(Assets.backgroundBuildings);
    backgroundBuildings.setWidth(FlappyGame.WIDTH);
    backgroundBuildings.setHeight(backgroundBuildings.getHeight()*2f);
    backgroundBuildings.setY(Ground.HEIGHT);
}

@Override
public void show() {
    tapToFlap.addAction(Actions.moveToAligned(FlappyGame.CENTER_X, FlappyGame.CENTER_Y + 100f, Align.center, .75f, Interpolation.sine));
    Assets.playWooshSound();
}

@Override
public void render(float delta) {

    Gdx.graphics.getGL20().glClearColor(backgroundColor.r, backgroundColor.g, backgroundColor.b, 1f);
    Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    switch (screenState) {
        case PREGAME:
            updateAndDrawStages();
            break;
        case PLAYING:
            renderPlaying();
            break;
        case DYING:
        case DEAD:
            renderDeadOrDying();
            break;
    }
}

private void renderDeadOrDying() {
    if (bird.getState() == Bird.State.DEAD) {
        screenState = State.DEAD;
    }
    updateAndDrawStages();
}

private void renderPlaying() {
    if (justTouched) {
        bird.jump();
        justTouched = false;
    }
    updatePipePairs();
    gameplayStage.act();
    uiStage.act();
    checkCollisions();
    if (bird.getState() == Bird.State.DYING) {
        stopTheWorld();

        RunnableAction playWooshAction = Actions.run(new Runnable() {
            @Override
            public void run() {
                Assets.playWooshSound();

            }
        });

        SequenceAction actions = Actions.sequence(Actions.delay(1f), playWooshAction, Actions.moveToAligned(FlappyGame.CENTER_X, FlappyGame.CENTER_Y, Align.bottom,
                .75f, Interpolation.sine), Actions.run(new Runnable() {
            @Override
            public void run() {
                allowRestart = true;
            }
        }));
        tapToRetry.addAction(actions);

        best.setText("Best: " + SavedDataManager.getInstance().getHighScore()); 

        best.setPosition(FlappyGame.CENTER_X, 0, Align.top);
        best.addAction(Actions.delay(1f, Actions.moveToAligned(FlappyGame.CENTER_X, FlappyGame.CENTER_Y, Align.top,
                .75f, Interpolation.sine)));

        screenState = State.DYING;
    }
    gameplayStage.draw();
    uiStage.draw();
}

private void updateAndDrawStages() {
    gameplayStage.act();
    gameplayStage.draw();
    uiStage.act();
    uiStage.draw();
}

@Override
public void resize(int width, int height) {

    camera.setToOrtho(false, width, height);
    Assets.batch.setProjectionMatrix(camera.combined);
    gameplayStage.getViewport().update(width, height, true);
    uiStage.getViewport().update(width, height, true);

}

@Override
public void dispose() {
    gameplayStage.dispose();
    uiStage.dispose();
}

private void checkCollisions() {

    for (int i = 0; i < pipePairs.size; i++) {
        PipePair pair = pipePairs.get(i);
        if (pair.getBottomPipe().getBounds().overlaps(bird.getBounds()) || pair.getTopPipe().getBounds().overlaps(bird.getBounds())) {
            stopTheWorld();
            SavedDataManager.getInstance().setHighScore(score);
            showWhiteScreen();
        } else if (bird.isBelowGround()) {
            bird.setY(FlappyGame.GROUND_LEVEL);
            bird.clearActions();
            bird.setToDying();
            showWhiteScreen();
        } else if (bird.isAboveCeiling()) {
            bird.setY(FlappyGame.HEIGHT - bird.getHeight());
            bird.setToDying();
            showWhiteScreen();
        } else if (pair.getRuby().getBounds().overlaps(bird.getBounds())) {
            score++;
            updateScoreLabel();
            pair.moveCoinOffscreen();
            Assets.playBingSound();
        }
    }

}

private void showWhiteScreen() {
    whitePixel.setWidth(FlappyGame.WIDTH);
    whitePixel.setHeight(FlappyGame.HEIGHT);

    gameplayStage.addActor(whitePixel);

    whitePixel.addAction(Actions.fadeOut(.5f));
}

private void updateScoreLabel() {
    scoreLabel.setText(String.valueOf(score));

    scoreLabel.setPosition(FlappyGame.WIDTH / 2, FlappyGame.HEIGHT * .9f, Align.center);
}

private void stopTheWorld() {
    bird.setToDying();
    killPipePairs();
    stopTheGround();
    screenState = State.DYING;

}

private void stopTheGround() {
    ground.vel.x = 0;
}

private void killPipePairs() {
    for (PipePair pair : pipePairs) {
        pair.getBottomPipe().setState(Pipe.State.dead);
        pair.getTopPipe().setState(Pipe.State.dead);
        pair.getRuby().setVel(0, 0);
    }
}

private void updatePipePairs() {
    for (int i = 0; i < pipePairs.size; i++) {
        pipePairs.get(i).update();
    }
}

private void addPipes(Stage gameplayStage) {
    for (int i = 0; i < pipePairs.size; i++) {
        gameplayStage.addActor(pipePairs.get(i).getBottomPipe());
        gameplayStage.addActor(pipePairs.get(i).getTopPipe());
        gameplayStage.addActor(pipePairs.get(i).getRuby());
    }
}

private void initThirdSetOfPipes() {
    Pipe topPipe = new Pipe();
    Pipe bottomPipe = new Pipe();
    topPipe.getRegion().flip(false, true);
    PipePair pair = new PipePair(topPipe, bottomPipe);
    pair.initThird();

    pipePairs.add(pair);
}

private void initSecondSetOfPipes() {
    Pipe topPipe = new Pipe();
    Pipe bottomPipe = new Pipe();
    topPipe.getRegion().flip(false, true);
    PipePair pair = new PipePair(topPipe, bottomPipe);
    pair.initSecond();

    pipePairs.add(pair);
}

private void initFirstSetOfPipes() {
    Pipe topPipe = new Pipe();
    Pipe bottomPipe = new Pipe();
    topPipe.getRegion().flip(false, true);
    PipePair pair = new PipePair(topPipe, bottomPipe);
    pair.initFirst();

    pipePairs.add(pair);
}

private void initInputProcessor() {
    Gdx.input.setInputProcessor(new InputAdapter() {
        // We only care about the touch down event
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {

            switch (screenState) {

                case DYING:
                    justTouched = true;
                    break;

                case DEAD:
                    if (allowRestart) {
                        game.setScreen(new GameplayScreen(game));
                    }
                    justTouched = true;
                    break;

                case PLAYING:
                    justTouched = true;
                    break;

                case PREGAME:
                    justTouched = true;
                    screenState = State.PLAYING;
                    bird.setState(Bird.State.ALIVE);
                    bird.clearActions();
                    tapToFlap.addAction(Actions.moveToAligned(FlappyGame.CENTER_X, FlappyGame.HEIGHT, Align.bottom, .75f, Interpolation.sine));
                    initFirstSetOfPipes();
                    initSecondSetOfPipes();
                    initThirdSetOfPipes();
                    addPipes(gameplayStage);
                    gameplayStage.addActor(ground);
                    gameplayStage.addActor(bird);
                    break;

            }
            return true;
        }
    });
}

}

zpgglvta

zpgglvta1#

您可以使用javajdbc连接到mysql服务器。但这意味着您必须在程序中写入mysql服务器用户名和密码。这不安全。

相关问题