Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rick van Lieshout (en I1E-n) Donderdag 11 September.
- // Moduleopdrachten 3
- int robotX, robotY = 0;
- int robotSize = 100;
- int circleSize = robotSize;
- int circleX = 3*robotSize;
- int circleY = 3*robotSize;
- boolean attached = false;
- void setup() {
- int size = 10*robotSize;
- size(size, size);
- }
- void draw() {
- background(#D600B3);
- drawGrid(); //draw an optional grid
- //use robot size for circle too, since they wil always be the same
- robotX = constrain(robotX, 0, width-robotSize);
- robotY = constrain(robotY, 0, height-robotSize);
- circleX = constrain(circleX, 0, width-circleSize);
- circleY = constrain(circleY, 0, width-circleSize);
- fill(#F7C50C);
- rect(robotX, robotY, robotSize, robotSize);
- ellipseMode(CORNER);
- fill(#DBDBDB);
- ellipse(circleX, circleY, circleSize, circleSize);
- }
- void keyPressed() {
- if (keyCode == ENTER || keyCode == RETURN) {
- if (robotX == circleX && robotY == circleY) {
- if (attached) {
- attached = false;
- } else {
- attached = true;
- }
- }
- }
- if (key == CODED) {
- switch(keyCode) {
- case UP:
- if (keyCode == UP) {
- robotY-=robotSize;
- if (attached) {
- circleY-=robotSize;
- }
- }
- break;
- case DOWN:
- if (keyCode == DOWN) {
- robotY+=robotSize;
- if (attached) {
- circleY+=robotSize;
- }
- }
- break;
- case LEFT:
- if (keyCode == LEFT) {
- robotX-=robotSize;
- if (attached) {
- circleX-=circleSize;
- }
- }
- break;
- case RIGHT:
- if (keyCode == RIGHT) {
- robotX+=robotSize;
- if (attached) {
- circleX+=circleSize;
- }
- }
- break;
- }
- }
- }
- void drawGrid() {
- for (int i = 0; i<= width; i+=robotSize) {
- stroke(126);
- line(i, 0, i, height);
- line(0, i, width, i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment