Add sprite spinning, persistent sprite positions, menu

master
John 2022-06-01 14:39:57 -04:00
parent 8a3abaf889
commit fe43eb9c3b
9 changed files with 83 additions and 17 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

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

View File

@ -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 ArrayList<Tile>map = new ArrayList<Tile>();
@ -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;
}
}

View File

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

39
src/MenuPanel.java Normal file
View File

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

View File

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