Merge time

master
bob 2022-06-01 14:39:10 -04:00
parent 8a3abaf889
commit 2c5cbf5c72
6 changed files with 28 additions and 11 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.

View File

@ -50,6 +50,11 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
}
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', spriteArray); //create a player controlled player, set start location to middle of screen
map.add(new Tile(1000, 700));
map.add(new Tile(700, 600));
map.add(new Tile(1000, 500));
map.add(new Tile(700, 400));
map.add(new Tile(1000, 300));
map.add(new Tile(700, 200));
this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input
@ -96,31 +101,28 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
player.isGrounded = false;
for (Tile i : map) {
i.collide(player);
}
//force player to remain on screen
if (player.y <= 0) {
player.y = 0;
}
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
player.yVelocity = 0;
player.isGrounded = true;
}
if (player.x <= 0) {
player.x = 0;
}
if (player.x <= 0) {
player.x = 0;
}
}
if (player.x + Player.PLAYER_WIDTH >= GAME_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
public void run(){

View File

@ -12,9 +12,9 @@ public class GenericSprite extends Rectangle{
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 WIDTH = 20; //size of ball
public static final int HEIGHT = 20; //size of ball
public final double speedCap = 5; //Speed cap of ball
public int WIDTH = 20; //size of ball
public int HEIGHT = 20; //size of ball
public boolean rightPressed = false;
public boolean leftPressed = false;
public boolean upPressed= false;
@ -22,7 +22,7 @@ public class GenericSprite extends Rectangle{
public boolean isGrounded = false;
//constructor creates ball at given location with given dimensions
public GenericSprite(int x, int y, int height, int width){
super(x, y, height, width);
super(x, y, height, width);WIDTH = width; HEIGHT = height;
}
//called from GamePanel when any keyboard input is detected

View File

@ -9,6 +9,21 @@ public class Tile {
this.y = y;
}
//Actions when tile interacts with sprites
public void collide(GenericSprite s){
if(s.x+s.WIDTH>x&&s.x<x+length&&s.y+s.HEIGHT>=y&&s.y<=y+length){
if(s.x+s.WIDTH>x+20&&s.x<x+length-20&&y-s.y-s.HEIGHT<4){
s.isGrounded = true;
s.yVelocity = 0;
s.y = y-s.HEIGHT;
}
if(s.x+s.WIDTH<x+length/2){
s.x = x-s.WIDTH;
} else if(s.x>x+length-length/2){
s.x = x+length;
}
}
}
public void draw(Graphics g){
g.setColor(Color.black);
g.fillRect(x, y, length, length);