final/src/GenericSprite.java

112 lines
2.8 KiB
Java
Raw Normal View History

2022-05-30 19:46:53 +01:00
/* PlayerBall class defines behaviours for the player-controlled ball
child of Rectangle because that makes it easy to draw and check for collision
In 2D GUI, basically everything is a rectangle even if it doesn't look like it!
*/
import java.awt.*;
import java.awt.event.*;
2022-05-31 18:19:23 +01:00
public class GenericSprite extends Rectangle{
2022-05-30 19:46:53 +01:00
2022-05-31 18:19:41 +01:00
public double yVelocity;
public double xVelocity;
public final double SPEED = 20; //movement speed of ball
public final double speedCap = 7; //Speed cap of ball
2022-05-30 19:46:53 +01:00
public static final int BALL_DIAMETER = 20; //size of ball
2022-05-31 18:19:41 +01:00
public boolean rightPressed = false;
public boolean leftPressed = false;
public boolean upPressed= false;
public boolean downPressed = false;
public boolean isGrounded = false;
2022-05-30 19:46:53 +01:00
//constructor creates ball at given location with given dimensions
2022-05-31 18:19:23 +01:00
public GenericSprite(int x, int y, int height, int width){
super(x, y, height, width);
2022-05-30 19:46:53 +01:00
}
//called from GamePanel when any keyboard input is detected
//updates the direction of the ball based on user input
//if the keyboard input isn't any of the options (d, a, w, s), then nothing happens
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == 'd'){
2022-05-31 18:19:41 +01:00
rightPressed = true;
2022-05-30 19:46:53 +01:00
move();
}
if(e.getKeyChar() == 'a'){
2022-05-31 18:19:41 +01:00
leftPressed = true;
2022-05-30 19:46:53 +01:00
move();
}
if(e.getKeyChar() == 'w'){
2022-05-31 18:19:41 +01:00
upPressed = true;
2022-05-30 19:46:53 +01:00
move();
}
if(e.getKeyChar() == 's'){
2022-05-31 18:19:41 +01:00
downPressed = true;
2022-05-30 19:46:53 +01:00
move();
}
}
//called from GamePanel when any key is released (no longer being pressed down)
//Makes the ball stop moving in that direction
public void keyReleased(KeyEvent e){
if(e.getKeyChar() == 'd'){
2022-05-31 18:19:41 +01:00
rightPressed = false;
2022-05-30 19:46:53 +01:00
move();
}
if(e.getKeyChar() == 'a'){
2022-05-31 18:19:41 +01:00
leftPressed = false;
2022-05-30 19:46:53 +01:00
move();
}
if(e.getKeyChar() == 'w'){
2022-05-31 18:19:41 +01:00
upPressed = false;
2022-05-30 19:46:53 +01:00
move();
}
if(e.getKeyChar() == 's'){
2022-05-31 18:19:41 +01:00
downPressed = false;
2022-05-30 19:46:53 +01:00
move();
}
}
//called from GamePanel whenever a mouse click is detected
//changes the current location of the ball to be wherever the mouse is located on the screen
public void mousePressed(MouseEvent e){
2022-05-31 18:19:41 +01:00
//x = e.getX();
// y = e.getY();
2022-05-30 19:46:53 +01:00
}
2022-05-31 18:19:41 +01:00
public void move(){
y = y + (int)yVelocity;
x = x + (int)xVelocity;
if(rightPressed==true){
xVelocity+=1;
}
if(leftPressed==true){
xVelocity-=1;
}
if(upPressed&isGrounded){
yVelocity = -10;
}
xVelocity*=0.9;
yVelocity+=0.3;
capSpeed();
2022-05-30 19:46:53 +01:00
}
2022-05-31 18:19:41 +01:00
public void capSpeed(){
if(xVelocity>speedCap){
xVelocity = speedCap;
} else if(xVelocity<-1*speedCap) {
xVelocity = -1*speedCap;
}
2022-05-30 19:46:53 +01:00
}
2022-05-31 18:19:41 +01:00
2022-05-30 19:46:53 +01:00
//called frequently from the GamePanel class
//draws the current location of the ball to the screen
public void draw(Graphics g){
g.setColor(Color.black);
g.fillOval(x, y, BALL_DIAMETER, BALL_DIAMETER);
}
}