We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello my name is Jorge and I am new to coding, I have been working on this code for about 3 months and this code is for my Final Project.
I already got my character to move left and right. Can someone please help me on how to make my character jump. Thank You.
Grass a;
Guy b;
Dirt c;
Cloud d;
Red_Spike e;
void setup() {
size(800,400);
a = new Grass();
b = new Guy();
c = new Dirt();
d = new Cloud();
e = new Red_Spike();
}
void draw() {
background(#1DB4ED);
line(0,300,3000,300);
a.display();
b.display();
c.display();
d.display();
e.display();
fill(#FF0808);
textSize(50);
text("Red Spikes", 280, 150);
}
class Grass {
void display(){
fill(#09B212);
rect(0,300,20,20);
rect(20,300,20,20);
rect(40,300,20,20);
rect(60,300,20,20);
rect(80,300,20,20);
rect(100,300,20,20);
rect(120,300,20,20);
rect(140,300,20,20);
rect(160,300,20,20);
rect(180,300,20,20);
rect(200,300,20,20);
rect(220,300,20,20);
rect(240,300,20,20);
rect(260,300,20,20);
rect(280,300,20,20);
rect(300,300,20,20);
rect(320,300,20,20);
rect(340,300,20,20);
rect(360,300,20,20);
rect(380,300,20,20);
rect(400,300,20,20);
rect(420,300,20,20);
rect(440,300,20,20);
rect(460,300,20,20);
rect(480,300,20,20);
rect(500,300,20,20);
rect(520,300,20,20);
rect(540,300,20,20);
rect(560,300,20,20);
rect(580,300,20,20);
rect(600,300,20,20);
rect(620,300,20,20);
rect(640,300,20,20);
rect(660,300,20,20);
rect(680,300,20,20);
rect(700,300,20,20);
rect(720,300,20,20);
rect(740,300,20,20);
rect(760,300,20,20);
rect(780,300,20,20);
rect(800,300,20,20);
rect(820,300,20,20);
rect(840,300,20,20);
rect(860,300,20,20);
rect(880,300,20,20);
rect(900,300,20,20);
rect(920,300,20,20);
rect(940,300,20,20);
rect(960,300,20,20);
rect(980,300,20,20);
rect(1000,300,20,20);
rect(1020,300,20,20);
rect(1040,300,20,20);
rect(1060,300,20,20);
rect(1080,300,20,20);
rect(1100,300,20,20);
rect(1120,300,20,20);
rect(1140,300,20,20);
rect(1160,300,20,20);
rect(1180,300,20,20);
rect(1200,300,20,20);
rect(1220,300,20,20);
rect(1240,300,20,20);
rect(1260,300,20,20);
rect(1280,300,20,20);
rect(1300,300,20,20);
rect(1320,300,20,20);
rect(1340,300,20,20);
rect(1360,300,20,20);
rect(1380,300,20,20);
rect(1400,300,20,20);
rect(1420,300,20,20);
rect(1440,300,20,20);
rect(1460,300,20,20);
rect(1480,300,20,20);
rect(1500,300,20,20);
rect(1520,300,20,20);
rect(1540,300,20,20);
rect(1560,300,20,20);
rect(1580,300,20,20);
rect(1600,300,20,20);
rect(1620,300,20,20);
rect(1640,300,20,20);
rect(1660,300,20,20);
rect(1680,300,20,20);
rect(1700,300,20,20);
rect(1720,300,20,20);
rect(1740,300,20,20);
rect(1760,300,20,20);
rect(1780,300,20,20);
rect(1800,300,20,20);
rect(1820,300,20,20);
rect(1840,300,20,20);
rect(1860,300,20,20);
rect(1880,300,20,20);
rect(1900,300,20,20);
}
}
class Dirt {
void display() {
fill(165,42,42);
rect(0,320,100,80);
rect(100,320,100,80);
rect(200,320,100,80);
rect(300,320,100,80);
rect(400,320,100,80);
rect(500,320,100,80);
rect(600,320,100,80);
rect(700,320,100,80);
rect(800,320,100,80);
rect(900,320,100,80);
rect(1000,320,100,80);
rect(1100,320,100,80);
rect(1200,320,100,80);
rect(1300,320,100,80);
rect(1400,320,100,80);
rect(1500,320,100,80);
rect(1600,320,100,80);
rect(1700,320,100,80);
rect(1800,320,100,80);
rect(1900,320,100,80);
rect(2000,320,100,80);
rect(2100,320,100,80);
rect(2200,320,100,80);
rect(2300,320,100,80);
rect(2400,320,100,80);
rect(2500,320,100,80);
rect(2600,320,100,80);
rect(2700,320,100,80);
rect(2800,320,100,80);
rect(2900,320,100,80);
rect(3000,320,100,80);
rect(3100,320,100,80);
rect(3200,320,100,80);
rect(3300,320,100,80);
rect(3400,320,100,80);
rect(3500,320,100,80);
rect(3600,320,100,80);
rect(3700,320,100,80);
rect(3800,320,100,80);
rect(3900,320,100,80);
rect(4000,320,100,80);
rect(4100,320,100,80);
rect(4200,320,100,80);
rect(4300,320,100,80);
rect(4400,320,100,80);
rect(4500,320,100,80);
rect(4600,320,100,80);
rect(4700,320,100,80);
rect(4800,320,100,80);
rect(4900,320,100,80);
rect(5000,320,100,80);
rect(5100,320,100,80);
rect(5200,320,100,80);
rect(5300,320,100,80);
rect(5400,320,100,80);
rect(5500,320,100,80);
}
}
class Cloud {
void display() {
pushMatrix();
translate(30, 20);
fill(255);
ellipse(60,65,40,40);
ellipse(130,65,40,40);
ellipse(80,80,40,40);
ellipse(80,50,40,40);
ellipse(110,80,40,40);
ellipse(110,50,40,40);
popMatrix();
pushMatrix();
translate(570, 70);
ellipse(60,65,40,40);
ellipse(130,65,40,40);
ellipse(80,80,40,40);
ellipse(80,50,40,40);
ellipse(110,80,40,40);
ellipse(110,50,40,40);
popMatrix();
}
}
class Red_Spike {
void display() {
pushMatrix();
translate(140,25);
fill(#FF0808);
triangle(230,275,258,220,286,275);
popMatrix();
pushMatrix();
translate(572,255);
triangle(30, 45, 38, 30, 46, 45);
popMatrix();
pushMatrix();
translate(592,255);
triangle(30, 45, 38, 30, 46, 45);
popMatrix();
pushMatrix();
translate(692,255);
triangle(30, 45, 38, 30, 46, 45);
popMatrix();
pushMatrix();
translate(712,255);
triangle(30, 45, 38, 30, 46, 45);
popMatrix();
}
}
class Guy {
void display(){
fill(255);
strokeWeight(2);
ellipse(x,j,10,10);
line(x,y,x,f);
line(x,y,i,z);
line(x,y,h,z);
line(x,f,i,g);
line(x,f,h,g);
}
}
void keyPressed(){
if ((key == CODED) && (keyCode == RIGHT)){
x=x+6;
y=y;
f=f;
i=i+6;
// z=z+6;
h=h+6;
// j=j+6;
g=g;
}
if ((key == CODED) && (keyCode == LEFT)){
x=x-6;
y=y;
f=f;
i=i-6;
// z=z-6;
h=h-6;
// j=j-6;
g=g;
}
if ((key == CODED) && (keyCode == UP)){
}
}
Answers
Shameless self-promotion: I wrote a tutorial on collision detection, which includes a section on collision detection between the player and the ground when jumping, available here. Check out the Snapping to an Edge section.