Merge remote-tracking branch 'origin/master'
# Conflicts: # out/production/final/GamePanel.class # out/production/final/GenericSprite.classmaster
commit
f6fe5def83
|
@ -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.
|
@ -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
|
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(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.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
|
||||||
|
|
||||||
|
@ -96,31 +101,28 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
player.isGrounded = false;
|
player.isGrounded = false;
|
||||||
|
|
||||||
for (Tile i : map) {
|
for (Tile i : map) {
|
||||||
|
i.collide(player);
|
||||||
}
|
}
|
||||||
//force player to remain on screen
|
//force player to remain on screen
|
||||||
if (player.y <= 0) {
|
if (player.y <= 0) {
|
||||||
player.y = 0;
|
player.y = 0;
|
||||||
}
|
}
|
||||||
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
|
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.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
|
||||||
player.yVelocity = 0;
|
player.yVelocity = 0;
|
||||||
player.isGrounded = true;
|
player.isGrounded = true;
|
||||||
}
|
}
|
||||||
if (player.x <= 0) {
|
if (player.x <= 0) {
|
||||||
player.x = 0;
|
player.x = 0;
|
||||||
|
}
|
||||||
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(){
|
||||||
|
|
|
@ -12,9 +12,9 @@ public class GenericSprite extends Rectangle{
|
||||||
public double yVelocity;
|
public double yVelocity;
|
||||||
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 = 5; //Speed cap of ball
|
||||||
public static final int WIDTH = 20; //size of ball
|
public int WIDTH = 20; //size of ball
|
||||||
public static final int HEIGHT = 20; //size of ball
|
public 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;
|
||||||
|
@ -22,7 +22,7 @@ public class GenericSprite extends Rectangle{
|
||||||
public boolean isGrounded = false;
|
public boolean isGrounded = false;
|
||||||
//constructor creates ball at given location with given dimensions
|
//constructor creates ball at given location with given dimensions
|
||||||
public GenericSprite(int x, int y, int height, int width){
|
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
|
//called from GamePanel when any keyboard input is detected
|
||||||
|
|
|
@ -9,6 +9,21 @@ public class Tile {
|
||||||
this.y = y;
|
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){
|
public void draw(Graphics g){
|
||||||
g.setColor(Color.black);
|
g.setColor(Color.black);
|
||||||
g.fillRect(x, y, length, length);
|
g.fillRect(x, y, length, length);
|
||||||
|
|
Loading…
Reference in New Issue