Question
Question: With Processing, How do I move the robot to the left when the robot reaches the lower edge? Then, how do I move it
Question: With Processing, How do I move the robot to the left when the robot reaches the lower edge? Then, how do I move it upward when the robot reaches the left end?
PImage bgImg; // name of the background PImage robotImg; // name of the robot int robotX; // x position of the robot int robotY; // y position of the robot int velX; // x direction velocity of the robot int velY; // y direction velocity of the robot int speed; int direction; // speed of the robot
void setup() { size(750, 650); // set the window size bgImg = loadImage("space.png"); // background robotImg = loadImage("robot.png"); // robot
robotX = 0; // initial position of the robot robotY = 0; speed = 10; velX = speed; velY = speed; direction = 1; }
void draw() { image(bgImg, 0, 0); // display the background image(robotImg, robotX, robotY); // display the robot if(direction == 1) { velX = speed; velY = 0; if(robotX == width-300)//reach the right end { direction = 2; } }else if(direction == 2) { velX = 0; velY = speed; if(robotY == height-300)//reach the lower edge { direction = 3; } } robotX = robotX + velX; // make the robot move horizontally robotY = robotY + velY; // make the robot move vertically }
Step 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