Add sprite spinning, persistent sprite positions, menu
parent
8a3abaf889
commit
fe43eb9c3b
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -8,11 +8,23 @@ import javax.swing.*;
|
||||||
|
|
||||||
public class GameFrame extends JFrame{
|
public class GameFrame extends JFrame{
|
||||||
|
|
||||||
GamePanel panel;
|
MenuPanel menu;
|
||||||
|
GamePanel game;
|
||||||
|
boolean startGame = false;
|
||||||
|
|
||||||
public GameFrame(){
|
public GameFrame(){
|
||||||
panel = new GamePanel(); //run GamePanel constructor
|
menu = new MenuPanel();
|
||||||
this.add(panel);
|
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.setTitle("GUI is cool!"); //set title for frame
|
||||||
this.setResizable(false); //frame can't change size
|
this.setResizable(false); //frame can't change size
|
||||||
this.setBackground(Color.white);
|
this.setBackground(Color.white);
|
||||||
|
|
|
@ -19,8 +19,8 @@ import javax.swing.*;
|
||||||
public class GamePanel extends JPanel implements Runnable, KeyListener{
|
public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
|
|
||||||
//dimensions of window
|
//dimensions of window
|
||||||
public static final int GAME_WIDTH = 1536;
|
public static final int GAME_WIDTH = 1200;
|
||||||
public static final int GAME_HEIGHT = 768;
|
public static final int GAME_HEIGHT = 600;
|
||||||
|
|
||||||
public Thread gameThread;
|
public Thread gameThread;
|
||||||
public Image image;
|
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
|
// keeps track of how many ticks has elapsed since last frame change
|
||||||
public int frameCounter = 0;
|
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>();
|
public static ArrayList<Tile>map = new ArrayList<Tile>();
|
||||||
|
|
||||||
|
@ -39,10 +39,10 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
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')));
|
||||||
|
|
||||||
spriteArray[1][i] = sprite;
|
spriteArray[1][0][i] = sprite;
|
||||||
spriteArray[3][i] = sprite;
|
spriteArray[1][1][i] = sprite;
|
||||||
spriteArray[0][i] = sprite;
|
spriteArray[0][0][i] = flipImageHorizontally(sprite);
|
||||||
spriteArray[2][i] = sprite;
|
spriteArray[0][1][i] = flipImageHorizontally(sprite);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -170,4 +170,15 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
return ImageIO.read(new File(imageLocation));
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -79,10 +79,10 @@ public class GenericSprite extends Rectangle{
|
||||||
public void move(){
|
public void move(){
|
||||||
y = y + (int)yVelocity;
|
y = y + (int)yVelocity;
|
||||||
x = x + (int)xVelocity;
|
x = x + (int)xVelocity;
|
||||||
if(rightPressed==true){
|
if(rightPressed){
|
||||||
xVelocity+=1;
|
xVelocity+=1;
|
||||||
}
|
}
|
||||||
if(leftPressed==true) {
|
if(leftPressed) {
|
||||||
xVelocity -= 1;
|
xVelocity -= 1;
|
||||||
}
|
}
|
||||||
if(upPressed&isGrounded){
|
if(upPressed&isGrounded){
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,13 +11,14 @@ public class Player extends GenericSprite {
|
||||||
public final int SPEED = 5;
|
public final int SPEED = 5;
|
||||||
public static final int PLAYER_WIDTH = 72;
|
public static final int PLAYER_WIDTH = 72;
|
||||||
public static final int PLAYER_HEIGHT = 97;
|
public static final int PLAYER_HEIGHT = 97;
|
||||||
|
public int lastXDirection, lastYDirection, lastFrame;
|
||||||
public int upKey, downKey, rightKey, leftKey;
|
public int upKey, downKey, rightKey, leftKey;
|
||||||
// sA[0] is -x, -y
|
// sA[0] is -x, -y
|
||||||
// sA[1] is x, -y
|
// sA[1] is x, -y
|
||||||
// sA[2] is -x, y
|
// sA[2] is -x, y
|
||||||
// sA[3] is x, y
|
// sA[3] is x, y
|
||||||
public BufferedImage[][] spriteArray;
|
public BufferedImage[][][] spriteArray;
|
||||||
public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][] sprites) {
|
public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][][] sprites) {
|
||||||
super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH);
|
super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH);
|
||||||
|
|
||||||
this.upKey = upKey;
|
this.upKey = upKey;
|
||||||
|
@ -46,11 +47,14 @@ public class Player extends GenericSprite {
|
||||||
|
|
||||||
public int draw(Graphics g, int frame) {
|
public int draw(Graphics g, int frame) {
|
||||||
// g.setColor(Color.WHITE);
|
// g.setColor(Color.WHITE);
|
||||||
if (xVelocity == 0 && yVelocity == 0) {
|
if (!upPressed && !downPressed && !leftPressed && !rightPressed) {
|
||||||
g.drawImage(spriteArray[1][0], x, y, null);
|
g.drawImage(spriteArray[lastXDirection][lastYDirection][lastFrame], x, y, null);
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} 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;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue