Super Mario game help

Hey everyone, I was programming a simple mario game but having heaps of trouble limiting the mario to ONE jump only everytime the up button is pressed. I'm getting real sick of him flying off the screen haha, and if there's anyway to reduce the lag of my game it would be appreciated.

I've tried using boolean airborne states etc but can't quite piece it together, here's my code for the mario class:

PImage marioimage;
PImage mariorun;
PImage mariorun2;

Mario myMario;

void setup() {
 size(800, 800);
 smooth();
 noStroke();
 marioimage = loadImage("mario.png"); 
 mariorun = loadImage("mariorun.png");
 mariorun2 = loadImage("mariorun2.png");
 myMario = new Mario(0,575);
}

void draw() {
  background(0);   
  myMario.move();
  myMario.display(); 
}

//KEYBOARD INPUT

void keyPressed() {
  if (key == CODED) {
     if (keyCode == LEFT) {
       myMario.moveLeft = true;
       marioimage = loadImage("mariorun2.png");
       marioimage.resize(80,65);

     } else if(keyCode == RIGHT) {
       myMario.moveRight = true;
       marioimage = loadImage("mariorun.png");
       marioimage.resize(80,65);

     } else if(keyCode == UP) {
       myMario.moveUp = true;
     }
  }
}

void keyReleased() {
  if (key == CODED) {
     if (keyCode == LEFT) {
       myMario.moveLeft = false;
       marioimage = loadImage("mario.png");
       marioimage.resize(60,60);
     } else if(keyCode == RIGHT) {
       myMario.moveRight = false;
       marioimage = loadImage("mario.png");
       marioimage.resize(60,60);
     } else if(keyCode == UP) {
       myMario.moveUp = false;
       myMario.moveDown = true;
     }
  }
}

//PLAYER CLASS

class Mario {
  float xPos;
  float yPos;
  float speed = 2;
  boolean moveLeft, moveRight, moveUp, moveDown = false;

  Mario(float x_in, float y_in) {
    xPos = x_in;
    yPos = y_in; 
  }

  void display() {
    fill(255);
    noStroke();
    image(marioimage, xPos, yPos);
    marioimage.resize(50,60);
  }

  void move() {
    if(moveLeft) xPos -= speed;
    if(moveRight) xPos += speed;
    if(moveUp) yPos -= speed*3;
    if(moveDown) yPos += speed*2;
    if (yPos >= 575) {
    moveDown = false;
    }  
  }
  }

Cheers, Lewis

Answers

  • You shouldn't load resources outside setup()! Here's a "jump" example:
    http://studio.processingtogether.com/sp/pad/export/ro.9LZizxKsIAY4F/latest

  • Any ideas how I could change it? Thanks anyway for the example! :)

  • edited June 2014

    My example got a method called move() just like yours.

    • While it's jumping, it checks whether coordinate y becomes less than JUMP.
    • When it does, it inverts the sign of dir, so it starts falling down instead!
    • Once y becomes greater than FLOOR, it makes dir = 0. Thus stopping the vertical animation!


    void move() {
      if ((y += dir) < JUMP)  dir *= -1;
    
      else if (y > FLOOR) {
        dir = 0;
        y = FLOOR;
      }
    }
    
    • In my example, both JUMP & FLOOR are limit constants for the y coordinate.
    • In your case, floor would be the y position of the platform where the jump starts.
    • And jump would be floor + MAX_JUMP_HEIGHT.
  • GoToLoop is right about loading images on the fly, even more as you have them already loaded.

    Have a currentMarioImage variable, for example, that's the one to display. Then, where you do a loadImage(), just assign the corresponding PImage:

    currentMarioImage = mariorun2; // Or other
    
  • edited June 2014

    Thanks everyone I fixed the problem with some help of my lecturer :) Now I really need help so that if the mario class passes the coin images, that the coin image (xpos,ypos) relocates to the top left of the screen.

    boolean sketchFullScreen() {
      return true;
    }
    
    //Use the minim library
    import ddf.minim.*;
    
    //Global Minim Object Instance
    Minim minim;
    
    //Sound Instances
    AudioPlayer thememusic;
    
    //Goomba image
    PImage goombaimage;
    
    Goomba myGoomba;
    
    //Coin image
    
    PImage coinimage;
    
    //Coin image coordinates
    
    float XPos;
    float YPos;
    
    float xposcoin = 350;
    float xposcoin2 = 400;
    float xposcoin3 = 450;
    float xposcoin4 = 500;
    float xposcoin5 = 550;
    float xposcoin6 = 600;
    
    float yposcoin = 500;
    float yposcoin2 = 500;
    float yposcoin3 = 500;
    float yposcoin4 = 500;
    float yposcoin5 = 500;
    float yposcoin6 = 500;
    
    //Background
    PImage landscape;
    
    //Mario
    PImage marioimage;
    PImage marioStanding;
    PImage marioRunL;
    PImage marioRunR;
    
    Mario myMario;
    
    float gravity = 3;
    float damping = 0.9;
    float jumpSpeed = 40;
    float moveSpeed = 0.8;
    
    float groundYpos = 575;
    
    //Mario Title
    PImage titlemario;
    
    void setup() {
      println("Welcome to Super Mario - Lewis Poll 2014");
    
      minim = new Minim(this);
      //load the sounds
      thememusic = minim.loadFile("themesong.mp3");
      thememusic.play();
    
      size(displayWidth, displayHeight);
      smooth();
      noCursor();
      titlemario = loadImage("MarioTitle.PNG");
      titlemario.resize(800, 250);
      coinimage = loadImage("coin.png");
      coinimage.resize(50, 50);
      landscape = loadImage("background.jpg");
      landscape.resize(displayWidth, displayHeight);
      goombaimage = loadImage("goomba.png");
      myGoomba = new Goomba();
      marioStanding = loadImage("mario.png"); 
      marioRunR   = loadImage("mariorun.png");
      marioRunL  = loadImage("mariorun2.png");
      marioimage = marioStanding;
      myMario = new Mario(0, 575);
    }
    
    void draw() {
      image(landscape, 0, 0);
      image(titlemario, width/4, 20);
      line(0, 634, width, 634);
      strokeWeight(3);
      fill(0);
      rect(300, 634, 100, 80);
    
      myMario.move();
      myMario.display();
      myGoomba.drive();
      myGoomba.display();
    
      image(coinimage, xposcoin, yposcoin);
      image(coinimage, xposcoin2, yposcoin2);
      image(coinimage, xposcoin3, yposcoin3);
    
      image(coinimage, xposcoin4, yposcoin4);
      image(coinimage, xposcoin5, yposcoin5);
      image(coinimage, xposcoin6, yposcoin6);
    
      if (XPos == xposcoin && YPos == yposcoin) {
        xposcoin = 0;
        yposcoin = 0;
      }
      if (XPos == xposcoin2 && YPos == yposcoin2) {
        xposcoin2 = 30;
        yposcoin2 = 0;
      }
      if (XPos == xposcoin3 && YPos == yposcoin3) {
        xposcoin3 = 60;
        yposcoin3 = 0;
      }
      if (XPos == xposcoin4 && YPos == yposcoin4) {
        xposcoin4 = 90;
        yposcoin4 = 0;
      }
      if (XPos == xposcoin5 && YPos == yposcoin5) {
        xposcoin5 = 120;
        yposcoin5 = 0;
      }
      if (XPos == xposcoin6 && YPos == yposcoin6) {
        xposcoin6 = 150;
        yposcoin6 = 0;
      }
    }
    class Goomba {
      float xpos = 400;
      float ypos = 575;
      float xspeed = 5;
    
      void display() {
        stroke(0);
        image(goombaimage, xpos, ypos);
        goombaimage.resize(70, 70);
      }
    
      void drive() {
        xpos = xpos + xspeed;
        if (xpos > width) {
          xspeed=-5;
        }
        if (xpos == 400) {
          xspeed=5;
        }
      }
    }
    
    void keyPressed() {
      if (key == CODED) {
        if (keyCode == LEFT) {
          myMario.moveLeft = true;
        } 
        else if (keyCode == RIGHT) {
          myMario.moveRight = true;
        } 
        else if (keyCode == UP) {
          myMario.moveUp = true;
        }
      }
    }
    
    void keyReleased() {
      if (key == CODED) {
        if (keyCode == LEFT) {
          myMario.moveLeft = false;
        } 
        else if (keyCode == RIGHT) {
          myMario.moveRight = false;
        } 
        else if (keyCode == UP) {
          myMario.moveUp = false;
        }
      }
    }
    
    
    //PLAYER CLASS
    
    class Mario {
      float xPos, xVel;
      float yPos, yVel;
    
      boolean moveLeft, moveRight, moveUp, onGround;
    
      Mario(float x_in, float y_in) {
        xPos = x_in;
        yPos = y_in; 
        moveLeft = false;
        moveRight = false;
        moveUp = false;
        onGround = true;
        xVel = 0;
        yVel = 0;
      }
    
      void display() {
        fill(255);
        noStroke();
        image(marioimage, xPos, yPos);
        marioimage.resize(60, 60);
      }
    
      void move() {
        if (moveLeft) xVel -= moveSpeed;
        if (moveRight) xVel += moveSpeed;
        if (moveUp && onGround) {
          yVel = -jumpSpeed;
          onGround = false;
        }
    
        xPos += xVel;
        yPos += yVel;
        xVel *= damping;
        yVel *= damping;
    
        if ( !onGround )
        {
          yVel += gravity;
        }
    
        if (yPos >= groundYpos) {
          onGround = true;
          yPos = groundYpos;
        }  
        if ( moveRight ) { 
          marioimage = marioRunR;
        }
    
        if ( moveLeft ) { 
          marioimage = marioRunL;
        }
        else if (moveRight == false && moveLeft == false) {
          marioimage = marioStanding;
        }
      }
    }
    
  • Please edit your post, highlight code, hit ctrl-k.

  • Hey man, I have been working on this too. Message me if you would like to see my code

Sign In or Register to comment.