How to make the blocks fall?

This is the current function to make the blocks fall, however it isn't working. Any ideas?

void fall() { for (int i=0;i<10;i++) { for (int j=19;j>-1;j--) { if (motion[i][j]==1) { if (j+1==20) { motion[i][j]=0; drop=true; } else if (motion[i][j+1]==0) { motion[i][j]=0; drop=true; } else { playspace[i][j]=0; playspace[i][j-1]=1; motion[i][j]=0; motion[i][j-1]=1; } } } } } 
submitted by /u/detetris
[link] [comments]

How to make the blocks fall?

This is the current function to make the blocks fall, however it isn't working. Any ideas?

void fall() { for (int i=0;i<10;i++) { for (int j=19;j>-1;j--) { if (motion[i][j]==1) { if (j+1==20) { motion[i][j]=0; drop=true; } else if (motion[i][j+1]==0) { motion[i][j]=0; drop=true; } else { playspace[i][j]=0; playspace[i][j-1]=1; motion[i][j]=0; motion[i][j-1]=1; } } } } } 
submitted by /u/detetris
[link] [comments]

(school) python keyPressed()() while another function is running

I am making a math game and in it the user has to click on the "enemies" to pull up a question and I was using KeyPressed to do this but now after the enemy is clicked on and the function to display and answer the question it running the keyPressed does not work what are some ways around this.

def keyPressed(): global userinput global value global user_answer if key == "d": user_answer = int(userinput) userinput = '' else: userinput = userinput + key print (userinput) def mouseClicked(): for enemy in enemy_list: if mouseX >= enemy.x and mouseX <= enemy.x+17.5 and mouseY >= enemy.y and mouseY <= enemy.y+17.5: enemy.question_display() goal_time = timer() real_time = timer() while real_time <= goal_time+10: real_time = timer() enemy.answer() 
submitted by /u/nikkibear44
[link] [comments]

How to make lava rise from the ground slowly and die on collision? [FIXED CODE]

I want to make lava rise from the ground on every level and if you touch it you lose all you levels. Here is the code

// Global game variables PImage backgroundpic1, backgroundpic2,backgroundpic3,platform1,coinphot, coin2, hearth,bulletphot,bulletphot2; Hero h; Shooter s; ArrayList<Coin> c; ArrayList<Walker> walkers; ArrayList<Shooter> shooters; ArrayList<Bullet> bullets; PImage [] spriteImages,walkerImages,shooterImages; int frames, zombieframes,shooterframes,life,startingcoordx,startingcoordy,counter,bottomPane;; float heroX, heroY, heroSpeedVert, gravity; boolean runonce,left, right, up, down, space, shift; Platform [] platforms; int activeLevel = 0,coincounter = 0; void setup() { size(800, 600); //platform tile images. platform1 = loadImage("ui/platform_long.png"); backgroundpic1 = loadImage("ui/bg.png"); //background image for level1 backgroundpic2 = loadImage("ui/bg2.png"); //background image for level2 backgroundpic3 = loadImage("ui/bg3.png"); //background image for level3 coinphot = loadImage("ui/coin.png"); hearth = loadImage("ui/life.png"); bulletphot = loadImage("ui/bullet.png"); bulletphot2 = loadImage("ui/bullet2.png"); coinphot.resize(30, 30); hearth.resize(30, 30); bulletphot.resize(20,15); bulletphot2.resize(20,15); startingcoordx = 0; startingcoordy = 575; h = new Hero(startingcoordx,startingcoordy); frames = 24; zombieframes = 2; shooterframes = 2; life = 3; spriteImages = new PImage[frames]; for (int i = 1; i<frames; i++) { spriteImages[i]=loadImage("hero/person"+i+".png"); spriteImages[i].resize(40, 50); } walkerImages = new PImage[zombieframes]; for (int i = 0; i<zombieframes; i++) { walkerImages[i]=loadImage("zombie/walk"+i+".png"); walkerImages[i].resize(40, 50); } shooterImages = new PImage[shooterframes]; for (int i = 0; i<shooterframes; i++) { shooterImages[i]=loadImage("shooter/shooter"+i+".png"); shooterImages[i].resize(40, 50); } //boolean things left = false; right = false; up = false; down = false; space = false; shift = false; runonce = true; } void startMenu() { /*-------------------DESIGNING THE MAIN MENU-------------------------*/ background(236, 240, 241); textAlign(CENTER); fill(52, 73, 94); textSize(70); text("JUMPER", width/2, height/2); textSize(15); text("Click to start", width/2, height-30); h.x = startingcoordx; h.y = startingcoordy; runonce = true; coincounter = 0; life = 3; /*-------------------ENDING OF MAIN MENU---------------------------*/ } void YouWon() { /*-------------------DESIGNING THE WINNING MENU-------------------------*/ PImage star,star2; star = loadImage("ui/star.png"); star2 = loadImage("ui/star.png"); star.resize(100,100); star2.resize(50,50); background(236, 240, 241); image(star,(width/2)-50, (height/2)-200); image(star2,(width/2)-100, (height/2)-150); image(star2,(width/2)+50, (height/2)-150); textAlign(CENTER); fill(52, 73, 94); textSize(70); text("Congratulations", width/2, height/2); textSize(15); text("Click to start game again", width/2, height-30); h.x = startingcoordx; h.y = startingcoordy; runonce = true; coincounter = 0; life = 3; /*-------------------ENDING OF GAME WIN MENU---------------------------*/ } void level1ScreenGen() { /* ----------------- DESIGNING THE LEVEL 1----------------------- */ background(backgroundpic1); // how many platforms will be used? platforms = new Platform[18]; //ground floor platforms platforms[0] = new Platform (0, 580, "safe"); platforms[1] = new Platform (120, 580, "coin"); platforms[2] = new Platform (240, 580, "walker"); platforms[3] = new Platform (360, 580, "safe"); platforms[4] = new Platform (480, 580, "walker"); platforms[5] = new Platform (600, 580, "walker"); platforms[6] = new Platform (720, 580, "coin"); // all other platforms to jump on platforms[7] = new Platform (0, 290, "coin"); platforms[8] = new Platform (360, 490, "safe"); platforms[9] = new Platform (750, 250, "safe"); platforms[10] = new Platform (250, 330, "safe"); platforms[11] = new Platform (0, 120, "safe"); platforms[12] = new Platform (350, 90, "coin"); platforms[13] = new Platform (580, 370, "coin"); platforms[14] = new Platform (460, 250, "safe"); platforms[15] = new Platform (700, 100, "walker"); platforms[16] = new Platform (680, 100, "coin"); platforms[17] = new Platform (560, 370, "walker"); /* ------------------------ END OF LEVEL DESIGNING ---------------*/ if (runonce) { c = new ArrayList<Coin>(); walkers = new ArrayList<Walker>(); for (int i = 0; i < platforms.length; ++i) { if (platforms[i].typeof == "coin") { c.add(new Coin(platforms[i].x+50, platforms[i].y-30)); } else if (platforms[i].typeof == "walker") { walkers.add(new Walker(int(platforms[i].x), int(platforms[i].y-50))); } } runonce = false; } } void level2ScreenGen() { /* ----------------- DESIGNING THE LEVEL 2----------------------- */ background(backgroundpic2); // how many platforms will be used? platforms = new Platform[21]; platforms[0] = new Platform (0, 580, "safe"); platforms[1] = new Platform (120, 580, "safe"); platforms[2] = new Platform (40, 580, "safe"); platforms[3] = new Platform (360, 580, "safe"); platforms[4] = new Platform (480, 580, "safe"); platforms[5] = new Platform (600, 580, "safe"); platforms[6] = new Platform (700, 580, "safe"); platforms[7] = new Platform (240, 580, "safe"); platforms[8] = new Platform (600, 430, "coin"); platforms[9] = new Platform (690, 430, "shooter"); platforms[10] = new Platform (0, 390, "shooter"); platforms[11] = new Platform (80, 180, "safe"); platforms[12] = new Platform (0, 50, "shooter"); platforms[13] = new Platform (690, 250, "shooter"); platforms[14] = new Platform (460, 250, "safe"); platforms[15] = new Platform (337, 369, "coin"); platforms[16] = new Platform (230, 50, "coin"); platforms[17] = new Platform (220, 487, "safe"); platforms[18] = new Platform (700, 106, "coin"); platforms[19] = new Platform (590, 312, "coin"); platforms[20] = new Platform (474, 76, "safe"); /* ------------------------ END OF LEVEL 2 DESIGNING ---------------*/ if (runonce) { c = new ArrayList<Coin>(); shooters = new ArrayList<Shooter>(); bullets = new ArrayList<Bullet>(); for (int i = 0; i < platforms.length; ++i) { if (platforms[i].typeof == "coin") { c.add(new Coin(platforms[i].x+50, platforms[i].y-30)); } else if (platforms[i].typeof == "shooter") { shooters.add(new Shooter(int(platforms[i].x), int(platforms[i].y-50))); bullets.add(new Bullet(int(platforms[i].x), int(platforms[i].y-30))); } } runonce = false; } } void level3ScreenGen() { /* ----------------- DESIGNING THE LEVEL 3----------------------- */ background(backgroundpic3); // how many platforms will be used? platforms = new Platform[19]; //ground floor platforms platforms[0] = new Platform (0, 580, "safe"); platforms[1] = new Platform (120, 580, "safe"); platforms[2] = new Platform (240, 580, "safe"); platforms[3] = new Platform (360, 580, "safe"); platforms[4] = new Platform (480, 580, "safe"); platforms[5] = new Platform (600, 580, "safe"); platforms[6] = new Platform (720, 580, "safe"); // all other platforms to jump on platforms[7] = new Platform (0, 400, "coin"); platforms[8] = new Platform (331, 525, "safe"); platforms[9] = new Platform (462, 431, "safe"); platforms[10] = new Platform (318, 321, "coin"); platforms[11] = new Platform (205, 207, "walker"); platforms[12] = new Platform (0, 50, "coin"); platforms[13] = new Platform (740, 430, "shooter"); platforms[14] = new Platform (741, 289, "shooter"); platforms[15] = new Platform (699, 116, "coin"); platforms[16] = new Platform (423, 208, "walker"); platforms[17] = new Platform (324, 70, "coin"); platforms[18] = new Platform (385, 70, "walker"); /* ------------------------ END OF LEVEL 3 DESIGNING ---------------*/ if (runonce) { c = new ArrayList<Coin>(); shooters = new ArrayList<Shooter>(); bullets = new ArrayList<Bullet>(); walkers = new ArrayList<Walker>(); for (int i = 0; i < platforms.length; ++i) { if (platforms[i].typeof == "coin") { c.add(new Coin(platforms[i].x+50, platforms[i].y-30)); } if (platforms[i].typeof == "shooter") { shooters.add(new Shooter(int(platforms[i].x), int(platforms[i].y-50))); bullets.add(new Bullet(int(platforms[i].x), int(platforms[i].y-30))); } if (platforms[i].typeof == "walker") { walkers.add(new Walker(int(platforms[i].x), int(platforms[i].y-50))); } } runonce = false; } } void CoinCounter() { image(coinphot, 700, 5); fill(52, 73, 94); textSize(20); text(coincounter, 770, 25); if (coincounter == 5) { activeLevel+=1; h.x = startingcoordx; h.y = startingcoordy; runonce = true; coincounter = 0; System.gc(); } } void LifeBar() { int distance = 0; int xcord = 600; ; for (int i=0; i< life; i++) { image(hearth, xcord+distance, 5); distance+=30; } if (life == 0) { activeLevel=0; coincounter = 0; life=3; } } void commonThings() { h.update(); for (int i = 0; i < platforms.length; ++i) { rectangleCollisions(h, platforms[i]); h.checkPlatforms(); } h.checkBoundaries(); for (int i = 0; i < platforms.length; ++i) { platforms[i].display(platform1); } for (int k = c.size()-1; k >= 0; k--) { CoinCollisions(h, c.get(k)); if (h.CoinCollisionSide != "none") { c.remove(k); coincounter++; } } for (int k = c.size()-1; k >= 0; k--) { c.get(k).display(coinphot); } if (activeLevel == 1) { for (int k= walkers.size()-1; k>=0; k--) { WalkerCollision(h, walkers.get(k)); walkers.get(k).display(); if (h.WalkerCollisionSide != "none") { h.x = startingcoordx; h.y = startingcoordy; life--; } } } else if (activeLevel == 2) { for (int k = shooters.size()-1; k >= 0; k--) { bullets.get(k).display(bulletphot); BulletCollisions(h,bullets.get(k)); shooters.get(k).display(); if (h.BulletCollisionSide != "none") { h.x = startingcoordx; h.y = startingcoordy; life--; } } } else if (activeLevel == 3) { for (int k= walkers.size()-1; k>=0; k--) { WalkerCollision(h, walkers.get(k)); walkers.get(k).display(); if (h.WalkerCollisionSide != "none") { h.x = startingcoordx; h.y = startingcoordy; life--; } } for (int k = shooters.size()-1; k >= 0; k--) { bullets.get(k).display(bulletphot); BulletCollisions(h,bullets.get(k)); shooters.get(k).display(); if (h.BulletCollisionSide != "none") { h.x = startingcoordx; h.y = startingcoordy; life--; } } } h.display(); } void draw() { if (activeLevel == 0) { startMenu(); } else if (activeLevel == 1) { level1ScreenGen(); commonThings(); CoinCounter(); LifeBar(); } else if (activeLevel == 2) { level2ScreenGen(); commonThings(); CoinCounter(); LifeBar(); } else if (activeLevel == 3) { level3ScreenGen(); commonThings(); CoinCounter(); LifeBar(); } else if (activeLevel > 3) { YouWon(); } } // If user clicked mouse on main menu we sending him to first level. void mousePressed() { if (activeLevel == 0) { activeLevel=1; } else if (activeLevel > 3) { activeLevel=1; } } // catching the keyboard direction buttons. void keyPressed() { switch (keyCode) { case 37://left left = true; break; case 39://right right = true; break; case 38://up up = true; break; case 40://down down = true; break; case 32: //space space = true; break; case 16: //shift shift = true; } } void keyReleased() { switch (keyCode) { case 37://left left = false; break; case 39://right right = false; break; case 38://up up = false; break; case 40://down down = false; break; case 32: //space space = false; break; case 16: //shift shift = false; } } // checking collision between our Hero and platform tiles. void rectangleCollisions(Hero r1, Platform r2) { ////r1 is the hero ////r2 is the platform tile float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2); float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2); float combinedHalfWidths = r1.halfWidth + r2.halfWidth; float combinedHalfHeights = r1.halfHeight + r2.halfHeight; if (abs(dx) < combinedHalfWidths) { ////a collision may be happening ////now check on the y axis if (abs(dy) < combinedHalfHeights) { ////excellent. they are overlapping //determine the overlap on each axis float overlapX = combinedHalfWidths - abs(dx); float overlapY = combinedHalfHeights - abs(dy); ////the collision is on the axis with the ////SMALLEST about of overlap if (overlapX >= overlapY) { if (dy > 0) { r1.collisionSide = "top"; ////move the rectangle back to eliminate overlap ////before calling its display to prevent ////drawing object inside each other r1.y += overlapY; // println("collisionSide: "+r1.collisionSide); } else { r1.collisionSide = "bottom"; r1.y -= overlapY; // println("collisionSide: "+r1.collisionSide); } } else { if (dx > 0) { r1.collisionSide = "left"; r1.x += overlapX; // println("collisionSide: "+r1.collisionSide); } else { r1.collisionSide = "right"; r1.x -= overlapX; // println("collisionSide: "+r1.collisionSide); } } } else { r1.collisionSide = "none"; } } else { r1.collisionSide = "none"; } } void CoinCollisions(Hero r1, Coin r2) { ////r1 is the player ////r2 is the platform rectangle float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2); float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2); float combinedHalfWidths = r1.halfWidth + r2.halfWidth; float combinedHalfHeights = r1.halfHeight + r2.halfHeight; if (abs(dx) < combinedHalfWidths) { ////a collision may be happening ////now check on the y axis if (abs(dy) < combinedHalfHeights) { ////excellent. they are overlapping //determine the overlap on each axis float overlapX = combinedHalfWidths - abs(dx); float overlapY = combinedHalfHeights - abs(dy); ////the collision is on the axis with the ////SMALLEST about of overlap if (overlapX >= overlapY) { if (dy > 0) { r1.CoinCollisionSide = "top"; ////move the rectangle back to eliminate overlap ////before calling its display to prevent ////drawing object inside each other // r1.y += overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.CoinCollisionSide = "bottom"; // r1.y -= overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } else { if (dx > 0) { r1.CoinCollisionSide = "left"; // r1.x += overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.CoinCollisionSide = "right"; // r1.x -= overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } } else { r1.CoinCollisionSide = "none"; } } else { r1.CoinCollisionSide = "none"; } // return CoinCollisionSide; } void BulletCollisions(Hero r1, Bullet r2) { ////r1 is the player ////r2 is the platform rectangle float dx = (r1.x+r1.w/2) - (r2.x4+r2.w4/2); float dy = (r1.y+r1.h/2) - (r2.y4+r2.h4/2); float combinedHalfWidths = r1.halfWidth + r2.halfWidth; float combinedHalfHeights = r1.halfHeight + r2.halfHeight; if (abs(dx) < combinedHalfWidths) { ////a collision may be happening ////now check on the y axis if (abs(dy) < combinedHalfHeights) { ////excellent. they are overlapping //determine the overlap on each axis float overlapX = combinedHalfWidths - abs(dx); float overlapY = combinedHalfHeights - abs(dy); ////the collision is on the axis with the ////SMALLEST about of overlap if (overlapX >= overlapY) { if (dy > 0) { r1.BulletCollisionSide = "top"; ////move the rectangle back to eliminate overlap ////before calling its display to prevent ////drawing object inside each other // r1.y += overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.BulletCollisionSide = "bottom"; // r1.y -= overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } else { if (dx > 0) { r1.BulletCollisionSide = "left"; // r1.x += overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.BulletCollisionSide = "right"; // r1.x -= overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } } else { r1.BulletCollisionSide = "none"; } } else { r1.BulletCollisionSide = "none"; } // return CoinCollisionSide; } void WalkerCollision(Hero r1, Walker r2) { ////r1 is the player ////r2 is the platform rectangle float dx = (r1.x+r1.w/2) - (r2.x2+r2.w2/2); float dy = (r1.y+r1.h/2) - (r2.y2+r2.h2/2); float combinedHalfWidths = r1.halfWidth + r2.half_Width; float combinedHalfHeights = r1.halfHeight + r2.half_Height; if (abs(dx) < combinedHalfWidths) { ////a collision may be happening ////now check on the y axis if (abs(dy) < combinedHalfHeights) { ////excellent. they are overlapping //determine the overlap on each axis float overlapX = combinedHalfWidths - abs(dx); float overlapY = combinedHalfHeights - abs(dy); ////the collision is on the axis with the ////SMALLEST about of overlap if (overlapX >= overlapY) { if (dy > 0) { r1.WalkerCollisionSide = "top"; ////move the rectangle back to eliminate overlap ////before calling its display to prevent ////drawing object inside each other // r1.y += overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.WalkerCollisionSide = "bottom"; // r1.y -= overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } else { if (dx > 0) { r1.WalkerCollisionSide = "left"; // r1.x += overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.WalkerCollisionSide = "right"; // r1.x -= overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } } else { r1.WalkerCollisionSide = "none"; } } else { r1.WalkerCollisionSide = "none"; } // return CoinCollisionSide; } class Platform { float x, y, w, h; String typeof; float halfWidth, halfHeight; Platform(float _x, float _y, String _typeof ) { x = _x; y = _y; w = 130; h = 33; typeof = _typeof; halfWidth = w/2; halfHeight = h/2; } void display(PImage platform_imag) { image(platform_imag, x, y); } } class Coin { float x, y, w, h; float halfWidth, halfHeight; Coin(float _x, float _y) { x = _x; y = _y; w = 30; h = 30; halfWidth = w/2; halfHeight = h/2; } void display(PImage coin_imag) { image(coin_imag, x, y); } } class Bullet { float x4, y4, w4, h4,beginningx4; float halfWidth, halfHeight; Bullet(float _x, float _y) { x4 = _x; beginningx4 = _x; y4 = _y; w4 = 30; h4 = 30; halfWidth = w4/2; halfHeight = h4/2; } void display(PImage bullet_imag) { if (h.x != startingcoordx) { if( h.x != h.x-1 ){ if (beginningx4 < width/2){ image(bullet_imag, x4++, y4); } else if (beginningx4 > width/2) { image(bullet_imag, x4--, y4); } if (x4 > width) { x4 = beginningx4; } else if ( x4 == 0) { x4 = beginningx4; } } } } } class Hero { float w, h, x, y, vx, vy, accelerationX, accelerationY, speedLimit, friction, bounce, gravity; boolean isOnGround; float jumpForce; float halfWidth, halfHeight; int currentFrame; String collisionSide, CoinCollisionSide, WalkerCollisionSide, BulletCollisionSide; boolean facingRight; int frameSequence; int life; Hero(int _x,int _y) { w = 40; h = 50; x = _x; y = _y; vx = 0; vy = 0; accelerationX = 0; accelerationY = 0; speedLimit = 5; friction = 0.96; bounce = -0.7; gravity = 3; isOnGround = false; jumpForce = -10; life = 3; halfWidth = w/2; halfHeight = h/2; currentFrame = 0; collisionSide = ""; CoinCollisionSide= ""; WalkerCollisionSide= ""; BulletCollisionSide = ""; frameSequence = 6; } void update() { if (left) { // vx =-5; accelerationX = -0.2; friction = 1; facingRight = false; // if(currentFrame <= startLeft){currentFrame=startLeft;} } if (right) { // vx =5; accelerationX = 0.2; friction = 1; facingRight = true; } if (!left&&!right) { // vx=0; accelerationX = 0; friction = 0.96; gravity = 0.3; } else if (left&&right) { // vx=0; accelerationX = 0; friction = 0.96; gravity = 0.3; } if (up && isOnGround) { vy += jumpForce; isOnGround = false; friction = 1; } vx += accelerationX; vy += accelerationY; ////apply the forces of the universe if (isOnGround) { vx *= friction; } vy += gravity; ////correct for maximum speeds if (vx > speedLimit) { vx = speedLimit; } if (vx < -speedLimit) { vx = -speedLimit; } if (vy > speedLimit * 2) { vy = speedLimit * 2; } ////move the player x+=vx; y+=vy; } void checkPlatforms() { ////update for platform collisions if (collisionSide == "bottom" && vy >= 0) { isOnGround = true; ////flip gravity to neutralize gravity's effect vy = -gravity; } else if (collisionSide == "top" && vy <= 0) { vy = 0; } else if (collisionSide == "right" && vx >= 0) { vx = 0; } else if (collisionSide == "left" && vx <= 0) { vx = 0; } if (collisionSide != "bottom" && vy > 0) { isOnGround = false; } } void checkBoundaries() { ////check boundaries ////left if (x < -w) { // vx *= bounce; // x = 0; x = width; } if (x > width) { //// right // vx *= bounce; // x = width - w; x = -w; } ////top if (y < 0) { // vy *= bounce; // y = 0; } if (y + h > height) { y = height - h; isOnGround = true; vy = -gravity; } } void display() { fill(0, 255, 0, 128); if (facingRight) { image(spriteImages[currentFrame+1], x, y+3); } else { image(spriteImages[currentFrame+12], x, y+3); } if (abs(vx)>1 && isOnGround) { // println("currentFrame: "+currentFrame); currentFrame = (currentFrame+1)%frameSequence; } else { // println("currentFrame: "+currentFrame); } } } class Walker { float w2, h2, x2, y2, vx2, vy2, acceleration_X, acceleration_Y, speed_Limit, friction1, bounce0, gravity1, x3, y3; boolean isOnGround_bool; float jumpForced; float half_Width, half_Height; int current_Frame; String collision_Side, Coin_CollisionSide; boolean facing_Right, runoncee = true; int frame_Sequence, h; float endcoord, xmax; Walker(int x, int y) { endcoord = 130; h=0; x2 = x; y2 = y; w2 = 40; h2 = 50; vx2 = 0; vy2 = 0; acceleration_X = 0; acceleration_Y = 0; speed_Limit = 5; friction1 = 0.96; bounce0 = -0.7; gravity1 = 3; isOnGround_bool = false; jumpForced = -10; half_Width = w2/2; half_Height = h2/2; current_Frame = 0; collision_Side = ""; Coin_CollisionSide= ""; frame_Sequence = 1; } void display() { if (h < endcoord) { image(walkerImages[0], x2++, y2); xmax = x2; h++; } else { if (x2+endcoord == xmax) { h=0; } image(walkerImages[1], x2--, y2); } // this thing helps to create moving animation if (x2 != x2-1) { current_Frame = (current_Frame+1)%frame_Sequence; } } } class Shooter { float w3, h3, x3, y3; boolean isOnGround_bool3; float jumpForced3; float half_Width3, half_Height3; int current_Frame3; String collision_Side3, Coin_CollisionSide3; boolean facing_Right3; int frame_Sequence3; float endcoord3, xmax3; Shooter(int x, int y) { endcoord3 = 130; h3=0; x3 = x; y3 = y; w3 = 40; h3 = 50; current_Frame3 = 0; collision_Side3 = ""; Coin_CollisionSide3= ""; frame_Sequence3 = 2; } void display() { fill(0, 255, 0, 128); if (x3 < width/2) { image(shooterImages[0], x3, y3+3); } else { image(shooterImages[1], x3, y3+3); } current_Frame3 = (current_Frame3+1)%frame_Sequence3; } } 
submitted by /u/seanj5140
[link] [comments]

How to make lava rise from the ground slowly and die on collision?

I want to make lava rise from the ground on every level and if you touch it you lose all you levels. Here is the code // Global game variables

PImage backgroundpic1, backgroundpic2,backgroundpic3,platform1,coinphot, coin2, hearth,bulletphot,bulletphot2; Hero h; Shooter s; ArrayList<Coin> c; ArrayList<Walker> walkers; ArrayList<Shooter> shooters; ArrayList<Bullet> bullets; PImage [] spriteImages,walkerImages,shooterImages; int frames, zombieframes,shooterframes,life,startingcoordx,startingcoordy,counter,bottomPane;; float heroX, heroY, heroSpeedVert, gravity; boolean runonce,left, right, up, down, space, shift; Platform [] platforms; int activeLevel = 0,coincounter = 0;

void setup() { size(800, 600); //platform tile images. platform1 = loadImage("data/platform_long.png"); backgroundpic1 = loadImage("data/bg.png"); //background image for level1 backgroundpic2 = loadImage("data/bg2.png"); //background image for level2 backgroundpic3 = loadImage("data/bg3.png"); //background image for level3 coinphot = loadImage("data/coin.png"); hearth = loadImage("data/life.png"); bulletphot = loadImage("data/bullet.png"); bulletphot2 = loadImage("data/bullet2.png"); coinphot.resize(30, 30); hearth.resize(30, 30); bulletphot.resize(20,15); bulletphot2.resize(20,15); startingcoordx = 0; startingcoordy = 575; h = new Hero(startingcoordx,startingcoordy); frames = 24; zombieframes = 2; shooterframes = 2; life = 3;

spriteImages = new PImage[frames]; for (int i = 1; i<frames; i++) { spriteImages[i]=loadImage("data/person"+i+".png"); spriteImages[i].resize(40, 50); } walkerImages = new PImage[zombieframes]; for (int i = 0; i<zombieframes; i++) { walkerImages[i]=loadImage("data/walk"+i+".png"); walkerImages[i].resize(40, 50); } shooterImages = new PImage[shooterframes]; for (int i = 0; i<shooterframes; i++) { shooterImages[i]=loadImage("data/shooter"+i+".png"); shooterImages[i].resize(40, 50); } //boolean things left = false; right = false; up = false; down = false; space = false; shift = false; runonce = true; }

void startMenu() { /-------------------DESIGNING THE MAIN MENU-------------------------/ background(236, 240, 241); textAlign(CENTER); fill(52, 73, 94); textSize(70); text("JUMPER", width/2, height/2); textSize(15); text("Click to start", width/2, height-30); h.x = startingcoordx; h.y = startingcoordy; runonce = true; coincounter = 0; life = 3; /-------------------ENDING OF MAIN MENU---------------------------/ } void YouWon() { /-------------------DESIGNING THE WINNING MENU-------------------------/
PImage star,star2; star = loadImage("data/star.png"); star2 = loadImage("data/star.png"); star.resize(100,100); star2.resize(50,50); background(236, 240, 241); image(star,(width/2)-50, (height/2)-200); image(star2,(width/2)-100, (height/2)-150); image(star2,(width/2)+50, (height/2)-150); textAlign(CENTER); fill(52, 73, 94); textSize(70); text("Congratulations", width/2, height/2); textSize(15); text("Click to start game again", width/2, height-30); h.x = startingcoordx; h.y = startingcoordy; runonce = true; coincounter = 0; life = 3; /-------------------ENDING OF GAME WIN MENU---------------------------/ } void level1ScreenGen() { /* ----------------- DESIGNING THE LEVEL 1----------------------- */ background(backgroundpic1); // how many platforms will be used? platforms = new Platform[18];

//ground floor platforms platforms[0] = new Platform (0, 580, "safe");
platforms[1] = new Platform (120, 580, "coin"); platforms[2] = new Platform (240, 580, "walker"); platforms[3] = new Platform (360, 580, "safe"); platforms[4] = new Platform (480, 580, "walker"); platforms[5] = new Platform (600, 580, "walker");
platforms[6] = new Platform (720, 580, "coin");

// all other platforms to jump on platforms[7] = new Platform (0, 290, "coin"); platforms[8] = new Platform (360, 490, "safe"); platforms[9] = new Platform (750, 250, "safe"); platforms[10] = new Platform (250, 330, "safe"); platforms[11] = new Platform (0, 120, "safe"); platforms[12] = new Platform (350, 90, "coin");
platforms[13] = new Platform (580, 370, "coin"); platforms[14] = new Platform (460, 250, "safe"); platforms[15] = new Platform (700, 100, "walker"); platforms[16] = new Platform (680, 100, "coin"); platforms[17] = new Platform (560, 370, "walker"); /* ------------------------ END OF LEVEL DESIGNING ---------------*/ if (runonce) { c = new ArrayList<Coin>(); walkers = new ArrayList<Walker>(); for (int i = 0; i < platforms.length; ++i) { if (platforms[i].typeof == "coin") { c.add(new Coin(platforms[i].x+50, platforms[i].y-30)); } else if (platforms[i].typeof == "walker") { walkers.add(new Walker(int(platforms[i].x), int(platforms[i].y-50))); } } runonce = false; } }

void level2ScreenGen() { /* ----------------- DESIGNING THE LEVEL 2----------------------- */ background(backgroundpic2); // how many platforms will be used? platforms = new Platform[21]; platforms[0] = new Platform (0, 580, "safe"); platforms[1] = new Platform (120, 580, "safe"); platforms[2] = new Platform (40, 580, "safe"); platforms[3] = new Platform (360, 580, "safe"); platforms[4] = new Platform (480, 580, "safe"); platforms[5] = new Platform (600, 580, "safe"); platforms[6] = new Platform (700, 580, "safe"); platforms[7] = new Platform (240, 580, "safe");

platforms[8] = new Platform (600, 430, "coin"); platforms[9] = new Platform (690, 430, "shooter"); platforms[10] = new Platform (0, 390, "shooter"); platforms[11] = new Platform (80, 180, "safe"); platforms[12] = new Platform (0, 50, "shooter"); platforms[13] = new Platform (690, 250, "shooter"); platforms[14] = new Platform (460, 250, "safe"); platforms[15] = new Platform (337, 369, "coin"); platforms[16] = new Platform (230, 50, "coin"); platforms[17] = new Platform (220, 487, "safe"); platforms[18] = new Platform (700, 106, "coin"); platforms[19] = new Platform (590, 312, "coin"); platforms[20] = new Platform (474, 76, "safe"); /* ------------------------ END OF LEVEL 2 DESIGNING ---------------*/

if (runonce) { c = new ArrayList<Coin>(); shooters = new ArrayList<Shooter>(); bullets = new ArrayList<Bullet>(); for (int i = 0; i < platforms.length; ++i) { if (platforms[i].typeof == "coin") { c.add(new Coin(platforms[i].x+50, platforms[i].y-30)); } else if (platforms[i].typeof == "shooter") { shooters.add(new Shooter(int(platforms[i].x), int(platforms[i].y-50))); bullets.add(new Bullet(int(platforms[i].x), int(platforms[i].y-30))); } } runonce = false; } } void level3ScreenGen() { /* ----------------- DESIGNING THE LEVEL 3----------------------- */ background(backgroundpic3); // how many platforms will be used? platforms = new Platform[19]; //ground floor platforms platforms[0] = new Platform (0, 580, "safe"); platforms[1] = new Platform (120, 580, "safe"); platforms[2] = new Platform (240, 580, "safe"); platforms[3] = new Platform (360, 580, "safe"); platforms[4] = new Platform (480, 580, "safe"); platforms[5] = new Platform (600, 580, "safe"); platforms[6] = new Platform (720, 580, "safe");

// all other platforms to jump on platforms[7] = new Platform (0, 400, "coin"); platforms[8] = new Platform (331, 525, "safe"); platforms[9] = new Platform (462, 431, "safe"); platforms[10] = new Platform (318, 321, "coin"); platforms[11] = new Platform (205, 207, "walker"); platforms[12] = new Platform (0, 50, "coin"); platforms[13] = new Platform (740, 430, "shooter"); platforms[14] = new Platform (741, 289, "shooter"); platforms[15] = new Platform (699, 116, "coin"); platforms[16] = new Platform (423, 208, "walker"); platforms[17] = new Platform (324, 70, "coin"); platforms[18] = new Platform (385, 70, "walker"); /* ------------------------ END OF LEVEL 3 DESIGNING ---------------*/

if (runonce) { c = new ArrayList<Coin>(); shooters = new ArrayList<Shooter>(); bullets = new ArrayList<Bullet>(); walkers = new ArrayList<Walker>(); for (int i = 0; i < platforms.length; ++i) { if (platforms[i].typeof == "coin") { c.add(new Coin(platforms[i].x+50, platforms[i].y-30)); } if (platforms[i].typeof == "shooter") { shooters.add(new Shooter(int(platforms[i].x), int(platforms[i].y-50))); bullets.add(new Bullet(int(platforms[i].x), int(platforms[i].y-30))); } if (platforms[i].typeof == "walker") { walkers.add(new Walker(int(platforms[i].x), int(platforms[i].y-50))); } } runonce = false;
} } void CoinCounter() { image(coinphot, 700, 5); fill(52, 73, 94); textSize(20); text(coincounter, 770, 25); if (coincounter == 5) { activeLevel+=1; h.x = startingcoordx; h.y = startingcoordy; runonce = true; coincounter = 0; System.gc(); } } void LifeBar() { int distance = 0; int xcord = 600; ; for (int i=0; i< life; i++) { image(hearth, xcord+distance, 5); distance+=30; } if (life == 0) { activeLevel=0; coincounter = 0; life=3; } } void commonThings() { h.update(); for (int i = 0; i < platforms.length; ++i) { rectangleCollisions(h, platforms[i]); h.checkPlatforms(); } h.checkBoundaries(); for (int i = 0; i < platforms.length; ++i) { platforms[i].display(platform1); } for (int k = c.size()-1; k >= 0; k--) { CoinCollisions(h, c.get(k)); if (h.CoinCollisionSide != "none") { c.remove(k); coincounter++; } } for (int k = c.size()-1; k >= 0; k--) { c.get(k).display(coinphot); } if (activeLevel == 1) { for (int k= walkers.size()-1; k>=0; k--) { WalkerCollision(h, walkers.get(k)); walkers.get(k).display(); if (h.WalkerCollisionSide != "none") { h.x = startingcoordx; h.y = startingcoordy; life--;
} } } else if (activeLevel == 2) { for (int k = shooters.size()-1; k >= 0; k--) { bullets.get(k).display(bulletphot); BulletCollisions(h,bullets.get(k)); shooters.get(k).display(); if (h.BulletCollisionSide != "none") {
h.x = startingcoordx; h.y = startingcoordy; life--; } } } else if (activeLevel == 3) { for (int k= walkers.size()-1; k>=0; k--) { WalkerCollision(h, walkers.get(k)); walkers.get(k).display(); if (h.WalkerCollisionSide != "none") { h.x = startingcoordx; h.y = startingcoordy; life--;
} } for (int k = shooters.size()-1; k >= 0; k--) { bullets.get(k).display(bulletphot); BulletCollisions(h,bullets.get(k)); shooters.get(k).display(); if (h.BulletCollisionSide != "none") {
h.x = startingcoordx; h.y = startingcoordy; life--; } } } h.display(); } void draw() { if (activeLevel == 0) { startMenu(); } else if (activeLevel == 1) { level1ScreenGen(); commonThings(); CoinCounter(); LifeBar(); } else if (activeLevel == 2) { level2ScreenGen(); commonThings(); CoinCounter(); LifeBar(); } else if (activeLevel == 3) { level3ScreenGen(); commonThings(); CoinCounter(); LifeBar(); } else if (activeLevel > 3) { YouWon(); } } // If user clicked mouse on main menu we sending him to first level. void mousePressed() { if (activeLevel == 0) { activeLevel=1; } else if (activeLevel > 3) { activeLevel=1; } } // catching the keyboard direction buttons. void keyPressed() { switch (keyCode) { case 37://left left = true; break; case 39://right right = true; break; case 38://up up = true; break; case 40://down down = true; break; case 32: //space space = true; break; case 16: //shift shift = true; } } void keyReleased() { switch (keyCode) { case 37://left left = false; break; case 39://right right = false; break; case 38://up up = false; break; case 40://down down = false; break; case 32: //space space = false; break; case 16: //shift shift = false; } } // checking collision between our Hero and platform tiles. void rectangleCollisions(Hero r1, Platform r2) { ////r1 is the hero ////r2 is the platform tile float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2); float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2);

float combinedHalfWidths = r1.halfWidth + r2.halfWidth; float combinedHalfHeights = r1.halfHeight + r2.halfHeight;

if (abs(dx) < combinedHalfWidths) { ////a collision may be happening ////now check on the y axis if (abs(dy) < combinedHalfHeights) { ////excellent. they are overlapping //determine the overlap on each axis float overlapX = combinedHalfWidths - abs(dx); float overlapY = combinedHalfHeights - abs(dy); ////the collision is on the axis with the ////SMALLEST about of overlap if (overlapX >= overlapY) { if (dy > 0) { r1.collisionSide = "top"; ////move the rectangle back to eliminate overlap ////before calling its display to prevent ////drawing object inside each other r1.y += overlapY; // println("collisionSide: "+r1.collisionSide); } else { r1.collisionSide = "bottom"; r1.y -= overlapY; // println("collisionSide: "+r1.collisionSide); } } else { if (dx > 0) { r1.collisionSide = "left"; r1.x += overlapX; // println("collisionSide: "+r1.collisionSide); } else { r1.collisionSide = "right"; r1.x -= overlapX; // println("collisionSide: "+r1.collisionSide); } } } else { r1.collisionSide = "none"; } } else { r1.collisionSide = "none"; } } void CoinCollisions(Hero r1, Coin r2) { ////r1 is the player ////r2 is the platform rectangle float dx = (r1.x+r1.w/2) - (r2.x+r2.w/2); float dy = (r1.y+r1.h/2) - (r2.y+r2.h/2);

float combinedHalfWidths = r1.halfWidth + r2.halfWidth; float combinedHalfHeights = r1.halfHeight + r2.halfHeight;

if (abs(dx) < combinedHalfWidths) { ////a collision may be happening ////now check on the y axis if (abs(dy) < combinedHalfHeights) { ////excellent. they are overlapping //determine the overlap on each axis float overlapX = combinedHalfWidths - abs(dx); float overlapY = combinedHalfHeights - abs(dy); ////the collision is on the axis with the ////SMALLEST about of overlap if (overlapX >= overlapY) { if (dy > 0) { r1.CoinCollisionSide = "top"; ////move the rectangle back to eliminate overlap ////before calling its display to prevent ////drawing object inside each other // r1.y += overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.CoinCollisionSide = "bottom"; // r1.y -= overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } else { if (dx > 0) { r1.CoinCollisionSide = "left"; // r1.x += overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.CoinCollisionSide = "right"; // r1.x -= overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } } else { r1.CoinCollisionSide = "none"; } } else { r1.CoinCollisionSide = "none"; } // return CoinCollisionSide; } void BulletCollisions(Hero r1, Bullet r2) { ////r1 is the player ////r2 is the platform rectangle float dx = (r1.x+r1.w/2) - (r2.x4+r2.w4/2); float dy = (r1.y+r1.h/2) - (r2.y4+r2.h4/2);

float combinedHalfWidths = r1.halfWidth + r2.halfWidth; float combinedHalfHeights = r1.halfHeight + r2.halfHeight;

if (abs(dx) < combinedHalfWidths) { ////a collision may be happening ////now check on the y axis if (abs(dy) < combinedHalfHeights) { ////excellent. they are overlapping //determine the overlap on each axis float overlapX = combinedHalfWidths - abs(dx); float overlapY = combinedHalfHeights - abs(dy); ////the collision is on the axis with the ////SMALLEST about of overlap if (overlapX >= overlapY) { if (dy > 0) { r1.BulletCollisionSide = "top"; ////move the rectangle back to eliminate overlap ////before calling its display to prevent ////drawing object inside each other // r1.y += overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.BulletCollisionSide = "bottom"; // r1.y -= overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } else { if (dx > 0) { r1.BulletCollisionSide = "left"; // r1.x += overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.BulletCollisionSide = "right"; // r1.x -= overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } } else { r1.BulletCollisionSide = "none"; } } else { r1.BulletCollisionSide = "none"; } // return CoinCollisionSide; } void WalkerCollision(Hero r1, Walker r2) { ////r1 is the player ////r2 is the platform rectangle float dx = (r1.x+r1.w/2) - (r2.x2+r2.w2/2); float dy = (r1.y+r1.h/2) - (r2.y2+r2.h2/2);

float combinedHalfWidths = r1.halfWidth + r2.half_Width; float combinedHalfHeights = r1.halfHeight + r2.half_Height;

if (abs(dx) < combinedHalfWidths) { ////a collision may be happening ////now check on the y axis if (abs(dy) < combinedHalfHeights) { ////excellent. they are overlapping //determine the overlap on each axis float overlapX = combinedHalfWidths - abs(dx); float overlapY = combinedHalfHeights - abs(dy); ////the collision is on the axis with the ////SMALLEST about of overlap if (overlapX >= overlapY) { if (dy > 0) { r1.WalkerCollisionSide = "top"; ////move the rectangle back to eliminate overlap ////before calling its display to prevent ////drawing object inside each other // r1.y += overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.WalkerCollisionSide = "bottom"; // r1.y -= overlapY; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } else { if (dx > 0) { r1.WalkerCollisionSide = "left"; // r1.x += overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } else { r1.WalkerCollisionSide = "right"; // r1.x -= overlapX; // println("CoinCollisionSide: "+r1.CoinCollisionSide); } } } else { r1.WalkerCollisionSide = "none"; } } else { r1.WalkerCollisionSide = "none"; } // return CoinCollisionSide; } class Platform { float x, y, w, h; String typeof; float halfWidth, halfHeight;

Platform(float _x, float _y, String _typeof ) { x = _x; y = _y; w = 130; h = 33; typeof = _typeof; halfWidth = w/2; halfHeight = h/2; } void display(PImage platform_imag) { image(platform_imag, x, y); } } class Coin { float x, y, w, h; float halfWidth, halfHeight;

Coin(float _x, float _y) { x = _x; y = _y; w = 30; h = 30; halfWidth = w/2; halfHeight = h/2; } void display(PImage coin_imag) { image(coin_imag, x, y); } }

class Bullet { float x4, y4, w4, h4,beginningx4; float halfWidth, halfHeight;

Bullet(float _x, float _y) { x4 = _x; beginningx4 = _x; y4 = _y; w4 = 30; h4 = 30; halfWidth = w4/2; halfHeight = h4/2; } void display(PImage bullet_imag) { if (h.x != startingcoordx) { if( h.x != h.x-1 ){ if (beginningx4 < width/2){ image(bullet_imag, x4++, y4); } else if (beginningx4 > width/2) { image(bullet_imag, x4--, y4); } if (x4 > width) { x4 = beginningx4; } else if ( x4 == 0) { x4 = beginningx4; } } } } }

class Hero { float w, h, x, y, vx, vy, accelerationX, accelerationY, speedLimit, friction, bounce, gravity; boolean isOnGround; float jumpForce; float halfWidth, halfHeight; int currentFrame; String collisionSide, CoinCollisionSide, WalkerCollisionSide, BulletCollisionSide; boolean facingRight; int frameSequence; int life; Hero(int _x,int _y) { w = 40; h = 50; x = _x; y = _y; vx = 0; vy = 0; accelerationX = 0; accelerationY = 0; speedLimit = 5; friction = 0.96; bounce = -0.7; gravity = 3; isOnGround = false; jumpForce = -10; life = 3;

halfWidth = w/2; halfHeight = h/2; currentFrame = 0; collisionSide = ""; CoinCollisionSide= ""; WalkerCollisionSide= ""; BulletCollisionSide = ""; frameSequence = 6; 

} void update() { if (left) { // vx =-5; accelerationX = -0.2; friction = 1; facingRight = false; // if(currentFrame <= startLeft){currentFrame=startLeft;} } if (right) { // vx =5; accelerationX = 0.2; friction = 1; facingRight = true; } if (!left&&!right) { // vx=0; accelerationX = 0; friction = 0.96; gravity = 0.3; } else if (left&&right) { // vx=0; accelerationX = 0; friction = 0.96; gravity = 0.3; } if (up && isOnGround) { vy += jumpForce; isOnGround = false; friction = 1; }

vx += accelerationX; vy += accelerationY; ////apply the forces of the universe if (isOnGround) { vx *= friction; } vy += gravity; ////correct for maximum speeds if (vx > speedLimit) { vx = speedLimit; } if (vx < -speedLimit) { vx = -speedLimit; } if (vy > speedLimit * 2) { vy = speedLimit * 2; } ////move the player x+=vx; y+=vy; 

} void checkPlatforms() { ////update for platform collisions if (collisionSide == "bottom" && vy >= 0) { isOnGround = true; ////flip gravity to neutralize gravity's effect vy = -gravity; } else if (collisionSide == "top" && vy <= 0) { vy = 0; } else if (collisionSide == "right" && vx >= 0) { vx = 0; } else if (collisionSide == "left" && vx <= 0) { vx = 0; } if (collisionSide != "bottom" && vy > 0) { isOnGround = false; } } void checkBoundaries() { ////check boundaries ////left if (x < -w) { // vx *= bounce; // x = 0; x = width; } if (x > width) { //// right // vx *= bounce; // x = width - w; x = -w; } ////top if (y < 0) { // vy *= bounce; // y = 0; } if (y + h > height) { y = height - h; isOnGround = true; vy = -gravity; } } void display() { fill(0, 255, 0, 128); if (facingRight) { image(spriteImages[currentFrame+1], x, y+3); } else { image(spriteImages[currentFrame+12], x, y+3); } if (abs(vx)>1 && isOnGround) { // println("currentFrame: "+currentFrame); currentFrame = (currentFrame+1)%frameSequence; } else { // println("currentFrame: "+currentFrame); } } } class Walker { float w2, h2, x2, y2, vx2, vy2, acceleration_X, acceleration_Y, speed_Limit, friction1, bounce0, gravity1, x3, y3; boolean isOnGround_bool; float jumpForced; float half_Width, half_Height; int current_Frame; String collision_Side, Coin_CollisionSide; boolean facing_Right, runoncee = true; int frame_Sequence, h; float endcoord, xmax; Walker(int x, int y) { endcoord = 130; h=0; x2 = x; y2 = y; w2 = 40; h2 = 50; vx2 = 0; vy2 = 0; acceleration_X = 0; acceleration_Y = 0; speed_Limit = 5; friction1 = 0.96; bounce0 = -0.7; gravity1 = 3; isOnGround_bool = false; jumpForced = -10; half_Width = w2/2; half_Height = h2/2; current_Frame = 0; collision_Side = ""; Coin_CollisionSide= ""; frame_Sequence = 1; } void display() { if (h < endcoord) { image(walkerImages[0], x2++, y2); xmax = x2; h++; } else { if (x2+endcoord == xmax) { h=0; } image(walkerImages[1], x2--, y2); } // this thing helps to create moving animation if (x2 != x2-1) { current_Frame = (current_Frame+1)%frame_Sequence; } } } class Shooter { float w3, h3, x3, y3; boolean isOnGround_bool3; float jumpForced3; float half_Width3, half_Height3; int current_Frame3; String collision_Side3, Coin_CollisionSide3; boolean facing_Right3; int frame_Sequence3; float endcoord3, xmax3; Shooter(int x, int y) { endcoord3 = 130; h3=0; x3 = x; y3 = y; w3 = 40; h3 = 50; current_Frame3 = 0; collision_Side3 = ""; Coin_CollisionSide3= ""; frame_Sequence3 = 2; } void display() { fill(0, 255, 0, 128); if (x3 < width/2) { image(shooterImages[0], x3, y3+3); } else { image(shooterImages[1], x3, y3+3); } current_Frame3 = (current_Frame3+1)%frame_Sequence3; } }

submitted by /u/seanj5140
[link] [comments]

How do i change color of a rectangle after releasing the mouse click and do the same thing when i redraw another one?

So when I draw a rectangle its initially blue then when i release the mouse button it turns red. What I want to happen is when i draw another rectangle it should do the same thing. I dont know how to do it

int x, y, x2, y2 = 0; color c = color(0, 0, 255); void setup() { size(300, 300); } void draw() { background(255); fill(c); rect(x, y, x2, y2); } void mousePressed() { x = mouseX; y = mouseY; } void mouseDragged() { x2 = mouseX - x; y2 = mouseY - y; rect(x, y, x2, y2); } void mouseReleased() { c = color(255, 0, 0); } 
submitted by /u/Kdot_Cdot
[link] [comments]

Controlling Servos With Virtual Processing Buttons

Hi guys,

So I've got my code here...

// Processing application that demonstrates the Button class by creating a button // Draws a square in the window when the mouse cursor is over the button // Writes to the Processing IDE console pane when the button is clicked // 3 July 2015 http://startingelectronics.org import processing.serial.*; int xpos=90; // set x servo's value to mid point (0-180); int ypos=90; // and the same here Serial serv_port; // The serial port we will be using Button serv1; Button serv2; Button serv3; Button serv4; Button serv5; // the button int clk1 = 1; // number of times the button is clicked int clk2 = 1; int clk3 = 1; int clk4 = 1; int clk5 = 1; void setup() { size (800, 600); smooth(); println((Object[])Serial.list()); // List COM-ports //select second com-port from the list (COM3 for my device) // You will want to change the [1] to select the correct device // Remember the list starts at [0] for the first option. serv_port = new Serial(this, Serial.list()[0], 9600); // create the button object serv1 = new Button("Servo Position 1", 50, 50, 100, 50); serv2 = new Button("Servo Position 2", 200, 50, 100, 50); serv3 = new Button("Servo Position 3", 350, 50, 100, 50); serv4 = new Button("Servo Position 4", 500, 50, 100, 50); serv5 = new Button("Servo Position 5", 650, 50, 100, 50); } void draw() { // draw a square if the mouse curser is over the button if (serv1.MouseIsOver()) { rect(700, 500, 50, 50); } else { // hide the square if the mouse cursor is not over the button background(0); } // draw a square if the mouse curser is over the button if (serv2.MouseIsOver()) { rect(700, 500, 50, 50); } else { // hide the square if the mouse cursor is not over the button background(0); } // draw a square if the mouse curser is over the button if (serv3.MouseIsOver()) { rect(700, 500, 50, 50); } else { // hide the square if the mouse cursor is not over the button background(0); } // draw a square if the mouse curser is over the button if (serv4.MouseIsOver()) { rect(700, 500, 50, 50); } else { // hide the square if the mouse cursor is not over the button background(0); } // draw a square if the mouse curser is over the button if (serv5.MouseIsOver()) { rect(700, 500, 50, 50); } else { // hide the square if the mouse cursor is not over the button background(0); } // draw the button in the window serv1.Draw(); serv2.Draw(); serv3.Draw(); serv4.Draw(); serv5.Draw(); } // mouse button clicked void mousePressed() { if (serv1.MouseIsOver()) { // print some text to the console pane if the button is clicked print("Skittles in ServPos1: "); println(clk1++); serv_port.write(xpos = 30); } if (serv2.MouseIsOver()) { // print some text to the console pane if the button is clicked print("Skittles in ServPos2: "); println(clk2++); } if (serv3.MouseIsOver()) { // print some text to the console pane if the button is clicked print("Skittles in ServPos3: "); println(clk3++); } if (serv4.MouseIsOver()) { // print some text to the console pane if the button is clicked print("Skittles in ServPos4: "); println(clk4++); } if (serv5.MouseIsOver()) { // print some text to the console pane if the button is clicked print("Skittles in ServPos5: "); println(clk5++); serv_port.write(30); } } // the Button class class Button { String label; // button label float x; // top left corner x position float y; // top left corner y position float w; // width of button float h; // height of button // constructor Button(String labelB, float xpos, float ypos, float widthB, float heightB) { label = labelB; x = xpos; y = ypos; w = widthB; h = heightB; } void Draw() { fill(218); stroke(141); rect(x, y, w, h, 10); textAlign(CENTER, CENTER); fill(0); text(label, x + (w / 2), y + (h / 2)); } boolean MouseIsOver() { if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) { return true; } return false; } } 

It's a little patchy, taking bits and pieces from different areas, but my current goal is to get each of the buttons I have to move a servo into a desired position when pressed. So button 1 will move the servo to position 30, button 2 to position 60 etc.

Thanks in advance.

submitted by /u/ZupaTr00pa
[link] [comments]