Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/GamePanel.java
master
John 2022-06-02 13:24:01 -04:00
commit 96e1847d41
7 changed files with 89 additions and 58 deletions

7
src/Camera.java Normal file
View File

@ -0,0 +1,7 @@
public class Camera {
public int x;
public Camera(int x){
this.x = x;
}
}

View File

@ -35,12 +35,15 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public static ArrayList<Tile>map = new ArrayList<Tile>(); public static ArrayList<Tile>map = new ArrayList<Tile>();
public static Camera camera;
// image imports begin here // image imports begin here
public BufferedImage backgroundImage = getImage("img/backgrounds/pointyMountains.png"); public BufferedImage backgroundImage = getImage("img/backgrounds/pointyMountains.png");
public BufferedImage box = getImage("img/tiles/boxes/box.png"); public BufferedImage box = getImage("img/tiles/boxes/box.png");
public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.png"); public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.png");
public GamePanel() throws IOException, SpriteException { public GamePanel() throws IOException, SpriteException {
camera = new Camera(0);
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT); background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT);
for (int i = 0; i < 11; i++) { for (int i = 0; i < 11; i++) {
try { try {
@ -94,7 +97,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
for(Tile i: map){ for(Tile i: map){
i.draw(g); i.draw(g);
} }
g.drawString(camera.x+" "+map.get(0).x,100,100);
} }
//call the move methods in other classes to update positions //call the move methods in other classes to update positions

View File

@ -29,44 +29,13 @@ public class GenericSprite extends Rectangle{
//updates the direction of the ball based on user input //updates the direction of the ball based on user input
//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'){
rightPressed = true;
}
if(e.getKeyChar() == 'a'){
leftPressed = true;
}
if(e.getKeyChar() == 'w'){
upPressed = true;
}
if(e.getKeyChar() == 's'){
downPressed = true;
}
move();
} }
//called from GamePanel when any key is released (no longer being pressed down) //called from GamePanel when any key is released (no longer being pressed down)
//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'){
rightPressed = false;
move();
}
if(e.getKeyChar() == 'a'){
leftPressed = false;
move();
}
if(e.getKeyChar() == 'w'){
upPressed = false;
move();
}
if(e.getKeyChar() == 's'){
downPressed = false;
move();
}
} }
//called from GamePanel whenever a mouse click is detected //called from GamePanel whenever a mouse click is detected
@ -78,20 +47,6 @@ public class GenericSprite extends Rectangle{
public void move(){ public void move(){
y = y + (int)yVelocity;
x = x + (int)xVelocity;
if(rightPressed){
xVelocity+=1;
}
if(leftPressed) {
xVelocity -= 1;
}
if(upPressed&isGrounded){
yVelocity = -10;
}
xVelocity*=0.9;
yVelocity+=0.3;
capSpeed();
} }
public void capSpeed(){ public void capSpeed(){

5
src/Level1 Normal file
View File

@ -0,0 +1,5 @@
10000000
01101101
00000000
00000000
00010000

8
src/MapReader.java Normal file
View File

@ -0,0 +1,8 @@
import java.io.File;
import java.util.ArrayList;
public class MapReader {
public static void inputMap(ArrayList<Tile> map, File file){
}
}

View File

@ -32,17 +32,69 @@ public class Player extends GenericSprite {
// moves paddle when key is pressed // moves paddle when key is pressed
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
super.keyPressed(e); if(e.getKeyChar() == 'd'){
rightPressed = true;
}
if(e.getKeyChar() == 'a'){
leftPressed = true;
}
if(e.getKeyChar() == 'w'){
upPressed = true;
}
if(e.getKeyChar() == 's'){
downPressed = true;
}
move();
} }
// stops moving paddle when key is released // stops moving paddle when key is released
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
super.keyReleased(e); if(e.getKeyChar() == 'd'){
rightPressed = false;
move();
}
if(e.getKeyChar() == 'a'){
leftPressed = false;
move();
}
if(e.getKeyChar() == 'w'){
upPressed = false;
move();
}
if(e.getKeyChar() == 's'){
downPressed = false;
move();
}
} }
// calls parent // calls parent
public void move() { public void move(){
super.move(); y = y + (int)yVelocity;
GamePanel.camera.x = GamePanel.camera.x + (int)xVelocity;
if(rightPressed){
xVelocity+=1;
}
if(leftPressed) {
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;
}
} }
public int draw(Graphics g, int frame) { public int draw(Graphics g, int frame) {

View File

@ -12,21 +12,22 @@ public class Tile {
//Actions when tile interacts with sprites //Actions when tile interacts with sprites
public void collide(GenericSprite s){ public void collide(GenericSprite s){
if(s.x+s.WIDTH>x&&s.x<x+length&&s.y+s.HEIGHT>=y&&s.y<=y+length){ int realX = x-GamePanel.camera.x;
if(s.x+s.WIDTH>x+20&&s.x<x+length-20&&y-s.y-s.HEIGHT<4){ if(s.x+s.WIDTH>realX&s.x<realX+length&&s.y+s.HEIGHT>=y&&s.y<=y+length){
if(s.x+s.WIDTH>realX+20&&s.x<realX+length-20&&y-s.y-s.HEIGHT<4){
s.isGrounded = true; s.isGrounded = true;
s.yVelocity = 0; s.yVelocity = 0;
s.y = y-s.HEIGHT; s.y = y-s.HEIGHT;
} }
if(s.x+s.WIDTH<x+length/2){ if(s.x+s.WIDTH<realX+length/2){
s.x = x-s.WIDTH; GamePanel.camera.x = x-s.WIDTH-GamePanel.GAME_WIDTH/2;
} else if(s.x>x+length-length/2){ } else if(s.x>realX+length-length/2){
s.x = x+length; GamePanel.camera.x = x+length-GamePanel.GAME_WIDTH/2;
} }
} }
} }
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-GamePanel.camera.x, y, length, length);
} }
} }