Question
How can I fix this code? Processing 3.5.3 // ==================================================== // Processing Lab 9 example 2A - bounce circle off border // ==================================================== // global
How can I fix this code? Processing 3.5.3
// ==================================================== // Processing Lab 9 example 2A - bounce circle off border // ==================================================== // global variables for circle attributes int xFactor, yFactor; // -------------------------------------------- // x,y coordinate of center and radius int circleX, circleY, circleRadius; // amount to change the x and y coordinates int speedX, speedY; // global variables for circle colors color circleFillColor; // fill color color circleBorderColor; // border color // ============================================ // other global variables // -------------------------------------------- // screen background color color bgColor; // wall color color wallColor; // to keep track of where walls were boolean rightWallNow, leftWallNow, topWallNow, bottomWallNow; boolean rightWallNext, leftWallNext, topWallNext, bottomWallNext;
// ============================================ // setup method to initialize global variables // ============================================ void setup() { size(450,450); speedX=1; speedY=1; xFactor=1; yFactor=1; // use white for background color bgColor=color(255,255,255); // initialize the minimum and maximum values // used to get random values for the global variables int minRadius=10; int maxRadius=30; // value of coordinates of circle // should account for radius // to make sure it is all within the screen int minX=maxRadius+ 2; int minY=maxRadius+2; int maxX=width-maxRadius-2; int maxY=height -maxRadius-2; // minimum and maximum values for circle fill colors // to prevent being the same as the background (255,255,255) // or being the same as the rectangle border (0,0,0)
int minCV=1; int maxCV=254; //initialize the global variables //by using random methods circleX=(int) random(minX, maxX); circleY=(int) random(minX, maxX); circleRadius=(int) random(minRadius, maxRadius); int redV= (int) random(minCV,maxCV); int greenV= (int) random(minCV,maxCV); int blueV= (int) random(minCV,maxCV); circleFillColor=color(redV,greenV,blueV); circleBorderColor=circleFillColor; // ================================== // display the circle and walls and // get location of walls relative to circle background(bgColor); drawWalls(); drawCircle(); getWalls(); } // end of setup method
// method to use global variables // to display rectangle for walls void drawWalls() { stroke(wallColor); strokeWeight(10); fill(bgColor); rect(25,25,width-50,height-50); noStroke(); noFill(); } // end of drawWalls method
// method to use global variables // to display circle void drawCircle() { stroke(circleBorderColor); fill(circleFillColor); ellipse(circleX, circleY, circleRadius*2, circleRadius*2); noFill(); noStroke(); } // end of drawCircle method
// method to determine if walls are next to circle (1 pixel away) // or will be after circle is moved after adding speedX to circleX // and speedY to circleY void getWalls() { // determine if walls exist to top, bottom, left, right of circle color cTopColor, cBottomColor, cLeftColor, cRightColor; cTopColor = get(circleX, circleY-circleRadius+1); // top cBottomColor = get(circleX, circleY+circleRadius+1); // bottom cLeftColor = get(circleX-circleRadius-1, circleY); // left cRightColor = get(circleX+circleRadius+1, circleY); // right if(cTopColor != bgColor) { topWallNow = true; } else { topWallNow = false; } if(cBottomColor != bgColor) { bottomWallNow = true; } else { bottomWallNow = false; } if(cLeftColor != bgColor) { leftWallNow = true; } else { leftWallNow = false; } if(cRightColor != bgColor) { rightWallNow = true; } else { rightWallNow = false; } // determine if walls exist to top, bottom, left, right of circle after move color cNextTopColor, cNextBottomColor, cNextLeftColor, cNextRightColor; cNextTopColor = get(circleX+speedX, circleY+speedY-circleRadius-1); // top cNextBottomColor = get(circleX+speedX, circleY+speedY+circleRadius+1); // bottom cNextLeftColor = get(circleX+speedX-circleRadius-1, circleY+speedY); // left cNextRightColor = get(circleX+speedX+circleRadius+1, circleY+speedY); // right if(cNextTopColor != bgColor) { topWallNext = true; } else { topWallNext = false; } if(cNextBottomColor != bgColor) { bottomWallNext = true; } else { bottomWallNext = false; } if(cNextLeftColor != bgColor) { leftWallNext = true; } else { leftWallNext = false; } if(cNextRightColor != bgColor) { rightWallNext = true; } else { rightWallNext = false; } } // end of getWalls method
// main draw method void draw() { background(bgColor); drawWalls(); getWalls(); if(onWall()) { println("On wall, circle at(" + circleX + "," + circleY + ")"); } moveCircle(); // update circleX and circleY values drawCircle(); // display the circle } // end of draw method
// method to determine if circle is on wall, and // change direction of circle by modifying the values // of speedX and speedY global variables // Returns true if circle touching or about to touch wall boolean onWall() { // next move down is new wall if(bottomWallNow || bottomWallNext && speedY > 0){ // reverse y direction speedY = -1; return true; } // next move right is new wall if(rightWallNow || rightWallNext && speedX > 0){ // reverse x direction speedX = -1; return true; } // next move left is new wall if(leftWallNow || leftWallNext && speedX
// method to update the center of the // circle (circleX, circleY) based // on any modifications to speedX and speedY // made in the onWall method void moveCircle(){ circleX=circleX+ (speedX* xFactor); circleY=circleY+ (speedY* yFactor); } // end of moveCircle method void keyPressed() { if(key == CODED) { if (keyCode== UP) { yFactor= yFactor-1; } else if (keyCode== DOWN) { yFactor= yFactor+ 1; } else if (keyCode== LEFT) { xFactor= xFactor-1; } else if(keyCode== RIGHT) { xFactor= xFactor+ 1; } } else { xFactor=1; yFactor=1; } }//end of keyPressed method
coding language: java
Image Processing Pixels If done properly, then everytime you run the program a randomly sized and colored circle should start at a random spot, going in a random direction at a random speed; and correctly bounce off a randomly colored wall. The arrow keys should have the following behaviors: Pressing the up arrow key should subtract 1 from a factor that is used to multiply the speed going up. Pressing the down arrow key should add 1 to a factor that is used to multiply the speed going down. Pressing the left arrow key should subtract 1 from a factor that is used to multiply the speed going left. Pressing the right arrow key should add 1 to a factor that is used to multiply the speed going right. Lesson Objective: students will analyze problems, use structured programming techniques to implement algorithms. Image Processing Pixels If done properly, then everytime you run the program a randomly sized and colored circle should start at a random spot, going in a random direction at a random speed; and correctly bounce off a randomly colored wall. The arrow keys should have the following behaviors: Pressing the up arrow key should subtract 1 from a factor that is used to multiply the speed going up. Pressing the down arrow key should add 1 to a factor that is used to multiply the speed going down. Pressing the left arrow key should subtract 1 from a factor that is used to multiply the speed going left. Pressing the right arrow key should add 1 to a factor that is used to multiply the speed going right. Lesson Objective: students will analyze problems, use structured programming techniques to implement algorithmsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started