Merge remote-tracking branch 'origin/master'

master
bob 2022-06-03 13:50:24 -04:00
commit 1421495745
3 changed files with 32 additions and 8 deletions

25
doc/index.md Normal file
View File

@ -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

View File

@ -4,22 +4,21 @@ import java.awt.image.BufferedImage;
public class BackgroundImage { public class BackgroundImage {
public int x, y; public int x, y;
public int width, height; public int width, height;
public int parallaxRatio;
public BufferedImage backgroundImage; public BufferedImage backgroundImage;
public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height) throws SpriteException { public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height, int parallaxRatio) {
int compressionRatio;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.backgroundImage = backgroundImage; this.backgroundImage = backgroundImage;
// compressionRatio = Math.max(backgroundImage.getWidth() / width, backgroundImage.getHeight() / height); this.parallaxRatio = parallaxRatio;
// this.backgroundImage = backgroundImage.getSubimage((backgroundImage.getWidth() - width)/2,
// (backgroundImage.getHeight() - height)/2, width, height);
} }
public void draw(Graphics g){ 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);
} }
} }

View File

@ -50,7 +50,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public GamePanel() throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { public GamePanel() throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
camera = new Camera(0); 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++) { for (int i = 0; i < 11; i++) {
try { try {
BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0'))); BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0')));