diff --git a/doc/index.md b/doc/index.md new file mode 100644 index 0000000..9256125 --- /dev/null +++ b/doc/index.md @@ -0,0 +1,25 @@ +# Function Overview + +This document intended to assist developers who seek to extend the game by modifying source code in src/. + +## Classes + +### BackgroundImage + +Its constructor takes the following arguments: + +`int x, int y, BufferedImage backgroundImage, int width, int height, int parallaxRatio` + +Draws a background image that moves one pixel every `parallaxRatio` pixels when `BackgroundImage.draw()` is called. + +### Camera + +placeholder + +## Utility Functions + +These functions are located in the `public final` class UtilityFunction; please note that the UtilityFunction constructor is private to prevent initialization. + +### readFromFile + +placeholder \ No newline at end of file diff --git a/src/BackgroundImage.java b/src/BackgroundImage.java index de43202..beb758a 100644 --- a/src/BackgroundImage.java +++ b/src/BackgroundImage.java @@ -4,22 +4,21 @@ import java.awt.image.BufferedImage; public class BackgroundImage { public int x, y; public int width, height; + public int parallaxRatio; public BufferedImage backgroundImage; - public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height) throws SpriteException { - int compressionRatio; - + public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height, int parallaxRatio) { this.x = x; this.y = y; this.width = width; this.height = height; this.backgroundImage = backgroundImage; - // compressionRatio = Math.max(backgroundImage.getWidth() / width, backgroundImage.getHeight() / height); - // this.backgroundImage = backgroundImage.getSubimage((backgroundImage.getWidth() - width)/2, - // (backgroundImage.getHeight() - height)/2, width, height); + this.parallaxRatio = parallaxRatio; } public void draw(Graphics g){ - g.drawImage(backgroundImage, x, y, width, height, null); + g.drawImage(backgroundImage, x-GamePanel.camera.x/parallaxRatio % width, y, width, height, null); + // added to prevent the background image from disappearing + g.drawImage(backgroundImage, x-GamePanel.camera.x/parallaxRatio % width + width - 1, y, width, height, null); } } diff --git a/src/GamePanel.java b/src/GamePanel.java index 04f957e..8918f46 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -50,7 +50,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ public GamePanel() throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { camera = new Camera(0); - background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT); + background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10); for (int i = 0; i < 11; i++) { try { BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0')));