We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to create a platform game where my Image has to jump up the platforms and avoid the lava (flashing ellipse).
Using the code that /* @pjs preload="oldman_3.png"; */ posted i was able to make my image jump. but for the life of me I can not work out how to make my image register that it has found a new platform and can now move along it. In the event that an image was required for my platforms rather than me drawing them using lines i have pasted that image to the bottom of my code.
If anyone can help i would be very grateful, Thank you.
class DinoJumper
{
PImage image;
PVector position;
float direction;
PVector velocity;
float jumpSpeed;
float walkSpeed;
}
void lava() { //SETTING THE FUNCTION FOR THE TRACK
noFill();
noStroke();
strokeWeight(1);
beginShape();
vertex(300, -20);
bezierVertex( 200, 70, 400, 70, 600, 70);
bezierVertex( 600, 120, 800, 120, 1000, 120);
bezierVertex( 1000, 180, 800, 180, 600, 180);
bezierVertex( 600, 220, 400, 220, 200, 220);
bezierVertex( 200, 270, 400, 270, 600, 270);
bezierVertex( 600, 320, 800, 320, 1000, 320 );
bezierVertex( 1000, 370, 800, 370, 600, 370 );
bezierVertex( 600, 420, 400, 420, 200, 420 );
bezierVertex( 200, 470, 400, 470, 600, 470 );
bezierVertex( 600, 520, 800, 520, 1000, 520 );
bezierVertex( 1150, 575, 550, 575, -50, 575);
endShape();
}
void platforms() { //SETTING THE FUNCTION FOR THE PLATFORMS
stroke (0);
strokeWeight (3);
beginShape();
line (200, 70, 600, 70);
line (600, 70, 600, 120);
line (600, 120, 900, 120);
line (1000, 180, 600, 180);
line (600, 180, 600, 220);
line (600, 220, 300, 220);
line (200, 270, 600, 270);
line (600, 270, 600, 320);
line (600, 320, 900, 320);
line (1000, 370, 600, 370);
line (600, 370, 600, 420);
line (600, 420, 300, 420);
line (200, 470, 600, 470);
line (600, 470, 600, 520);
line (600, 520, 1000, 520);
endShape();
}
//references: /* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/92234*@* */
/* @pjs preload="oldman_3.png"; */
DinoJumper Dino;
float left;
float right;
float up;
float down;
float Dx;
float Dy;
float f=0;
boolean is_the_UP_key_down = false; //SETTING THE VARIABLES TO TRACK WHETHER THE KEYS USED IN THE GAME ARE PRESSED OR NOT
boolean is_the_DOWN_key_down = false;
boolean is_the_LEFT_key_down = false;
boolean is_the_RIGHT_key_down = false;
float gravity = .2;
float ground = 575;
void setup ()
{
size (1200, 600);
Dx = 100;
Dy = 575;
Dino = new DinoJumper();
Dino.image = loadImage("testDINO3.png");
Dino.position = new PVector(Dx, Dy);
Dino.direction = -1;
Dino.velocity = new PVector(0, 0);
Dino.jumpSpeed = 5.5;
Dino.walkSpeed = 4;
}
void draw ()
{
background(255);
updateDino();
platforms();
lava();
f+=0.005;
fill(128);
if (f < 1) {
float x = bezierPoint( 300, 200, 400, 600, f);
float y = bezierPoint( -20, 55, 55, 55, f);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 2 ) {
float x = bezierPoint( 600, 600, 800, 1000, f-1);
float y = bezierPoint( 70, 105, 105, 105, f-1);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 3 ) {
float x = bezierPoint( 1000, 1000, 800, 600, f-2);
float y = bezierPoint( 120, 165, 165, 165, f-2);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 4 ) {
float x = bezierPoint( 600, 600, 400, 200, f-3);
float y = bezierPoint( 180, 205, 205, 205, f-3);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 5 ) {
float x = bezierPoint( 200, 200, 400, 600, f-4);
float y = bezierPoint(220, 255, 255, 255, f-4);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 6 ) {
float x = bezierPoint( 600, 600, 800, 1000, f-5);
float y = bezierPoint(270, 305, 305, 305, f-5);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 7 ) {
float x = bezierPoint(1000, 1000, 800, 600, f-6);
float y = bezierPoint(320, 355, 355, 355, f-6);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 8 ) {
float x = bezierPoint(600, 600, 400, 200, f-7);
float y = bezierPoint(370, 405, 405, 405, f-7);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 9 ) {
float x = bezierPoint(200, 200, 400, 600, f-8);
float y = bezierPoint(420, 455, 455, 455, f-8);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 10 ) {
float x = bezierPoint(600, 600, 800, 1000, f-9);
float y = bezierPoint( 470, 505, 505, 505, f-9);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f < 11 ) {
float x = bezierPoint(1000, 1150, 550, -75, f-10);
float y = bezierPoint( 520, 585, 585, 585, f-10);
fill (random(0, 255), random(0, 127), random(0, 80));
ellipse( x, y, 30, 30 );
}
else if ( f > 11 ) {
f = 0;
}
}
void updateDino()
{
// Only apply gravity if above ground (since y positive is down we use < ground)
if (Dino.position.y < ground)
{
Dino.velocity.y += gravity;
}
else
{
Dino.velocity.y = 0;
}
// If on the ground and "jump" keyy is pressed set my upward velocity to the jump speed!
if (Dino.position.y >= ground && up != 0)
{
Dino.velocity.y = -Dino.jumpSpeed;
}
// Wlak left and right. See Car example for more detail.
Dino.velocity.x = Dino.walkSpeed * (left + right);
// We check the nextPosition before actually setting the position so we can
// not move the Dino if he's colliding.
PVector nextPosition = new PVector(Dino.position.x, Dino.position.y);
nextPosition.add(Dino.velocity);
// Check collision with edge of screen and don't move if at the edge
float offset = 0;
if (nextPosition.x > offset && nextPosition.x < (width - offset))
{
Dino.position.x = nextPosition.x;
}
if (nextPosition.y > offset && nextPosition.y < (height - offset))
{
Dino.position.y = nextPosition.y;
}
// See car example for more detail here.
pushMatrix();
translate(Dino.position.x, Dino.position.y);
// Always scale after translate and rotate.
// We're using Dino.direction because a -1 scale flips the image in that direction.
scale(Dino.direction, 1);
imageMode(CENTER);
image(Dino.image, 0, 0);
popMatrix();
if (left>=200&&right<=1100) {
/*if ( is_the_LEFT_key_down ) { //WHAT TO DO IF CERTAIN KEYS ARE PRESSED
Dx -= 5;
Dy-=0.3;
}
if ( is_the_RIGHT_key_down ) {
Dx += 5;
Dy +=0.3;
}
if ( is_the_UP_key_down ) {
Dy -= 5;
}
if ( is_the_DOWN_key_down ) {
Dy += 5;
;
}
}
//Bounc back
if(Dx<=200)
{
Dx=Dx+50;
}
if(Dx>=1100)
{
Dx=Dx-50;
}
*/
}
if (( Dino.position.x>510 && Dino.position.x<540 && Dino.position.y>1000 && Dino.position.y<1125))
{
Dino.position.x = 525;
Dino.position.y = 1100;
}
}
void keyPressed() {
if (keyCode == UP) {
is_the_UP_key_down=true;
up = -1;
}
if (keyCode == DOWN) {
is_the_DOWN_key_down=true;
down = 1;
}
if (keyCode == LEFT) {
is_the_LEFT_key_down=true;
left = -1;
Dy += .25;
Dino.direction = 1;
}
if (keyCode == RIGHT) {
is_the_RIGHT_key_down=true;
right = 1;
Dino.direction = -1;
}
}
void keyReleased() {
if (keyCode == UP) {
is_the_UP_key_down=false;
up=0;
}
if (keyCode == DOWN) {
is_the_DOWN_key_down=false;
down=0;
}
if (keyCode == LEFT) {
is_the_LEFT_key_down=false;
left = 0;
}
if (keyCode == RIGHT) {
is_the_RIGHT_key_down=false;
right = 0;
}
}
Answers
Start small. Can you make your dinosaur bounce around the screen? Can you make it collide with a square?
I wrote some tutorials on collision detection in Processing here, they might help you.
Thank you for your reply. My Dinosaur is able to walk and jump along the bottom of the screen.
I will take a look at your link and see if that can help detect the collision with a square.
Will the collision then enable my dinosaur to "land" on that square (platform)?
Well, collision detection isn't exactly easy or automatic. You have to first understand the principles behind it; you can't just post in code you got somewhere else and hope for the best. When you understand how basic collision detection works, your goal will be much easier.