seperating different parts of code
in
Programming Questions
•
11 months ago
hi there, in my code i have ellipses that travel accross the page, then when mouse is clicked rotate around my mouse point. however i also have a image that follows my mouse and i want that to stay still when i click. however i cannot find the right way to format my code so this does that, any ideas??
- PImage bg;
- PImage mi;
- float tx = 0;
- float ty = 0;
- static final byte dotCount = 15;
- static final byte speed = 11;
- static final byte size = 20;
- //
- static float angle = 0;
- //
- static final int[] xn = new int [dotCount];
- static final int[] yn = new int [dotCount];
- static final byte[] sn = new byte[dotCount];
- //
- void setup() {
- size(1000, 500);
- frameRate(60);
- smooth();
- noStroke();
- bg = loadImage("space.png");
- mi = loadImage("mouse.png");
- background(bg);
- //
- //loop that continusly puts random numbers into the 'boxes' in the arays. saves
- //time then putting them in one at a time.
- for (byte i=0; i<dotCount; i++) {
- xn[i] = (int) random (width);
- yn[i] = (int) random (height);
- sn[i] = (byte)random (1, speed);
- }
- }
- //
- void draw() {
- background (bg);
- //makes ellipses rotate around mouseX,mouseY
- if (mousePressed) {
- translate(mouseX, mouseY);
- rotate( angle += PI/72 );
- translate(-mouseX, -mouseY);
- }
- //
- for (byte i=0; i<dotCount; i++) {
- ellipse (xn[i], yn[i], size, size);
- xn[i] = ( xn[i] + sn[i] ) % width;
- }
- //spaceship that follows mouse
- tx += (mouseX-tx)/40.0;
- ty += (mouseY-ty)/40.0;
- {
- translate(width/tx, height/ty);
- angle = atan2(mouseY-width/40, mouseX-height/40);
- rotate(angle);
- image(mi, tx-50, ty+50, tx+50, ty+50);
- }
- }
1