diff --git a/out/production/final/GameFrame.class b/out/production/final/GameFrame.class index 324fea9..0280457 100644 Binary files a/out/production/final/GameFrame.class and b/out/production/final/GameFrame.class differ diff --git a/out/production/final/GamePanel.class b/out/production/final/GamePanel.class index 5a72dbc..0cd9913 100644 Binary files a/out/production/final/GamePanel.class and b/out/production/final/GamePanel.class differ diff --git a/out/production/final/GenericSprite.class b/out/production/final/GenericSprite.class index 87a5e2d..499fc0c 100644 Binary files a/out/production/final/GenericSprite.class and b/out/production/final/GenericSprite.class differ diff --git a/out/production/final/Player.class b/out/production/final/Player.class index aa4c79d..5909082 100644 Binary files a/out/production/final/Player.class and b/out/production/final/Player.class differ diff --git a/src/GameFrame.java b/src/GameFrame.java index ad7e7ad..c2c9559 100644 --- a/src/GameFrame.java +++ b/src/GameFrame.java @@ -8,11 +8,23 @@ import javax.swing.*; public class GameFrame extends JFrame{ - GamePanel panel; + MenuPanel menu; + GamePanel game; + boolean startGame = false; public GameFrame(){ - panel = new GamePanel(); //run GamePanel constructor - this.add(panel); + menu = new MenuPanel(); + while (!startGame) { + startGame = menu.hasButtonClicked(); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + menu.setVisible(false); // hide menu when game has launched + game = new GamePanel(); //run GamePanel constructor + this.add(game); this.setTitle("GUI is cool!"); //set title for frame this.setResizable(false); //frame can't change size this.setBackground(Color.white); diff --git a/src/GamePanel.java b/src/GamePanel.java index 3b6c3c7..a089809 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -19,8 +19,8 @@ import javax.swing.*; public class GamePanel extends JPanel implements Runnable, KeyListener{ //dimensions of window - public static final int GAME_WIDTH = 1536; - public static final int GAME_HEIGHT = 768; + public static final int GAME_WIDTH = 1200; + public static final int GAME_HEIGHT = 600; public Thread gameThread; public Image image; @@ -30,7 +30,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ // keeps track of how many ticks has elapsed since last frame change public int frameCounter = 0; - public BufferedImage[][] spriteArray = new BufferedImage[4][11]; + public BufferedImage[][][] spriteArray = new BufferedImage[2][2][11]; public static ArrayListmap = new ArrayList(); @@ -39,10 +39,10 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ try { BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0'))); - spriteArray[1][i] = sprite; - spriteArray[3][i] = sprite; - spriteArray[0][i] = sprite; - spriteArray[2][i] = sprite; + spriteArray[1][0][i] = sprite; + spriteArray[1][1][i] = sprite; + spriteArray[0][0][i] = flipImageHorizontally(sprite); + spriteArray[0][1][i] = flipImageHorizontally(sprite); } catch (IOException e) { e.printStackTrace(); @@ -170,4 +170,15 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ return ImageIO.read(new File(imageLocation)); } + public BufferedImage flipImageHorizontally(BufferedImage originalImage) { + BufferedImage flippedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB); + for (int x = 0; x < originalImage.getWidth(); x++) { + for (int y = 0; y < originalImage.getHeight(); y++) { + // -1 is added to prevent off-by-one errors + flippedImage.setRGB(x, y, originalImage.getRGB(originalImage.getWidth()-x-1, y)); + } + } + return flippedImage; + } + } \ No newline at end of file diff --git a/src/GenericSprite.java b/src/GenericSprite.java index e508a8b..6a7cbae 100644 --- a/src/GenericSprite.java +++ b/src/GenericSprite.java @@ -79,10 +79,10 @@ public class GenericSprite extends Rectangle{ public void move(){ y = y + (int)yVelocity; x = x + (int)xVelocity; - if(rightPressed==true){ + if(rightPressed){ xVelocity+=1; } - if(leftPressed==true) { + if(leftPressed) { xVelocity -= 1; } if(upPressed&isGrounded){ diff --git a/src/MenuPanel.java b/src/MenuPanel.java new file mode 100644 index 0000000..72b0a64 --- /dev/null +++ b/src/MenuPanel.java @@ -0,0 +1,39 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Arrays; + +public class MenuPanel extends JFrame implements ActionListener { + + private Boolean buttonClicked = false; + JButton launchGame; + Container menuContainer; + + public MenuPanel() { + this.setTitle("First"); + this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + this.setSize(280, 200); + this.setVisible(true); + + launchGame = new JButton("Click"); + launchGame.addActionListener(this); + + menuContainer = getContentPane(); + menuContainer.setLayout(new FlowLayout()); + menuContainer.add(launchGame); + } + + public void paint(Graphics g) { + super.paint(g); + g.drawString("potato", 50, 50); + } + + public void actionPerformed(ActionEvent evt) { + buttonClicked = true; + } + + public Boolean hasButtonClicked() { + return buttonClicked; + } +} \ No newline at end of file diff --git a/src/Player.java b/src/Player.java index 96e42dd..cc7565c 100644 --- a/src/Player.java +++ b/src/Player.java @@ -11,13 +11,14 @@ public class Player extends GenericSprite { public final int SPEED = 5; public static final int PLAYER_WIDTH = 72; public static final int PLAYER_HEIGHT = 97; + public int lastXDirection, lastYDirection, lastFrame; public int upKey, downKey, rightKey, leftKey; // sA[0] is -x, -y // sA[1] is x, -y // sA[2] is -x, y // sA[3] is x, y - public BufferedImage[][] spriteArray; - public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][] sprites) { + public BufferedImage[][][] spriteArray; + public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][][] sprites) { super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH); this.upKey = upKey; @@ -46,11 +47,14 @@ public class Player extends GenericSprite { public int draw(Graphics g, int frame) { // g.setColor(Color.WHITE); - if (xVelocity == 0 && yVelocity == 0) { - g.drawImage(spriteArray[1][0], x, y, null); + if (!upPressed && !downPressed && !leftPressed && !rightPressed) { + g.drawImage(spriteArray[lastXDirection][lastYDirection][lastFrame], x, y, null); return 0; } else { - g.drawImage(spriteArray[(int)(Math.signum(xVelocity)+Math.signum(yVelocity)*2+3)/2][frame], x, y, null); + lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2; + lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2; + lastFrame = frame; + g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x, y, null); return 1; } }