Fixing Jump Bug

master
bob 2022-06-01 13:52:13 -04:00
parent 2d24fe98fa
commit bf7d6e4b19
6 changed files with 12 additions and 13 deletions

Binary file not shown.

Binary file not shown.

View File

@ -104,8 +104,8 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
} }
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) { if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT; player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
if (player.y >= GAME_HEIGHT - Player.BALL_DIAMETER) { if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
player.y = GAME_HEIGHT - Player.BALL_DIAMETER; player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
player.yVelocity = 0; player.yVelocity = 0;
player.isGrounded = true; player.isGrounded = true;
} }
@ -115,12 +115,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
if (player.x <= 0) { if (player.x <= 0) {
player.x = 0; player.x = 0;
} }
}
if (player.x + Player.PLAYER_WIDTH >= GAME_WIDTH) { if (player.x + Player.PLAYER_WIDTH >= GAME_WIDTH) {
player.x = GAME_WIDTH - Player.PLAYER_WIDTH; player.x = GAME_WIDTH - Player.PLAYER_WIDTH;
} }
} }
} }
}
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen //run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){ public void run(){
@ -142,7 +142,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
checkCollision(); checkCollision();
repaint(); repaint();
if (frameCounter > 10) { if (frameCounter > 10) {
System.out.println(frameCounter);
// increment sprite image to be used and keeps it below 12 // increment sprite image to be used and keeps it below 12
frame = (frame + 1) % 11; frame = (frame + 1) % 11;
frameCounter -= 10; frameCounter -= 10;

View File

@ -13,8 +13,8 @@ public class GenericSprite extends Rectangle{
public double xVelocity; public double xVelocity;
public final double SPEED = 20; //movement speed of ball public final double SPEED = 20; //movement speed of ball
public final double speedCap = 7; //Speed cap of ball public final double speedCap = 7; //Speed cap of ball
public static final int BALL_DIAMETER = 20; //size of ball public static final int WIDTH = 20; //size of ball
public static final int HEIGHT = 20; //size of ball
public boolean rightPressed = false; public boolean rightPressed = false;
public boolean leftPressed = false; public boolean leftPressed = false;
public boolean upPressed= false; public boolean upPressed= false;
@ -82,8 +82,8 @@ public class GenericSprite extends Rectangle{
if(rightPressed==true){ if(rightPressed==true){
xVelocity+=1; xVelocity+=1;
} }
if(leftPressed==true){ if(leftPressed==true) {
xVelocity-=1; xVelocity -= 1;
} }
if(upPressed&isGrounded){ if(upPressed&isGrounded){
yVelocity = -10; yVelocity = -10;
@ -106,7 +106,7 @@ public class GenericSprite extends Rectangle{
//draws the current location of the ball to the screen //draws the current location of the ball to the screen
public void draw(Graphics g){ public void draw(Graphics g){
g.setColor(Color.black); g.setColor(Color.black);
g.fillOval(x, y, BALL_DIAMETER, BALL_DIAMETER); g.fillOval(x, y, WIDTH, HEIGHT);
} }
} }

View File

@ -9,8 +9,8 @@ import java.awt.image.ImageObserver;
public class Player extends GenericSprite { public class Player extends GenericSprite {
public final int SPEED = 5; public final int SPEED = 5;
public static final int PLAYER_WIDTH = 8; public static final int PLAYER_WIDTH = 72;
public static final int PLAYER_HEIGHT = 80; public static final int PLAYER_HEIGHT = 97;
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