Swtiched to 2D x-scrolling

master
bob 2022-06-02 13:21:29 -04:00
parent 8d7b62c065
commit c8e72b1b65
7 changed files with 88 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

@ -34,7 +34,9 @@ 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;
public GamePanel(){ public GamePanel(){
camera = new Camera(0);
for (int i = 0; i < 11; i++) { for (int i = 0; i < 11; i++) {
try { try {
BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0'))); BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0')));
@ -85,7 +87,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

@ -11,21 +11,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);
} }
} }