Skip to content

Commit

Permalink
General code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottwahl committed Jan 3, 2024
1 parent a190f4b commit 3fd2948
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
28 changes: 28 additions & 0 deletions core/src/com/mygdx/game/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
package com.mygdx.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Main extends Game {
public SpriteBatch batch;
public OrthographicCamera camera;
public Fonts fonts;
public Background background;

public void create() {
batch = new SpriteBatch();

camera = new OrthographicCamera();
camera.setToOrtho(false, 800f, 600f);

fonts = new Fonts();

background = new Background();

this.setScreen(new MainMenuScreen(this));
}

@Override
public void render() {
super.render();
}

@Override
public void dispose() {
batch.dispose();
fonts.dispose();
background.dispose();
}
}
49 changes: 16 additions & 33 deletions core/src/com/mygdx/game/MainMenuScreen.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,48 @@
package com.mygdx.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;

public class MainMenuScreen implements Screen {
private Game game;
private Main game;
private OrthographicCamera camera;
private Background background;
private Player player;
private boolean isTextureDown;
private float timer;
private float animTimer;
private BitmapFont titleText;
private BitmapFont startText;
private Fonts fonts;
private SpriteBatch batch;

public MainMenuScreen(Game game) {
public MainMenuScreen(Main game) {
this.game = game;
}

@Override
public void show() {
camera = new OrthographicCamera();
camera.setToOrtho(false, 800f, 600f);

batch = new SpriteBatch();

background = new Background();
camera = game.camera;

// passing null for the PipeManager parameter to avoid unnecessary instantiation
player = new Player(null);

isTextureDown = true;

fonts = new Fonts();

titleText = fonts.createFont(60, 3, Color.WHITE);
titleText = game.fonts.createFont(60, 3, Color.WHITE);

startText = fonts.createFont(24, 0, Color.BLACK);
startText = game.fonts.createFont(24, 0, Color.BLACK);
}

@Override
public void render(float delta) {
ScreenUtils.clear(0, 0, 0, 1f);

// timer increments based on the time since the last frame
timer += Gdx.graphics.getDeltaTime();
animTimer += Gdx.graphics.getDeltaTime();

// switch isTextureDown to its opposite value every half a second
if (timer >= 0.5f) {
timer = 0;
if (animTimer >= 0.5f) {
animTimer = 0;

isTextureDown = !isTextureDown;
}
Expand All @@ -68,18 +54,18 @@ public void render(float delta) {
player.getPlayerSprite().setTexture(player.getPlayerTextureUp());
}

batch.setProjectionMatrix(camera.combined);
batch.begin();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();

background.draw(batch);
game.background.draw(game.batch);

player.draw(batch);
player.draw(game.batch);

titleText.draw(batch, "Flap-tastrophe", 100f, 550f);
titleText.draw(game.batch, "Flap-tastrophe", 100f, 550f);

startText.draw(batch, "Click anywhere to start", 400f, 300f);
startText.draw(game.batch, "Click anywhere to start", 400f, 300f);

batch.end();
game.batch.end();

if (Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
// set the player texture to playerTextureDown before switching to the game screen for a smooth transition
Expand Down Expand Up @@ -111,11 +97,8 @@ public void hide() {

@Override
public void dispose() {
background.dispose();
player.dispose();
titleText.dispose();
startText.dispose();
fonts.dispose();
batch.dispose();
}
}
1 change: 1 addition & 0 deletions core/src/com/mygdx/game/PipeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void draw(SpriteBatch batch, float deltaTime) {
for (Pipe p : pipes) {
p.movePipe(deltaTime, collisionCheck, moveUp);

// updates the pipe's opacity based on the setAlpha flag
if (setAlpha) {
p.getPipeSprite().setAlpha(0.5f);
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/src/com/mygdx/game/Score.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int scalingAmount() {
return 0;
}


// notifies subscribed classes on score changes to update difficulty scaling
public void notifyDifficulty() {
int scalingAmount = scalingAmount();

Expand Down

0 comments on commit 3fd2948

Please sign in to comment.