Reputation: 41
I am making my own version of mario. One problem im running into is how to do the side scrolling part of the game. But i dont quite know how to implement the imageableX and imageableY into the world display. Any help would be much appreciated! Here is what i have so far:
public class World extends JPanel implements Runnable, KeyListener{
private static final int BEGINNING_X = 0, ENDING_X = 10000;
private static final int TOP_Y = 0, BOTTOM_Y = 5000;
private boolean inGame = false;
private Player player;
//set up a new world with a player
public World(Player player){
inGame = true;
this.player = player;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
//the world size
g.drawRect(0, 0, BEGINNING_X, ENDING_X);
}
//check to see if the new x postition is valid on the game board
public boolean isValidXPosition(double x){
if(x < BEGINNING_X || x > ENDING_X){
return false;
}
return true;
}
public double imageableStartX(Player player){
if(player.returnX() - 100 <= BEGINNING_X)
return BEGINNING_X;
else
return player.returnX() - 100;
}
public double imageableTopY(Player player){
if(player.returnY()-100 <= BOTTOM_Y)
return BOTTOM_Y;
else
return BOTTOM_Y - 100;
}
public double imageableEndX(Player player){
if(player.returnX() + 200 <= ENDING_X)
return ENDING_X;
else
return player.getX() + 200;
}
public double imageableBottomY(Player player){
if(player.returnY()+100 >= BOTTOM_Y)
return BOTTOM_Y;
else
return BOTTOM_Y + 100;
}
public void run() {
while(inGame){
//TODO run game code
}
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
player.moveRight();
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
player.moveLeft();
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){
player.setSprint(true);
}
if(e.getKeyCode() == KeyEvent.VK_UP){
player.Jump();
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
player.Descend();
player.setFalling(true);
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){
player.setSprint(false);
}
}
}
Upvotes: 2
Views: 4843
Reputation: 1660
You might find it easier to implement your X and Y position definitions in your paintComponent() method instead of abstracting the process to other methods and passing a Player object every time. Try defining constants for your viewport dimensions (VIEWPORT_WIDTH, VIEWPORT_HEIGHT, etc). In your paintComponent() method, reference these in conjunction with your player's coordinates to position your character on the screen. Be sure to use relative coordinates, as well:
// center the screen on the player, though you can modify it to do otherwise
int viewportX = player.returnX() - VIEWPORT_WIDTH/2;
int viewportY = player.returnY() - VIEWPORT_HEIGHT/2;
g.setColor(Color.WHITE);
g.fillRect(viewportX, viewportY, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
If you want your viewport to remain in the world at all times, even when your character moves to the edges of the screen, do all of your positional calculations normally, but put in a few checks right before actually drawing anything so you can make changes to your viewport location in case it happens to go offscreen.
Good luck.
Upvotes: 1