Added box and background sprites, background image class, tiles with sprites, some error handling
parent
8d7b62c065
commit
e9f7b37486
Binary file not shown.
After Width: | Height: | Size: 2.6 KiB |
Binary file not shown.
After Width: | Height: | Size: 855 B |
Binary file not shown.
After Width: | Height: | Size: 962 B |
Binary file not shown.
After Width: | Height: | Size: 938 B |
Binary file not shown.
After Width: | Height: | Size: 836 B |
Binary file not shown.
After Width: | Height: | Size: 1002 B |
|
@ -0,0 +1,25 @@
|
|||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class BackgroundImage {
|
||||
public int x, y;
|
||||
public int width, height;
|
||||
public BufferedImage backgroundImage;
|
||||
|
||||
public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height) throws SpriteException {
|
||||
int compressionRatio;
|
||||
|
||||
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);
|
||||
}
|
||||
public void draw(Graphics g){
|
||||
g.drawImage(backgroundImage, x, y, width, height, null);
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ Runs the constructor in GamePanel class
|
|||
|
||||
*/
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import javax.swing.*;
|
||||
|
||||
public class GameFrame extends JFrame{
|
||||
|
@ -23,7 +24,14 @@ public class GameFrame extends JFrame{
|
|||
}
|
||||
}
|
||||
menu.setVisible(false); // hide menu when game has launched
|
||||
game = new GamePanel(); //run GamePanel constructor
|
||||
try {
|
||||
game = new GamePanel(); //run GamePanel constructor
|
||||
} catch (IOException | SpriteException e) {
|
||||
// TODO: handle IO errors gracefully
|
||||
// exceptions are raised when tiles are not found or are of incorrect dimensions
|
||||
menu.setVisible(true);
|
||||
menu.launchGame.setText("Invalid sprites error");
|
||||
}
|
||||
this.add(game);
|
||||
this.setTitle("GUI is cool!"); //set title for frame
|
||||
this.setResizable(false); //frame can't change size
|
||||
|
|
|
@ -26,6 +26,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
|||
public Image image;
|
||||
public Graphics graphics;
|
||||
public Player player;
|
||||
public BackgroundImage background;
|
||||
public int frame;
|
||||
// keeps track of how many ticks has elapsed since last frame change
|
||||
public int frameCounter = 0;
|
||||
|
@ -34,7 +35,13 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
|||
|
||||
public static ArrayList<Tile>map = new ArrayList<Tile>();
|
||||
|
||||
public GamePanel(){
|
||||
// image imports begin here
|
||||
public BufferedImage backgroundImage = getImage("img/backgrounds/pointyMountains.png");
|
||||
public BufferedImage box = getImage("img/tiles/boxes/box.png");
|
||||
public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.png");
|
||||
|
||||
public GamePanel() throws IOException, SpriteException {
|
||||
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT);
|
||||
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')));
|
||||
|
@ -49,9 +56,10 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
|||
}
|
||||
}
|
||||
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', spriteArray); //create a player controlled player, set start location to middle of screenk
|
||||
map.add(new Tile(1000, 500));
|
||||
map.add(new Tile(700, 400));
|
||||
map.add(new Tile(1000, 300));
|
||||
// the height of 35 is set because it is half of the original tile height (i.e., 70px)
|
||||
map.add(new SingleTile(1000, 500, box, 35));
|
||||
map.add(new SingleTile(700, 400, boxCoin, 35));
|
||||
map.add(new SingleTile(1000, 300, boxCoin, 35));
|
||||
map.add(new Tile(700, 200));
|
||||
this.setFocusable(true); //make everything in this class appear on the screen
|
||||
this.addKeyListener(this); //start listening for keyboard input
|
||||
|
@ -81,6 +89,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
|||
|
||||
//call the draw methods in each class to update positions as things move
|
||||
public void draw(Graphics g, int frame){
|
||||
background.draw(g);
|
||||
frameCounter += player.draw(g, frame);
|
||||
for(Tile i: map){
|
||||
i.draw(g);
|
||||
|
|
|
@ -7,7 +7,7 @@ import java.util.Arrays;
|
|||
public class MenuPanel extends JFrame implements ActionListener {
|
||||
|
||||
private Boolean buttonClicked = false;
|
||||
JButton launchGame;
|
||||
public JButton launchGame;
|
||||
Container menuContainer;
|
||||
|
||||
public MenuPanel() {
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class SingleTile extends Tile {
|
||||
public int length;
|
||||
public BufferedImage tileImage;
|
||||
|
||||
public SingleTile(int x, int y, BufferedImage tileImage, int length) throws SpriteException {
|
||||
super(x, y);
|
||||
|
||||
if (tileImage.getWidth() != tileImage.getHeight()) {
|
||||
throw new SpriteException();
|
||||
}
|
||||
|
||||
this.length = length;
|
||||
this.tileImage = tileImage;
|
||||
}
|
||||
public void draw(Graphics g){
|
||||
g.drawImage(tileImage, x, y, length, length, null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
public class SpriteException extends Exception {
|
||||
public SpriteException() {
|
||||
super("Tile sprites must have equal lengths and heights.");
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
import java.awt.*;
|
||||
|
||||
// all tiles must be squares
|
||||
public class Tile {
|
||||
public int x;
|
||||
public int y;
|
||||
|
|
Loading…
Reference in New Issue