From 50937ab7fd80fceeecdfbe2a276f3e27a4bbfcf1 Mon Sep 17 00:00:00 2001 From: Loosen-IT Date: Wed, 27 Nov 2024 16:50:18 +0100 Subject: [PATCH] refactorings I --- refactoring/flappy-bird/.idea/misc.xml | 2 +- refactoring/flappy-bird/.idea/uiDesigner.xml | 124 ++++++++++++++++++ .../com/example/flappybird/Animation.java | 53 -------- .../java/com/example/flappybird/Audio.java | 12 +- .../java/com/example/flappybird/Bird.java | 41 ++++-- .../com/example/flappybird/GamePanel.java | 15 ++- .../com/example/flappybird/HasPosition.java | 19 +++ .../java/com/example/flappybird/Helper.java | 31 ----- .../java/com/example/flappybird/Pipe.java | 40 ++---- .../java/com/example/flappybird/Texture.java | 32 ++--- 10 files changed, 215 insertions(+), 154 deletions(-) create mode 100644 refactoring/flappy-bird/.idea/uiDesigner.xml delete mode 100644 refactoring/flappy-bird/src/main/java/com/example/flappybird/Animation.java create mode 100644 refactoring/flappy-bird/src/main/java/com/example/flappybird/HasPosition.java delete mode 100644 refactoring/flappy-bird/src/main/java/com/example/flappybird/Helper.java diff --git a/refactoring/flappy-bird/.idea/misc.xml b/refactoring/flappy-bird/.idea/misc.xml index e122dea..cbec6ae 100644 --- a/refactoring/flappy-bird/.idea/misc.xml +++ b/refactoring/flappy-bird/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/refactoring/flappy-bird/.idea/uiDesigner.xml b/refactoring/flappy-bird/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/refactoring/flappy-bird/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Animation.java b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Animation.java deleted file mode 100644 index 416757d..0000000 --- a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Animation.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.example.flappybird; /** - * Animation.java - * Creates animation with array of sprites - * - * @author Paul Krishnamurthy - */ - -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.image.BufferedImage; -import java.awt.geom.AffineTransform; - -public class Animation { - - // Start at first frame - private static double currentFrame = 0; - - /** - * Creates an animation with an array of sprites - * - * @param sprites Array of BufferedImages - * @param x x-coordinate - * @param y y-coordinate - * @param speed Speed of animation - * @param angle Angle to rotate sprite - */ - public static void animate (Graphics g, BufferedImage[] sprites, int x, int y, double speed, double angle) { - - Graphics2D g2d = (Graphics2D) g; - AffineTransform trans = g2d.getTransform(); - AffineTransform at = new AffineTransform(); - - // Number of frames - int count = sprites.length; - - // Rotate the image - at.rotate(angle, x + 25, y + 25); - g2d.transform(at); - - // Draw the current rotated frame - g2d.drawImage(sprites[(int) (Math.round(currentFrame))], x, y, null); - - g2d.setTransform(trans); - - // Switch animation frames - if (currentFrame >= count - 1) { - currentFrame = 0; - } else currentFrame += speed; - - } - -} - diff --git a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Audio.java b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Audio.java index 21a4a57..a56114c 100644 --- a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Audio.java +++ b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Audio.java @@ -8,12 +8,9 @@ package com.example.flappybird; /** import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; +import java.net.URL; public class Audio { - - private AudioInputStream audioInputStream; - private Clip clip; - private void playSound (String sound) { // Path to sound file @@ -21,8 +18,11 @@ public class Audio { // Try to load and play sound try { - audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource(soundURL)); - clip = AudioSystem.getClip(); + URL audioUrl = this.getClass().getResource(soundURL); + assert audioUrl != null; + + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioUrl); + Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); } catch (Exception e) { diff --git a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Bird.java b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Bird.java index 3eb3cf2..acc6b6a 100644 --- a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Bird.java +++ b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Bird.java @@ -5,15 +5,15 @@ package com.example.flappybird; /** * @author Paul Krishnamurthy */ -import javax.swing.JPanel; +import java.awt.*; +import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; -import java.awt.Graphics; -public class Bird extends JPanel { +public class Bird extends HasPosition { // Bird attributes public String color; - private int x, y; + private boolean isAlive = true; // Bird constants @@ -34,11 +34,12 @@ public class Bird extends JPanel { // Bird sprites private BufferedImage[] sprites; + private static double currentFrame = 0; + public Bird (String color, int x, int y, BufferedImage[] s) { + super(x, y); this.color = color; - this.x = x; - this.y = y; this.sprites = s; } @@ -144,7 +145,7 @@ public class Bird extends JPanel { rotation /= 2; // Handle rotation offset - rotation = rotation > Math.PI / 2 ? Math.PI / 2 : rotation; + rotation = Math.min(rotation, Math.PI / 2); if (!isAlive()) { @@ -157,8 +158,32 @@ public class Bird extends JPanel { } // Create bird animation and pass in rotation angle - Animation.animate(g, sprites, x, y, .09, rotation); + animateBird(g, rotation); } + private void animateBird(Graphics g, double angle) { + + Graphics2D g2d = (Graphics2D) g; + AffineTransform trans = g2d.getTransform(); + AffineTransform at = new AffineTransform(); + + // Number of frames + int count = sprites.length; + + // Rotate the image + at.rotate(angle, x + 25, y + 25); + g2d.transform(at); + + // Draw the current rotated frame + g2d.drawImage(sprites[(int) (Math.round(currentFrame))], x, y, null); + + g2d.setTransform(trans); + + // Switch animation frames + if (currentFrame >= count - 1) { + currentFrame = 0; + } else currentFrame += 0.09; + + } } diff --git a/refactoring/flappy-bird/src/main/java/com/example/flappybird/GamePanel.java b/refactoring/flappy-bird/src/main/java/com/example/flappybird/GamePanel.java index d2728cf..4bd2b94 100644 --- a/refactoring/flappy-bird/src/main/java/com/example/flappybird/GamePanel.java +++ b/refactoring/flappy-bird/src/main/java/com/example/flappybird/GamePanel.java @@ -8,6 +8,7 @@ package com.example.flappybird; /** import java.awt.*; import java.awt.event.*; import javax.swing.*; +import java.net.URI; import java.util.Random; import java.util.ArrayList; import java.util.HashMap; @@ -501,6 +502,18 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener { } + public void openURL (String url) { + + try { + if (Desktop.isDesktopSupported()) { + Desktop.getDesktop().browse(new URI(url)); + } + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Sorry could not open URL..."); + } + + } ////////////////////// // Keyboard actions // @@ -569,7 +582,7 @@ public class GamePanel extends JPanel implements KeyListener, MouseListener { if (gameBird.isAlive()) { if (isTouching(textures.get("rateButton").getRect())) { - Helper.openURL("http://paulkr.com"); // Open website + openURL("http://paulkr.com"); // Open website } } diff --git a/refactoring/flappy-bird/src/main/java/com/example/flappybird/HasPosition.java b/refactoring/flappy-bird/src/main/java/com/example/flappybird/HasPosition.java new file mode 100644 index 0000000..01ad686 --- /dev/null +++ b/refactoring/flappy-bird/src/main/java/com/example/flappybird/HasPosition.java @@ -0,0 +1,19 @@ +package com.example.flappybird; + +public class HasPosition { + protected int x; + protected int y; + + public HasPosition(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } +} diff --git a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Helper.java b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Helper.java deleted file mode 100644 index 205bca3..0000000 --- a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Helper.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.example.flappybird; /** - * Helper.java - * Helper class with various tools - * - * @author Paul Krishnamurthy - */ - -import java.awt.Desktop; -import java.net.URI; - -public class Helper { - - /** - * Tries to open url in default web browser - * - * @param url Destination URL - */ - public static void openURL (String url) { - - try { - if (Desktop.isDesktopSupported()) { - Desktop.getDesktop().browse(new URI(url)); - } - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Sorry could not open URL..."); - } - - } - -} \ No newline at end of file diff --git a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Pipe.java b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Pipe.java index fcddd68..7edcbdb 100644 --- a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Pipe.java +++ b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Pipe.java @@ -7,12 +7,7 @@ package com.example.flappybird; * @author Paul Krishnamurthy */ -public class Pipe { - - // Pipe coordinates - private int x = FlappyBird.WIDTH + 5; - private int y; - +public class Pipe extends HasPosition { // Placement (top or bottom) of pipe String location; @@ -27,17 +22,18 @@ public class Pipe { public boolean canAwardPoint = true; public Pipe (String location) { + super(FlappyBird.WIDTH + 5, 0); this.location = location; reset(); } public void reset () { - x = FlappyBird.WIDTH + 5; // Reset x-coordinate + super.x = FlappyBird.WIDTH + 5; // Reset x-coordinate // Set boundaries for top pipes // This y-coordinte + PIPE_SPACING will be for the bottom pipe if (location.equals("top")) { - y = - Math.max((int) (Math.random() * 320) + 30, 140); + super.y = - Math.max((int) (Math.random() * 320) + 30, 140); } } @@ -45,7 +41,7 @@ public class Pipe { * Moves the pipe */ public void move () { - x += SPEED; + super.x += SPEED; } @@ -61,29 +57,14 @@ public class Pipe { public boolean collide (int nX, int nY, int nW, int nH) { // Do not allow bird to jump over pipe - if (nX > x && nY < 0 && canAwardPoint) { + if (nX > super.x && nY < 0 && canAwardPoint) { return true; } - return nX < x + WIDTH && - nX + nW > x && - nY < y + HEIGHT && - nY + nH > y; - - } - - /** - * @return Pipe's x-coordinate - */ - public int getX () { - return x; - } - - /** - * @return Pipe's y-coordinate - */ - public int getY () { - return y; + return nX < super.x + WIDTH && + nX + nW > super.x && + nY < super.y + HEIGHT && + nY + nH > super.y; } /** @@ -94,6 +75,5 @@ public class Pipe { public void setY (int newY) { y = newY; } - } diff --git a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Texture.java b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Texture.java index 6e4f33b..923a44e 100644 --- a/refactoring/flappy-bird/src/main/java/com/example/flappybird/Texture.java +++ b/refactoring/flappy-bird/src/main/java/com/example/flappybird/Texture.java @@ -8,20 +8,20 @@ package com.example.flappybird; /** import java.awt.image.BufferedImage; import java.awt.Rectangle; -public class Texture { +public class Texture extends HasPosition { // Image attributes - private BufferedImage image; - private int x, y, width, height; - private Rectangle rect; + private final BufferedImage image; + private final Rectangle rect; public Texture (BufferedImage image, int x, int y) { + super(x, y); + this.image = image; - this.x = x; - this.y = y; - this.width = image.getWidth(); - this.height = image.getHeight(); + int width = image.getWidth(); + int height = image.getHeight(); + this.rect = new Rectangle(x, y, width, height); } @@ -33,22 +33,6 @@ public class Texture { return image; } - public int getX () { - return x; - } - - public int getY () { - return y; - } - - public int getWidth () { - return width; - } - - public int getHeight () { - return height; - } - public Rectangle getRect () { return rect; }