final/src/Player.java

61 lines
1.7 KiB
Java
Raw Normal View History

2022-05-31 18:19:23 +01:00
/* Eric Li, ICS4U, Completed 5/29/2022
Paddle class defines behaviours for the left and right player-controlled paddles */
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
public class Player extends GenericSprite {
public final int SPEED = 5;
2022-06-01 18:52:13 +01:00
public static final int PLAYER_WIDTH = 72;
public static final int PLAYER_HEIGHT = 97;
public int lastXDirection, lastYDirection, lastFrame;
2022-05-31 18:19:23 +01:00
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) {
2022-05-31 18:19:23 +01:00
super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH);
this.upKey = upKey;
this.downKey = downKey;
this.leftKey = leftKey;
this.rightKey = rightKey;
spriteArray = sprites;
}
// moves paddle when key is pressed
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
}
// stops moving paddle when key is released
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
// calls parent
public void move() {
super.move();
}
public int draw(Graphics g, int frame) {
// g.setColor(Color.WHITE);
if (!upPressed && !downPressed && !leftPressed && !rightPressed) {
2022-06-01 19:48:46 +01:00
g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x, y, null);
2022-05-31 18:19:23 +01:00
return 0;
} else {
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);
2022-05-31 18:19:23 +01:00
return 1;
}
}
}