// Player moves left and right, and jumps over logs and rocks. // the game restarts if a log or rock is hit. // simple gravity used to make the rat fall to earth after jumping. // the Player is currently represented by a box :D // I was going to submit a game with a rat jumping over stuff, // hence the funny variable names. XD // michael dawes 2008 // based on example file by ben dalton //declaration of variables float gravity; int ratSize; // it is square for now float ratX, ratY; //position of rat float velocityY; int groundHeight; PImage bg; int logX, logY, logWidth, logHeight; int rockX, rockY, rockWidth, rockHeight; void setup() { size(480,320); bg = loadImage("procbg.jpg"); frameRate(55); // initialise the rat ratX = 190.0; ratY = 32.0; ratSize = 48; groundHeight = 16; velocityY = 2; // set the environment gravity = 0.1; // initialise the log logWidth = 25; logHeight = 110; logX = 120; logY = height - (logHeight + groundHeight); // initialise the rock rockWidth = 38; rockHeight = 38; rockX = 300; rockY = height - (rockHeight + groundHeight); } void draw() { background(bg); // draw the rat rect(int(ratX), int(ratY), ratSize, ratSize); //draw the log rect(logX, logY, logWidth, logHeight); //draw the rock rect(rockX, rockY, rockWidth, rockHeight); // move the rat ratY = ratY + velocityY; velocityY = velocityY + gravity; if (ratY > height - (ratSize + groundHeight)) { ratY = float(height - (ratSize + groundHeight)); velocityY = 0.0; } // restart the game if there is a collision if (collisionTest(int(ratX), int(ratY),ratSize, ratSize, logX, logY,logWidth,logHeight) == true) { setup(); } if (collisionTest(int(ratX), int(ratY),ratSize, ratSize, rockX, rockY,rockWidth,rockHeight) == true) { setup(); } } // test to see if two rectangles are overlapping boolean collisionTest(int aX, int aY, int aW, int aH, int bX, int bY, int bW, int bH) { if (bY + bH < aY || // is the bottom of B above the top of A, or bY > aY + aH || // is the top of B below the bottom of A, or bX + bW < aX || // is the right of B to the left of A, or bX > aX + aW) // is the left of B to the right of A return false; // the rectangles do not overlap - no collision return true; } void keyPressed() { //add for if capslock.. if(key == 'w') { // jump the rat if it is on the ground if (ratY > height - (ratSize + 1 + groundHeight)) { velocityY = -6.0; } } if(key == 'a') { // move the rat left ratX = ratX - 10; } if(key == 'd') { // move the rat right ratX = ratX + 10; } }