Basic physics

master
bob 2022-05-31 13:19:41 -04:00
parent e0352d0456
commit ef61ea475b
10 changed files with 75 additions and 36 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -9,21 +9,24 @@ Implements Runnable interface to use "threading" - let the game do two things at
*/
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class GamePanel extends JPanel implements Runnable, KeyListener{
//dimensions of window
public static final int GAME_WIDTH = 1920;
public static final int GAME_HEIGHT = 1080;
public static final int GAME_WIDTH = 1536;
public static final int GAME_HEIGHT = 768;
public Thread gameThread;
public Image image;
public Graphics graphics;
public PlayerBall ball;
public static ArrayList<Tile>map = new ArrayList<Tile>();
public GamePanel(){
map.add(new Tile(1000, 700));
ball = new PlayerBall(GAME_WIDTH/2, GAME_HEIGHT/2); //create a player controlled ball, set start location to middle of screen
this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input
@ -54,6 +57,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//call the draw methods in each class to update positions as things move
public void draw(Graphics g){
ball.draw(g);
for(Tile i: map){
i.draw(g);
}
}
//call the move methods in other classes to update positions
@ -64,14 +70,21 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//handles all collision detection and responds accordingly
public void checkCollision(){
ball.isGrounded = false;
for(Tile i: map){
}
//force player to remain on screen
if(ball.y<= 0){
ball.y = 0;
}
if(ball.y >= GAME_HEIGHT - PlayerBall.BALL_DIAMETER){
ball.y = GAME_HEIGHT-PlayerBall.BALL_DIAMETER;
ball.yVelocity = 0;
ball.isGrounded = true;
}
if(ball.x <= 0){
ball.x = 0;
}

View File

@ -9,11 +9,17 @@ import java.awt.event.*;
public class PlayerBall extends Rectangle{
public int yVelocity;
public int xVelocity;
public final int SPEED = 20; //movement speed of ball
public double yVelocity;
public double xVelocity;
public final double SPEED = 20; //movement speed of ball
public final double speedCap = 7; //Speed cap of ball
public static final int BALL_DIAMETER = 20; //size of ball
public boolean rightPressed = false;
public boolean leftPressed = false;
public boolean upPressed= false;
public boolean downPressed = false;
public boolean isGrounded = false;
//constructor creates ball at given location with given dimensions
public PlayerBall(int x, int y){
super(x, y, BALL_DIAMETER, BALL_DIAMETER);
@ -24,22 +30,19 @@ public class PlayerBall extends Rectangle{
//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'){
setXDirection(SPEED);
rightPressed = true;
move();
}
if(e.getKeyChar() == 'a'){
setXDirection(SPEED*-1);
leftPressed = true;
move();
}
if(e.getKeyChar() == 'w'){
setYDirection(SPEED*-1);
upPressed = true;
move();
}
if(e.getKeyChar() == 's'){
setYDirection(SPEED);
downPressed = true;
move();
}
}
@ -48,22 +51,19 @@ public class PlayerBall extends Rectangle{
//Makes the ball stop moving in that direction
public void keyReleased(KeyEvent e){
if(e.getKeyChar() == 'd'){
setXDirection(0);
rightPressed = false;
move();
}
if(e.getKeyChar() == 'a'){
setXDirection(0);
leftPressed = false;
move();
}
if(e.getKeyChar() == 'w'){
setYDirection(0);
upPressed = false;
move();
}
if(e.getKeyChar() == 's'){
setYDirection(0);
downPressed = false;
move();
}
}
@ -71,26 +71,36 @@ public class PlayerBall extends Rectangle{
//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){
x = e.getX();
y = e.getY();
//x = e.getX();
// y = e.getY();
}
//called whenever the movement of the ball changes in the y-direction (up/down)
public void setYDirection(int yDirection){
yVelocity = yDirection;
}
//called whenever the movement of the ball changes in the x-direction (left/right)
public void setXDirection(int xDirection){
xVelocity = xDirection;
}
//called frequently from both PlayerBall class and GamePanel class
//updates the current location of the ball
public void move(){
y = y + yVelocity;
x = x + xVelocity;
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();
}
public void capSpeed(){
if(xVelocity>speedCap){
xVelocity = speedCap;
} else if(xVelocity<-1*speedCap) {
xVelocity = -1*speedCap;
}
}
//called frequently from the GamePanel class
//draws the current location of the ball to the screen

16
src/Tile.java Normal file
View File

@ -0,0 +1,16 @@
import java.awt.*;
public class Tile {
public int x;
public int y;
public static final int length = 30;
public Tile(int x, int y){
this.x = x;
this.y = y;
}
public void draw(Graphics g){
g.setColor(Color.black);
g.fillRect(x, y, length, length);
}
}