Clicking moving circles
in
Programming Questions
•
1 year ago
I have searched and tried, but I can't get my first test game working. You just click circles to gain points and theyre bouncing around the screen, but it won't recognise that you've clicked one. I have it recording score, but it can't record anything because of this problom I have. Is there any simple way to do it?
I am very new to this 'Processing' tool, only at it a week.
I've seen most people asking questions post their whole code, so I might as well put it up. It could probobly speed this up anyway;
Any help is appreciated.
I am very new to this 'Processing' tool, only at it a week.
I've seen most people asking questions post their whole code, so I might as well put it up. It could probobly speed this up anyway;
- PFont f;
- int xdirection = 1; // Left or Rightint ydirection = 1; // Top to Bottom
- int value = 0;
- Clickball ball1;
- Clickball ball2; // Two objects!
- Clickball ball3;
- Clickball ball4;
- void setup() {
- size(200,200);
- noStroke();
- f = loadFont("AgencyFB-Reg-48.vlw");
- ball1 = new Clickball(color(255,0,0),10,100,2,8);
- ball2 = new Clickball(color(0,0,255),10,130,1,7);
- ball3 = new Clickball(color(0,255,0),10,100,3,8);
- ball4 = new Clickball(color(0,255,255),10,130,4,9);
- }
- void draw() {
- background(255);
- textFont(f,36);
- stroke(175);
- line(width/2,0,width/2,height);
- fill(0);
- textAlign(CENTER);
- text("Score: " + value ,width/2,60);
- ball1.bounce();
- ball1.mouseClicked();
- ball1.display();
- ball2.bounce();
- ball2.mouseClicked();
- ball2.display();
- ball3.bounce();
- ball3.mouseClicked();
- ball3.display();
- ball4.bounce();
- ball4.mouseClicked();
- ball4.display();
- }
- //score(width/2, height/2, 50, 50);
- class Clickball {
- color c;
- float xpos;
- float ypos;
- float xspeed;
- float yspeed;
- Clickball(color tempC, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed) {
- c = tempC;
- xspeed = tempXspeed;
- yspeed = tempYspeed;
- if (xpos > width-200 || xpos < 0) {
- xdirection *= -1;
- }
- if (ypos > height-200 || ypos < 0) {
- ydirection *= -1;
- }
- }
- void display() {
- stroke(0);
- fill(c);
- ellipseMode(CENTER);
- ellipse(xpos,ypos,20,20);
- }
- //Supposed to add 50 points per click. fix later.
- void mouseClicked() {
- if (dist(200,200,mouseX,mouseY) < 50) {
- if (value >= 1) { value = 0; }
- else if (value == 0) { value = 1; }
- }
- }
- {
- if(value == 0) {
- value = + 50;
- } else {
- value = +0;
- }
- }
-
- void bounce() {
- xpos = xpos + xspeed;
-
- xpos = xpos + ( xspeed * xdirection );
- ypos = ypos + ( yspeed * ydirection );
-
- if (xpos > width) {
- xspeed = xspeed * -1;
- }
- if (xpos < 0) {
- xspeed = xspeed * -1;
- }
- if (ypos > height) {
- yspeed = yspeed * -1;
- }
- if (ypos < 0) {
- yspeed = yspeed * -1;
- }
- }
- }
Any help is appreciated.
1