Basic physics
parent
e0352d0456
commit
ef61ea475b
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -9,21 +9,24 @@ Implements Runnable interface to use "threading" - let the game do two things at
|
||||||
*/
|
*/
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
public class GamePanel extends JPanel implements Runnable, KeyListener{
|
public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
|
|
||||||
//dimensions of window
|
//dimensions of window
|
||||||
public static final int GAME_WIDTH = 1920;
|
public static final int GAME_WIDTH = 1536;
|
||||||
public static final int GAME_HEIGHT = 1080;
|
public static final int GAME_HEIGHT = 768;
|
||||||
|
|
||||||
public Thread gameThread;
|
public Thread gameThread;
|
||||||
public Image image;
|
public Image image;
|
||||||
public Graphics graphics;
|
public Graphics graphics;
|
||||||
public PlayerBall ball;
|
public PlayerBall ball;
|
||||||
|
|
||||||
|
public static ArrayList<Tile>map = new ArrayList<Tile>();
|
||||||
|
|
||||||
public GamePanel(){
|
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
|
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.setFocusable(true); //make everything in this class appear on the screen
|
||||||
this.addKeyListener(this); //start listening for keyboard input
|
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
|
//call the draw methods in each class to update positions as things move
|
||||||
public void draw(Graphics g){
|
public void draw(Graphics g){
|
||||||
ball.draw(g);
|
ball.draw(g);
|
||||||
|
for(Tile i: map){
|
||||||
|
i.draw(g);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//call the move methods in other classes to update positions
|
//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
|
//handles all collision detection and responds accordingly
|
||||||
public void checkCollision(){
|
public void checkCollision(){
|
||||||
|
ball.isGrounded = false;
|
||||||
|
|
||||||
|
for(Tile i: map){
|
||||||
|
|
||||||
|
}
|
||||||
//force player to remain on screen
|
//force player to remain on screen
|
||||||
if(ball.y<= 0){
|
if(ball.y<= 0){
|
||||||
ball.y = 0;
|
ball.y = 0;
|
||||||
}
|
}
|
||||||
if(ball.y >= GAME_HEIGHT - PlayerBall.BALL_DIAMETER){
|
if(ball.y >= GAME_HEIGHT - PlayerBall.BALL_DIAMETER){
|
||||||
ball.y = GAME_HEIGHT-PlayerBall.BALL_DIAMETER;
|
ball.y = GAME_HEIGHT-PlayerBall.BALL_DIAMETER;
|
||||||
|
ball.yVelocity = 0;
|
||||||
|
ball.isGrounded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ball.x <= 0){
|
if(ball.x <= 0){
|
||||||
ball.x = 0;
|
ball.x = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,17 @@ import java.awt.event.*;
|
||||||
|
|
||||||
public class PlayerBall extends Rectangle{
|
public class PlayerBall extends Rectangle{
|
||||||
|
|
||||||
public int yVelocity;
|
public double yVelocity;
|
||||||
public int xVelocity;
|
public double xVelocity;
|
||||||
public final int 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 static final int BALL_DIAMETER = 20; //size 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
|
//constructor creates ball at given location with given dimensions
|
||||||
public PlayerBall(int x, int y){
|
public PlayerBall(int x, int y){
|
||||||
super(x, y, BALL_DIAMETER, BALL_DIAMETER);
|
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
|
//if the keyboard input isn't any of the options (d, a, w, s), then nothing happens
|
||||||
public void keyPressed(KeyEvent e){
|
public void keyPressed(KeyEvent e){
|
||||||
if(e.getKeyChar() == 'd'){
|
if(e.getKeyChar() == 'd'){
|
||||||
setXDirection(SPEED);
|
rightPressed = true;
|
||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(e.getKeyChar() == 'a'){
|
if(e.getKeyChar() == 'a'){
|
||||||
setXDirection(SPEED*-1);
|
leftPressed = true;
|
||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(e.getKeyChar() == 'w'){
|
if(e.getKeyChar() == 'w'){
|
||||||
setYDirection(SPEED*-1);
|
upPressed = true;
|
||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(e.getKeyChar() == 's'){
|
if(e.getKeyChar() == 's'){
|
||||||
setYDirection(SPEED);
|
downPressed = true;
|
||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,22 +51,19 @@ public class PlayerBall extends Rectangle{
|
||||||
//Makes the ball stop moving in that direction
|
//Makes the ball stop moving in that direction
|
||||||
public void keyReleased(KeyEvent e){
|
public void keyReleased(KeyEvent e){
|
||||||
if(e.getKeyChar() == 'd'){
|
if(e.getKeyChar() == 'd'){
|
||||||
setXDirection(0);
|
rightPressed = false;
|
||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(e.getKeyChar() == 'a'){
|
if(e.getKeyChar() == 'a'){
|
||||||
setXDirection(0);
|
leftPressed = false;
|
||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(e.getKeyChar() == 'w'){
|
if(e.getKeyChar() == 'w'){
|
||||||
setYDirection(0);
|
upPressed = false;
|
||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(e.getKeyChar() == 's'){
|
if(e.getKeyChar() == 's'){
|
||||||
setYDirection(0);
|
downPressed = false;
|
||||||
move();
|
move();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,26 +71,36 @@ public class PlayerBall extends Rectangle{
|
||||||
//called from GamePanel whenever a mouse click is detected
|
//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
|
//changes the current location of the ball to be wherever the mouse is located on the screen
|
||||||
public void mousePressed(MouseEvent e){
|
public void mousePressed(MouseEvent e){
|
||||||
x = e.getX();
|
//x = e.getX();
|
||||||
y = e.getY();
|
// 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(){
|
public void move(){
|
||||||
y = y + yVelocity;
|
y = y + (int)yVelocity;
|
||||||
x = x + xVelocity;
|
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
|
//called frequently from the GamePanel class
|
||||||
//draws the current location of the ball to the screen
|
//draws the current location of the ball to the screen
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue