Skip to content

Commit 2eb641b

Browse files
Working score now
1 parent 4bb103c commit 2eb641b

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

core/src/com/ninjaghost/flappy/FlappyBirdCloneGame.java

+47-33
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.badlogic.gdx.graphics.g2d.Sprite;
1313
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
1414
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
15-
import com.badlogic.gdx.math.MathUtils;
1615
import com.badlogic.gdx.math.Rectangle;
1716
import com.badlogic.gdx.utils.ScreenUtils;
1817
import com.badlogic.gdx.utils.viewport.FitViewport;
@@ -36,7 +35,7 @@
3635
* ✔️ Respawnable
3736
* ✔️ Death Sequence
3837
* ✔️ GUI Bar
39-
* Counter
38+
* ✔️ Counter
4039
* Make things smoother (rotate bird)
4140
* Make Exe
4241
*/
@@ -104,8 +103,11 @@ Difficulty.HARD, new Settings("Hard",65f, 1.5f, 240f, 120f, 80f)
104103
float playerJumpStrength = 250;
105104
float playerDeathTimer = 0;
106105
float playerDeathTimerMax = 2.0f;
106+
float playerLastScoreTimer = 0; // when the player hits a score gate, they cant score again until
107+
float playerLastScoreTimerMax = 1.0f; // the timer reaches this threshhold
107108
boolean playerDead = false;
108109
boolean playerCanRespawn = false;
110+
boolean playerCanScore = false;
109111
int playerScore = 0;
110112
int playerHighScore = 0;
111113
int playerLastScore = 0;
@@ -170,6 +172,8 @@ private void applySettings() {
170172
private void startStartMenu() {
171173
gameState = GameState.MENU;
172174
activeMenu = Menu.START;
175+
playerCanScore = true;
176+
playerLastScoreTimer = 0;
173177
}
174178

175179
private void startGame() {
@@ -186,12 +190,19 @@ private void respawn() {
186190
player.setRotation(0);
187191
playerVelocity = 0;
188192
pipeSpawnTimer = 0;
189-
playerScore = 0;
190193
playerDeathTimer = 0;
194+
playerLastScoreTimer = 0;
195+
playerCanScore = true;
191196
playerDead = false;
192197
playerCanRespawn = false;
193198
pipeOffsets.clear();
194199
gameState = GameState.GAME;
200+
201+
playerLastScore = playerScore;
202+
if(playerLastScore > playerHighScore) {
203+
playerHighScore = playerLastScore;
204+
}
205+
playerScore = 0;
195206
}
196207

197208
@Override
@@ -255,11 +266,15 @@ public void render () {
255266
for (Rectangle bbox : deathBoxes) {
256267
shapeRenderer.rect(bbox.x, bbox.y, bbox.width, bbox.height);
257268
}
269+
shapeRenderer.setColor(Color.BLUE);
270+
for (Rectangle bbox : scoreBoxes) {
271+
shapeRenderer.rect(bbox.x, bbox.y, bbox.width, bbox.height);
272+
}
258273
shapeRenderer.end();
259274
}
260275

261276

262-
// UI goes on top of everything else so it must be rendered last
277+
// UI goes on top of everything else, so it must be rendered last
263278
batch.begin();
264279
font.getData().setScale(1.2f);
265280

@@ -353,6 +368,7 @@ public void render () {
353368

354369
public void update (float delta) {
355370
deathBoxes.clear(); // empty the death boxes
371+
scoreBoxes.clear();
356372

357373
if(gameState == GameState.DEATH) {
358374

@@ -395,6 +411,14 @@ else if(gameState == GameState.GAME) {
395411
pipeSpawnTimer = 0;
396412
}
397413

414+
// score update
415+
if(!playerCanScore) {
416+
playerLastScoreTimer += delta;
417+
if(playerLastScoreTimer > playerLastScoreTimerMax) {
418+
playerCanScore = true;
419+
}
420+
}
421+
398422
// update floor position
399423
floorOffsets.replaceAll(aFloat -> aFloat - floorMovementConst * delta);
400424
if(floorOffsets.get(0) <= -floorSprite.getWidth()) {
@@ -443,6 +467,9 @@ else if(gameState == GameState.GAME) {
443467
// create the bounding boxes
444468
deathBoxes.add((new Rectangle()).set(val[0], offset, greenPipeHigh.getWidth(), greenPipeHigh.getHeight()));
445469
deathBoxes.add((new Rectangle()).set(val[0], offset - greenPipeLow.getHeight() - pipeGapSize, greenPipeLow.getWidth(), greenPipeLow.getHeight()));
470+
if(playerCanScore) {
471+
scoreBoxes.add((new Rectangle()).set(val[0] + greenPipeHigh.getWidth(), offset - pipeGapSize, 2.0f, pipeGapSize));
472+
}
446473
}
447474

448475
// deal with input and calculate new position
@@ -485,6 +512,13 @@ else if(gameState == GameState.GAME) {
485512
triggerDeath();
486513
}
487514
}
515+
for (Rectangle bbox : scoreBoxes) {
516+
if (bbox.overlaps(player.getBoundingRectangle())) {
517+
System.out.println("Score");
518+
triggerScore();
519+
}
520+
}
521+
488522
}
489523
} else if(gameState == GameState.MENU) {
490524

@@ -519,6 +553,12 @@ private void triggerDeath() {
519553
gameState = GameState.DEATH;
520554
}
521555

556+
private void triggerScore() {
557+
playerScore++;
558+
playerLastScoreTimer = 0;
559+
playerCanScore = false;
560+
}
561+
522562
private void spawnPipe() {
523563
float screenWidth = (float) Gdx.graphics.getWidth();
524564

@@ -545,42 +585,16 @@ public void dispose () {
545585

546586
@Override
547587
public boolean keyDown(int keycode) {
548-
switch (keycode) {
549-
// case com.badlogic.gdx.Input.Keys.W:
550-
// iUp = true;
551-
// break;
552-
// case com.badlogic.gdx.Input.Keys.S:
553-
// iDown = true;
554-
// break;
555-
// case com.badlogic.gdx.Input.Keys.A:
556-
// iLeft = true;
557-
// break;
558-
// case com.badlogic.gdx.Input.Keys.D:
559-
// iRight = true;
560-
// break;
561-
case com.badlogic.gdx.Input.Keys.SPACE:
562-
iSpace = true;
563-
break;
564-
}
588+
if (keycode == Input.Keys.SPACE) {
589+
iSpace = true;
590+
}
565591

566592
return true;
567593
}
568594

569595
@Override
570596
public boolean keyUp(int keycode) {
571597
switch (keycode) {
572-
// case Input.Keys.W:
573-
// iUp = false;
574-
// break;
575-
// case Input.Keys.S:
576-
// iDown = false;
577-
// break;
578-
// case Input.Keys.A:
579-
// iLeft = false;
580-
// break;
581-
// case Input.Keys.D:
582-
// iRight = false;
583-
// break;
584598
case Input.Keys.J:
585599
cheat_freemove = !cheat_freemove;
586600
break;

0 commit comments

Comments
 (0)