Movement/color detection problem
in
Programming Questions
•
10 months ago
I was trying to make a mario game and i had some problem with movement. If you hit the d or a key then you jump in a parabola even though you are standing still. nI was wondering how to jump in a parabola while moving but jump straight while standing still. Also i used color detection but when i land on a platform it puts me at different altitudes.
Heres the code if you need a commentated version let me know.
float mariox;
float marioy;
float fallspeed = 7.8;
float speed = 1;
boolean left = false;
boolean right = false;
boolean jump = false;
boolean move = false;
float runspeed = 0.1;
int jumptimer = 50;
int dir = 0;
boolean EC = false;
int fall = 0;
float gravity = .3;
PFont scoreFont;
color c = 0;
void setup () {
scoreFont = createFont("Arial", 16, true);
size (800, 800);
smooth();
mariox = 0;
marioy = 795;
}
void draw () {
background (0);
stroke (255);
fill (0);
rect (mariox, marioy, 5, 5);
fill (255);
text (marioy, 5, 10);
text (dir, 5, 20);
text (c, 5, 30);
text (fall, 5, 40);
text (speed, 5, 50);
text (runspeed, 5, 80);
int a = round(mariox);
int b = round(marioy);
text (a, 5, 60);
text (b, 5, 70);
rect (50, 720, 150, 40);
rect (200, 670, 220, 40);
fill (20);
rect (50, 730, 150, 30);
if (keyPressed) {
if (key == 'a') {
c = get(a-5, b);
dir = 2;
mariox -= runspeed;
runspeed = runspeed ;
}
if (key == 'd') {
mariox+=5;
c = get(a-5, b);
dir = 1;
mariox += runspeed;
runspeed = runspeed;
}
if (key == 'w'&& EC == false) {
jump = true;
move = true;
fall = 0;
}
}
if (runspeed >= 2){
}
if (jump == true ) {
speed = -7.8;
fall = 0;
jump = false;
EC = true;
}
if (move == true && (dir == 1) ) {
marioy = marioy + speed;
speed = speed + gravity;
mariox = mariox + 5;
}
if (move == true && (dir == 2) ) {
marioy = marioy + speed;
speed = speed + gravity;
mariox = mariox - 5;
}
if (move == true && (dir == 0) ) {
marioy = marioy + speed;
speed = speed + gravity;
}
if (marioy > 795) {
dir = 0 ;
jumptimer = 50;
marioy = 795;
move = false;
fall = 0;
EC = false;
speed = 7.8;
}
if (marioy <= 795) {
c = get(a+5, b+21);
}
if (jumptimer <= 0 && dir == 1) {
move = false;
jump = false;
marioy = marioy + speed;
mariox = mariox + 5;
}
if (jumptimer <= 0 && dir == 0) {
move = false;
jump = false;
marioy = marioy + speed;
}
if (jumptimer <= 0 && dir == 2) {
move = false;
jump = false;
marioy = marioy + speed;
mariox = mariox - 5;
}
if (c == -1) {
fall = 1;
move = false;
EC = false;
jump = false;
speed = 7.8;
marioy = marioy + 0.1;
}
if (speed > 7.8) {
speed = 7.8;
}
if (c == -16777216 && fall == 1) {
mariox = mariox + 5;
marioy = marioy + fallspeed;
speed = speed + 0.3;
}
}
1