57 lines
1.5 KiB
Java
57 lines
1.5 KiB
Java
|
/* 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;
|
||
|
public static final int PLAYER_WIDTH = 8;
|
||
|
public static final int PLAYER_HEIGHT = 80;
|
||
|
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) {
|
||
|
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 (xVelocity == 0 && yVelocity == 0) {
|
||
|
g.drawImage(spriteArray[1][0], x, y, null);
|
||
|
return 0;
|
||
|
} else {
|
||
|
g.drawImage(spriteArray[(int)(Math.signum(xVelocity)+Math.signum(yVelocity)*2+3)/2][frame], x, y, null);
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
|
}
|