Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Big code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszrajchert committed Apr 24, 2020
1 parent 71ccb26 commit 6a4ef35
Show file tree
Hide file tree
Showing 17 changed files with 309 additions and 339 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![Version](https://img.shields.io/github/v/tag/rajlu/awesome-dino?label=Version&style=for-the-badge) ![Repo size](https://img.shields.io/github/repo-size/rajlu/awesome-dino?style=for-the-badge) ![Code size](https://img.shields.io/github/languages/code-size/rajlu/awesome-dino?style=for-the-badge)
A Java version ot the offline T-Rex chrome game.
Some features are missing, some additional has been added.
Game works on ~**60 fps**.
Game works on ~**60 FPS**.

Game is for my CS project.
## Features
Expand All @@ -20,9 +20,13 @@ Game is for my CS project.
- Intro
- Pause
- Sounds
- **EASTER EGG** _(u need to click somewhere on intro screen)_
- **EASTER EGG** _(you need to click somewhere in intro screen)_

## Keybinds:
## How to start
1. Clone git
2. Run from "GameStarter" class

## Keybinds
##### Jump: `SPACE`, `w`, `ARROW UP`
##### Fall: `s`, `ARROW DOWN`
##### Pause: `p`, `ESC`
Expand Down
2 changes: 2 additions & 0 deletions src/GameStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public class GameStarter {
public static void main(String[] args) {
System.out.println("\nStartup log");
System.out.println("-----------------------------------------------------");
SwingUtilities.invokeLater(GameWindow::new);
}
}
23 changes: 9 additions & 14 deletions src/components/background/Background.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
import static main.GameWindow.WINDOW_WIDTH;

public class Background implements Drawable {
private ArrayList<ComponentImage> cloudImages;
private final BufferedImage cloud;
private static final BufferedImage CLOUD_IMAGE = new Resource().getResourceImage("/assets/Cloud.png");

private final int backgroundSpeed = gameSpeed / 3;

private BackgroundColors backgroundColor;

ComponentImage firstCloud;
ComponentImage secondCloud;
ComponentImage thirdCloud;
private static ArrayList<ComponentImage> cloudImages;
private static final ComponentImage firstCloud = new ComponentImage(CLOUD_IMAGE, WINDOW_WIDTH - 700, 40, Color.LIGHT_GRAY);
private static final ComponentImage secondCloud = new ComponentImage(CLOUD_IMAGE, WINDOW_WIDTH - 400, 20, Color.LIGHT_GRAY);
private static final ComponentImage thirdCloud = new ComponentImage(CLOUD_IMAGE, WINDOW_WIDTH - 200, 80, Color.LIGHT_GRAY);

public Background() {
cloud = new Resource().getResourceImage("/assets/Cloud.png");
backgroundColor = BackgroundColors.DEFAULT;

backgroundInit();
Expand All @@ -38,13 +37,9 @@ public void setBackgroundColor(BackgroundColors backgroundColor) {

private void backgroundInit() {
cloudImages = new ArrayList<>();
cloudImages.add(new ComponentImage(cloud, WINDOW_WIDTH - 700, 40, Color.LIGHT_GRAY));
cloudImages.add(new ComponentImage(cloud, WINDOW_WIDTH - 400, 20, Color.LIGHT_GRAY));
cloudImages.add(new ComponentImage(cloud, WINDOW_WIDTH - 200, 80, Color.LIGHT_GRAY));

firstCloud = cloudImages.get(0);
secondCloud = cloudImages.get(1);
thirdCloud = cloudImages.get(2);
cloudImages.add(firstCloud);
cloudImages.add(secondCloud);
cloudImages.add(thirdCloud);
}

private void changeBackgroundColor() {
Expand Down Expand Up @@ -88,7 +83,7 @@ public void draw(Graphics g) {
}

for (ComponentImage clouds : cloudImages) {
if (DEBUG_MODE) {
if (debugMode) {
g.setColor(clouds.debugColor);
g.drawRect(clouds.x, clouds.y, clouds.image.getWidth(), clouds.image.getHeight());
}
Expand Down
203 changes: 96 additions & 107 deletions src/components/dino/Dino.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,44 @@
import java.util.ArrayList;

import static components.ground.Ground.GROUND_Y;
import static main.GamePanel.DEBUG_MODE;
import static main.GamePanel.debugMode;
import static main.GamePanel.GAME_GRAVITY;

public class Dino implements Drawable {
private static final int DINO_JUMP_STRENGTH = 12;
private static final int DINO_FALL_STRENGTH = 8;
private static final float DINO_START_X = 50;
private static int DINO_RUNNING_ANIMATION_DELTA_TIME = 60;
private static final int DINO_BORDER_SIZE = 1;

private static DinoStates dinoState;
private static final float X = DINO_START_X;

private static float x, y;
public static boolean isMario = false;
public static boolean marioLoaded = false;

private static DinoStates dinoState = DinoStates.IDLE;

private static BufferedImage idleImage = new Resource().getResourceImage("/assets/Dino-stand.png");
private static BufferedImage jumpImage = new Resource().getResourceImage("/assets/Dino-stand.png");
private static Animation runAnimation = new Animation(DINO_RUNNING_ANIMATION_DELTA_TIME);
private static BufferedImage dieImage = new Resource().getResourceImage("/assets/Dino-big-eyes.png");

/**
* Collision adjustments.
* It is modified version from chromium source code.
* ---------------------------------------------------
* ______
* _| |-|
* | | dino | |
* |_| |_|
* |_____ |
*/
public static ArrayList<Coordinates> constructedCoordinates = new ArrayList<>();
private static final Coordinates collisionLeft = new Coordinates(0, 15, 11 - DINO_BORDER_SIZE, 21 - DINO_BORDER_SIZE);
private static final Coordinates collisionMiddle = new Coordinates(10, 0, 22 - DINO_BORDER_SIZE, 45 - DINO_BORDER_SIZE);
private static final Coordinates collisionRight = new Coordinates(31, 0, 10 - DINO_BORDER_SIZE, 21 - DINO_BORDER_SIZE);

private static float y = GROUND_Y - idleImage.getHeight();
private static float speedY;

/**
Expand All @@ -28,87 +54,102 @@ public class Dino implements Drawable {
*/
private static float TEMP_y;

private static BufferedImage idleImage;
private static BufferedImage jumpImage;
private static Animation runAnimation;
private static BufferedImage dieImage;

public static ArrayList<Coordinates> constructedCoordinates;
private static Coordinates collisionLeft;
private static Coordinates collisionMiddle;
private static Coordinates collisionRight;

/**
* It eliminates system delay between typed key event and pressed key event
*/
public static boolean jumpRequested;
public boolean jumpRequested;

private static Sound jumpSound;
public static Sound gameOverSound;
private static Sound jumpSound = new Sound("/assets/sounds/button-press.wav");
public Sound gameOverSound = new Sound("/assets/sounds/hit.wav");

public Dino() {
idleImage = new Resource().getResourceImage("/assets/Dino-stand.png");
jumpImage = new Resource().getResourceImage("/assets/Dino-stand.png");;
runAnimation = new Animation(DINO_RUNNING_ANIMATION_DELTA_TIME);
runAnimation.addFrame(new Resource().getResourceImage("/assets/Dino-left-up.png"));
runAnimation.addFrame(new Resource().getResourceImage("/assets/Dino-right-up.png"));
dieImage = new Resource().getResourceImage("/assets/Dino-big-eyes.png");

x = DINO_START_X;
y = GROUND_Y - idleImage.getHeight();
dinoState = DinoStates.IDLE;

jumpSound = new Sound("/assets/sounds/button-press.wav");
gameOverSound = new Sound("/assets/sounds/hit.wav");

// Collision adjustments.
// It is modified version from chromium source code.
// ---------------------------------------------------
// ______
// _| |-|
// | | dino | |
// |_| |_|
// |_____ |
int borderSize = 1;
constructedCoordinates = new ArrayList<>();
constructedCoordinates.add(new Coordinates((int) X, (int) y + collisionLeft.y, collisionLeft.width, collisionLeft.height));
constructedCoordinates.add(new Coordinates((int) X + collisionMiddle.x, (int) y, collisionMiddle.width, collisionMiddle.height));
constructedCoordinates.add(new Coordinates((int) X + collisionRight.x, (int) y, collisionRight.width, collisionRight.height));
}

public void run() {
dinoState = DinoStates.RUNNING;
}

public void jump() {
if (dinoState == DinoStates.RUNNING) {
dinoState = DinoStates.JUMPING;

speedY = -DINO_JUMP_STRENGTH;
y += speedY;

// It prevents from layering sounds and game freeze
if (!jumpSound.isNull()) {
if (jumpSound.isOpen()) jumpSound.stop();
}
jumpSound.play();
} else if (dinoState == DinoStates.JUMPING) {
jumpRequested = true;
}
}

public void fall() {
if (dinoState == DinoStates.JUMPING) {
speedY = DINO_FALL_STRENGTH;
y += speedY;
}
}

public void die() {
dinoState = DinoStates.DIE;
gameOverSound.play();
}

public void setMario() {
System.out.println("\nMARIOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
DINO_RUNNING_ANIMATION_DELTA_TIME = 100;

idleImage = new Resource().getResourceImage("/assets/mario/Mario-welcome.png");
jumpImage = new Resource().getResourceImage("/assets/mario/Mario-jump.png");
runAnimation = new Animation(DINO_RUNNING_ANIMATION_DELTA_TIME);
runAnimation.addFrame(new Resource().getResourceImage("/assets/mario/Mario-left-up.png"));
runAnimation.addFrame(new Resource().getResourceImage("/assets/mario/Mario-right-up.png"));
dieImage = new Resource().getResourceImage("/assets/mario/Mario-dead.png");

jumpSound = new Sound("/assets/sounds/mario/jump.wav");
gameOverSound = new Sound("/assets/sounds/mario/dead.wav");

collisionLeft = new Coordinates(0, 15, 11 - borderSize, 21 - borderSize);
collisionMiddle = new Coordinates(10, 0, 22 - borderSize, 45 - borderSize);
collisionRight = new Coordinates(31, 0, 10 - borderSize, 21 - borderSize);
constructedCoordinates = new ArrayList<>();
constructedCoordinates.add(new Coordinates((int) X, (int) y, idleImage.getWidth(), idleImage.getHeight()));

constructedCoordinates.add(new Coordinates((int) x, (int) y + collisionLeft.y, collisionLeft.width, collisionLeft.height));
constructedCoordinates.add(new Coordinates((int) x + collisionMiddle.x, (int) y, collisionMiddle.width, collisionMiddle.height));
constructedCoordinates.add(new Coordinates((int) x + collisionRight.x, (int) y, collisionRight.width, collisionRight.height));
marioLoaded = true;
System.out.println("MARIOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
}

@Override
public void draw(Graphics g) {
if (DEBUG_MODE) {
if (debugMode) {
for (Coordinates coordinates : constructedCoordinates) {
g.setColor(Color.BLACK);
g.drawRect(coordinates.x, coordinates.y, coordinates.width, coordinates.height);
}
}
switch (dinoState) {
case IDLE:
g.drawImage(idleImage, (int)x, (int)y, null);
g.drawImage(idleImage, (int) X, (int)y, null);
break;
case JUMPING:
g.drawImage(jumpImage, (int)x, (int)y, null);
g.drawImage(jumpImage, (int) X, (int)y, null);
break;
case RUNNING:
runAnimation.update();
g.drawImage(runAnimation.getFrame(), (int)x, (int)y, null);
g.drawImage(runAnimation.getFrame(), (int) X, (int)y, null);
break;
case DIE:
g.drawImage(dieImage, (int)x, (int)y, null);
g.drawImage(dieImage, (int) X, (int)y, null);
break;
}
}

/**
*
*/
@Override
public void update() {
if((TEMP_y + speedY) >= GROUND_Y - idleImage.getHeight()) {
Expand All @@ -125,16 +166,16 @@ public void update() {
TEMP_y = y;
}
if (constructedCoordinates.size() > 1) {
constructedCoordinates.get(0).x = (int) x;
constructedCoordinates.get(0).x = (int) X;
constructedCoordinates.get(0).y = (int) y + collisionLeft.y;

constructedCoordinates.get(1).x = (int) x + collisionMiddle.x;
constructedCoordinates.get(1).x = (int) X + collisionMiddle.x;
constructedCoordinates.get(1).y = (int) y;

constructedCoordinates.get(2).x = (int) x + collisionRight.x;
constructedCoordinates.get(2).x = (int) X + collisionRight.x;
constructedCoordinates.get(2).y = (int) y;
} else {
constructedCoordinates.get(0).x = (int) x;
constructedCoordinates.get(0).x = (int) X;
constructedCoordinates.get(0).y = (int) y;
}

Expand All @@ -145,56 +186,4 @@ public void reset() {
y = GROUND_Y - idleImage.getHeight();
run();
}

public void run() {
dinoState = DinoStates.RUNNING;
}

public void jump() {
if (dinoState == DinoStates.RUNNING) {
dinoState = DinoStates.JUMPING;

speedY = -DINO_JUMP_STRENGTH;
y += speedY;

// It prevents from layering sounds and game freeze
if (!jumpSound.isNull()) {
if (jumpSound.isOpen()) jumpSound.stop();
}
jumpSound.play();
} else if (dinoState == DinoStates.JUMPING) {
jumpRequested = true;
}
}

public void fall() {
if (dinoState == DinoStates.JUMPING) {
speedY = DINO_FALL_STRENGTH;
y += speedY;
}
}

public void die() {
dinoState = DinoStates.DIE;
gameOverSound.play();
}

public static void setMario() {
System.out.println("\nMARIOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
DINO_RUNNING_ANIMATION_DELTA_TIME = 100;

idleImage = new Resource().getResourceImage("/assets/mario/Mario-welcome.png");
jumpImage = new Resource().getResourceImage("/assets/mario/Mario-jump.png");
runAnimation = new Animation(DINO_RUNNING_ANIMATION_DELTA_TIME);
runAnimation.addFrame(new Resource().getResourceImage("/assets/mario/Mario-left-up.png"));
runAnimation.addFrame(new Resource().getResourceImage("/assets/mario/Mario-right-up.png"));
dieImage = new Resource().getResourceImage("/assets/mario/Mario-dead.png");

jumpSound = new Sound("/assets/sounds/mario/jump.wav");
gameOverSound = new Sound("/assets/sounds/mario/dead.wav");

constructedCoordinates = new ArrayList<>();
constructedCoordinates.add(new Coordinates((int) x, (int) y, idleImage.getWidth(), idleImage.getHeight()));
System.out.println("MARIOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
}
}
Loading

0 comments on commit 6a4ef35

Please sign in to comment.