Draw lives, add intro music, background siren

Move score to original position
This commit is contained in:
Matt Low 2019-12-25 01:24:30 +04:00
parent 67cf7ff7c5
commit e038db9500
3 changed files with 55 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package com.me.pacman;
import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver; import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
@ -25,6 +26,8 @@ public class Assets {
public Sound chomp_1; public Sound chomp_1;
public Sound chomp_2; public Sound chomp_2;
public Sound siren;
public Sound beginning;
public Assets() { public Assets() {
this.manager = new AssetManager(); this.manager = new AssetManager();
@ -44,6 +47,8 @@ public class Assets {
manager.load("sounds/chomp_1.wav", Sound.class); manager.load("sounds/chomp_1.wav", Sound.class);
manager.load("sounds/chomp_2.wav", Sound.class); manager.load("sounds/chomp_2.wav", Sound.class);
manager.load("sounds/siren.wav", Sound.class);
manager.load("sounds/beginning.wav", Sound.class);
// Yayyy! all of this to load a font // Yayyy! all of this to load a font
FileHandleResolver resolver = new InternalFileHandleResolver(); FileHandleResolver resolver = new InternalFileHandleResolver();
@ -70,6 +75,8 @@ public class Assets {
// all our sounds // all our sounds
chomp_1 = manager.get("sounds/chomp_1.wav", Sound.class); chomp_1 = manager.get("sounds/chomp_1.wav", Sound.class);
chomp_2 = manager.get("sounds/chomp_2.wav", Sound.class); chomp_2 = manager.get("sounds/chomp_2.wav", Sound.class);
siren = manager.get("sounds/siren.wav", Sound.class);
beginning = manager.get("sounds/beginning.wav", Sound.class);
} }
public Texture getLevelBackground() { public Texture getLevelBackground() {

View File

@ -13,14 +13,18 @@ public class Pacman extends MovableEntity {
private int freezeFrames = 0; private int freezeFrames = 0;
public Pacman(PlayState state) { public Pacman(PlayState state, boolean moving) {
super(state, 14, 7.5f, 7.5f, true, Direction.EAST, 0.3f); super(state, 14, 7.5f, 7.5f, moving, Direction.EAST, 0.3f);
this.state = state; this.state = state;
sprite = state.getGame().assets.pacman; sprite = state.getGame().assets.pacman;
} }
@Override @Override
public TextureRegion getSprite() { public TextureRegion getSprite() {
if (!moving) {
return sprite[0][0];
}
int spriteDir; int spriteDir;
switch(currDirection) { switch(currDirection) {
case NORTH: case NORTH:
@ -36,7 +40,7 @@ public class Pacman extends MovableEntity {
spriteDir = 3; spriteDir = 3;
break; break;
} }
return sprite[spriteDir][canMove && moving? counter % 3 : 1]; return sprite[spriteDir][canMove ? counter % 3 : 1];
} }
@Override @Override

View File

@ -3,8 +3,10 @@ package com.me.pacman.state;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter; import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.me.pacman.PacDude; import com.me.pacman.PacDude;
import com.me.pacman.entity.MovableEntity; import com.me.pacman.entity.MovableEntity;
import com.me.pacman.entity.Pacman; import com.me.pacman.entity.Pacman;
@ -19,8 +21,13 @@ public class PlayState extends LevelState {
private int pelletCount = 0; private int pelletCount = 0;
private int pelletEatenCount = 0; private int pelletEatenCount = 0;
private int score = 0; private int score = 0;
private int lives = 3;
public boolean paused = false; public boolean paused = false;
public boolean started = false;
private float waitTimer;
private TextureRegion lifeSprite;
private Pacman pacman; private Pacman pacman;
@ -32,13 +39,14 @@ public class PlayState extends LevelState {
public void setup() { public void setup() {
levelBackground = game.assets.getLevelBackground(); levelBackground = game.assets.getLevelBackground();
font = game.assets.getFont(); font = game.assets.getFont();
lifeSprite = game.assets.pacman[2][1];
level = new Level(game,"level"); level = new Level(game,"level");
pelletCount = level.getTileCount(LevelTile.PELLET); pelletCount = level.getTileCount(LevelTile.PELLET);
pacman = new Pacman(this);
Gdx.input.setInputProcessor(new Controller()); Gdx.input.setInputProcessor(new Controller());
newGame();
} }
@Override @Override
@ -48,17 +56,38 @@ public class PlayState extends LevelState {
pacman.render(game.batch, 0, 16); pacman.render(game.batch, 0, 16);
game.assets.getFont().draw(game.batch, "" + score, 25, 276); for (int i = 0; i < lives; i++) {
game.batch.draw(lifeSprite, i * 16, 0);
}
game.assets.getFont().draw(game.batch, "" + score, 40, 279);
} }
@Override @Override
public void update(float dt) { public void update(float dt) {
if (waitTimer > 0) {
waitTimer -= dt;
if (waitTimer <= 0) {
startGame();
} else {
return;
}
}
pacman.update(dt); pacman.update(dt);
} }
@Override public void newGame() {
public void dispose() { waitTimer = 4.3f;
Gdx.input.setInputProcessor(null); game.assets.beginning.play(1.0f);
pacman = new Pacman(this, false);
}
public void startGame() {
pacman.moving = true;
game.assets.beginning.stop();
game.assets.siren.loop(1.0f);
lives--;
} }
public void eatPellet(float x, float y) { public void eatPellet(float x, float y) {
@ -73,6 +102,11 @@ public class PlayState extends LevelState {
score += 10; score += 10;
} }
@Override
public void dispose() {
Gdx.input.setInputProcessor(null);
}
private final class Controller extends InputAdapter { private final class Controller extends InputAdapter {
@Override @Override