Mastermindzh

Sebastiaan_orsel_moduleopdracht3

Sep 11th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. // Rick van Lieshout (en I1E-n) Donderdag 11 September.
  2. // Moduleopdrachten 3
  3.  
  4.  
  5. int robotX, robotY = 0;
  6. int robotSize = 100;
  7. int circleSize = robotSize;
  8. int circleX = 3*robotSize;
  9. int circleY = 3*robotSize;
  10. boolean attached = false;
  11.  
  12. void setup() {
  13.   int size = 10*robotSize;
  14.   size(size, size);
  15. }
  16.  
  17. void draw() {
  18.   background(#D600B3);
  19.   drawGrid(); //draw an optional grid
  20.   //use robot size for circle too, since they wil always be the same
  21.   robotX = constrain(robotX, 0, width-robotSize);
  22.   robotY = constrain(robotY, 0, height-robotSize);
  23.   circleX = constrain(circleX, 0, width-circleSize);
  24.   circleY = constrain(circleY, 0, width-circleSize);
  25.  
  26.   fill(#F7C50C);
  27.   rect(robotX, robotY, robotSize, robotSize);
  28.   ellipseMode(CORNER);
  29.   fill(#DBDBDB);
  30.   ellipse(circleX, circleY, circleSize, circleSize);
  31. }
  32.  
  33. void keyPressed() {
  34.   if (keyCode == ENTER || keyCode == RETURN) {
  35.     if (robotX == circleX && robotY == circleY) {
  36.       if (attached) {
  37.         attached = false;
  38.       } else {
  39.         attached = true;
  40.       }
  41.     }
  42.   }
  43.  
  44.   if (key == CODED) {
  45.     switch(keyCode) {
  46.     case UP:
  47.       if (keyCode == UP) {
  48.         robotY-=robotSize;
  49.         if (attached) {
  50.           circleY-=robotSize;
  51.         }
  52.       }
  53.       break;
  54.     case DOWN:
  55.       if (keyCode == DOWN) {
  56.         robotY+=robotSize;
  57.         if (attached) {
  58.           circleY+=robotSize;
  59.         }
  60.       }
  61.       break;
  62.     case LEFT:
  63.       if (keyCode == LEFT) {
  64.         robotX-=robotSize;
  65.         if (attached) {
  66.           circleX-=circleSize;
  67.         }
  68.       }
  69.       break;
  70.     case RIGHT:
  71.       if (keyCode == RIGHT) {
  72.         robotX+=robotSize;
  73.         if (attached) {
  74.           circleX+=circleSize;
  75.         }
  76.       }
  77.       break;
  78.     }
  79.   }
  80. }
  81.  
  82. void drawGrid() {
  83.   for (int i = 0; i<= width; i+=robotSize) {
  84.     stroke(126);
  85.     line(i, 0, i, height);
  86.     line(0, i, width, i);
  87.   }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment