We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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! :)
My example got a method called move() just like yours.
dir = 0
. Thus stopping the vertical animation!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:
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.
Please edit your post, highlight code, hit ctrl-k.
done
Hey man, I have been working on this too. Message me if you would like to see my code