added game over and win screens

This commit is contained in:
2026-04-18 11:11:17 -05:00
parent b0673fc162
commit a211a10778
11 changed files with 28 additions and 23 deletions

View File

@@ -1,4 +1,3 @@
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Collectable extends Collidable{

View File

@@ -1,5 +1,4 @@
import javax.swing.*;
import java.awt.Color;
public class Display {
public static void main(String[] args) {

View File

@@ -1,4 +1,3 @@
import javax.swing.ImageIcon;
import java.util.*;
import java.io.*;

View File

@@ -1,13 +1,10 @@
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.*;
import java.time.LocalTime;
public class Platformer extends JPanel implements KeyListener, ActionListener{
@@ -43,7 +40,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
boolean allCollected;
boolean gameOver;
boolean gameStarted;
Image heart,emptyHeart,slash,amendmentImg,powerImg,startImg;
Image heart,emptyHeart,slash,amendmentImg,powerImg,pressRImg,startImg,endImg,winImg;
ArrayList<Image> numbers;
@@ -68,6 +65,9 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
heart = new ImageIcon("Sprites/Hearts/heart.png").getImage();
emptyHeart = new ImageIcon("Sprites/Hearts/emptyHeart.png").getImage();
startImg = new ImageIcon("Sprites/start.png").getImage();
endImg = new ImageIcon("Sprites/end.png").getImage();
pressRImg = new ImageIcon("Sprites/PressR.png").getImage();
winImg = new ImageIcon("Sprites/win.png").getImage();
gameTimer = new Timer(15,this);
player = new Player(-20,0,tileSize,tileSize);
collidables = new ArrayList<>();
@@ -105,6 +105,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
//gameloop
public void gameLoop(){
if (currentLevel > totalLevels) return;
//camera
cameraX = player.x - boardWidth / 2;
cameraX = Math.max(0, cameraX);
@@ -121,6 +122,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
if (currentLevel > totalLevels) {
gameTimer.stop();
System.out.println("You win!");
return;
} else {
loadLevel(currentLevel);
}
@@ -318,7 +320,20 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
//draw function
public void draw(Graphics g){
//gameover screen
if (gameOver){
g.drawImage(endImg, boardWidth/2 - 100, boardHeight/2 - 150, null);
g.drawImage(pressRImg, boardWidth/2 - 80, boardHeight/2, null);
return;
}
g.translate(-cameraX, -cameraY);
if (currentLevel > totalLevels) {
g.drawImage(winImg, boardWidth/2 - 100, boardHeight/2 - 50, null);
g.drawImage(pressRImg, boardWidth/2 - 80, boardHeight/2 + 60, null);
return;
}
player.draw(g);
for (Collidable c : collidables) c.draw(g);
@@ -331,7 +346,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
int amOnes = player.numAmendments % 10;
int amTens = player.numAmendments / 10;
if (currentLevel > 0 && !gameOver && player.numAmendments < numAm[currentLevel-1]){
if (currentLevel > 0 && player.numAmendments < numAm[currentLevel-1]){
if (amTens > 0) g.drawImage(numbers.get(amTens),flag.x-20,flag.y-30,null);
g.drawImage(numbers.get(amOnes),flag.x+5,flag.y-30,null);
g.drawImage(slash,flag.x+28,flag.y-32,null);
@@ -353,7 +368,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
else if (curTime >= modAmt/2 && curTime <= modAmt*3/4-1) startTime = 2;
else if (curTime >= modAmt*3/4 && curTime <= modAmt-1) startTime = 3;
if (currentLevel == 0 && !gameOver){
if (currentLevel == 0){
g.drawImage(startImg,340,300,null);
String text = "Press P to Start!";
@@ -375,7 +390,7 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
}
}
if (currentLevel > 0 && !gameOver){
if (currentLevel > 0){
//draw hearts:
int heartTime = 0;
@@ -409,11 +424,6 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
g.drawImage(powerImg,220,10,null);
}
}
//gameover screen
if (gameOver){
}
}
//is key pressed
@@ -444,13 +454,17 @@ public class Platformer extends JPanel implements KeyListener, ActionListener{
}
if (e.getKeyCode() == KeyEvent.VK_R){
if (gameOver) {
gameTimer.stop();
loadLevel(currentLevel);
loadLevel(1);
currentLevel = 1;
jumpPressed = false;
player.health=3;
gameOver = false;
repaint();
return;
}
return;
}
if (e.getKeyCode() == KeyEvent.VK_O){

View File

@@ -1,5 +1,3 @@
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.util.*;

View File

@@ -1,6 +1,4 @@
import javax.swing.ImageIcon;
import javax.swing.Timer;
import java.util.*;
public class Powerup extends Collectable{
int yVelo, xVelo, id;

View File

@@ -1,4 +1,3 @@
import java.awt.Graphics;
import javax.swing.ImageIcon;
import java.awt.Image;

BIN
Sprites/PressR.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 B

BIN
Sprites/end.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 694 B

BIN
Sprites/win.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@@ -1,4 +1,3 @@
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class Tile extends Collidable {