Refined the exhaust animation

This commit is contained in:
Martin Dvořák 2026-10-19 23:19:25 +02:00
commit b5afa132e7

View file

@ -63,6 +63,8 @@ public class Game extends JPanel implements ActionListener, KeyListener {
private int rocketYPre = windowHeight/2-125; private int rocketYPre = windowHeight/2-125;
private int rocketY = windowHeight/2-125; private int rocketY = windowHeight/2-125;
private int rocketExhaustY = rocketY; private int rocketExhaustY = rocketY;
private float opacityAnimation = 0.05f;
public Game() { public Game() {
// sets the window size // sets the window size
setPreferredSize(new Dimension(windowWidth,windowHeight)); setPreferredSize(new Dimension(windowWidth,windowHeight));
@ -112,17 +114,19 @@ public class Game extends JPanel implements ActionListener, KeyListener {
hoveringOverQuitButton = quitButtonCollision.contains(mouseX, mouseY); hoveringOverQuitButton = quitButtonCollision.contains(mouseX, mouseY);
if (hoverDirection == "up") { if (hoverDirection == "up") {
hoverYOffset++; hoverYOffset++;
opacityAnimation += 0.01;
if (hoverYOffset > 90) { if (hoverYOffset > 90) {
hoverDirection = "down"; hoverDirection = "down";
} }
} else { } else {
hoverYOffset--; hoverYOffset--;
opacityAnimation -= 0.01;
if (hoverYOffset <= 0) { if (hoverYOffset <= 0) {
hoverDirection = "up"; hoverDirection = "up";
} }
} }
if (level == 1) { planetY = planetYPre + hoverYOffset / 6; } if (level == 1) { planetY = planetYPre + hoverYOffset / 6; }
if (level == 2) { rocketY = rocketYPre + hoverYOffset / 6; rocketExhaustY = rocketYPre + 150 + hoverYOffset/3;} if (level == 2) { rocketY = rocketYPre + hoverYOffset / 3; rocketExhaustY = rocketYPre + 150 + hoverYOffset / 2;}
repaint(); // triggers render repaint(); // triggers render
} }
@ -184,7 +188,10 @@ public class Game extends JPanel implements ActionListener, KeyListener {
for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars
g.fillOval(starX[i]-starSize, starY[i]-starSize, starSize, starSize); g.fillOval(starX[i]-starSize, starY[i]-starSize, starSize, starSize);
} }
g.drawImage(rocketExhaust, windowWidth/2-25, rocketExhaustY, 50, 100, this); Graphics2D g2 = (Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacityAnimation));
g2.drawImage(rocketExhaust, windowWidth/2-25, rocketExhaustY, 50, 100, this);
g2.setComposite(AlphaComposite.SrcOver); // reset so nothing else is transparent
g.drawImage(rocket, windowWidth/2-73, rocketY, 147, 234, this); g.drawImage(rocket, windowWidth/2-73, rocketY, 147, 234, this);