Displaying frameCount on the canvas

Hello! I'm making a video game where you try to avoid green circles as you control the blue circle with your mouse. I've made it so that the screen flashes red when you intersect with one of the green circles and the enemies reset their positions randomly. I would like to have the frameCount as the score and I've displayed it on the canvas as "Score: " but i can't get it to display the frameCount as well. Any suggestions? Also I'd like to make it so that you only have 3 lives and then the game is over. Any suggestions there?

Any help is much appreciated!

float posX = int(random(1,499));
float posY = int(random(1,449));
float posX2 = int(random(1,499));
float posY2 = int(random(1,449));
float posX3 = int(random(1,499));
float posY3 = int(random(1,449));
int speedY = 10;
int speedX = 10;
int speedX2 = 8;
int speedY2 = 8;
int speedX3 = 7;
int speedY3 = 7;
float n = 350;
float z = 200;
float x = posX;
float y = posY;
float x1 = posX2;
float x2 = posX3;
float y1 = posY2;
float y2 = posY3;
PFont f = createFont("Arial", 48);

void setup() {
  size(500, 500);
}

void draw() {
  background(0);
  fill(0,255,0);

  posX = posX + speedX;
  posY = posY + speedY;
  posX2 = posX2 + speedX2;
  posY2 = posY2 + speedY2;
  posX3 = posX3 + speedX3;
  posY3 = posY3 + speedY3;

  if (posX > width) {
    speedX = speedX* -1;
  }
  if (posX < 0) {
    speedX = speedX* -1;
  }
  if (posY > height) {
    speedY= speedY* -1;
  }
  if (posY < 0) {
    speedY= speedY*-1;
  }
  ellipse(posX, posY, 60, 60);
  if (posX2 > width) {
    speedX2 = speedX2* -1;
  }
  if (posX2 < 0) {
    speedX2 = speedX2* -1;
  }
  if (posY2 > height) {
    speedY2 = speedY2* -1;
  }
  if (posY2 < 0) {
    speedY2 = speedY2*-1;
  }
  ellipse(posX2, posY2, 70, 70);
  if (posX3 > width) {
    speedX3 = speedX3* -1;
  }
  if (posX3 < 0) {
    speedX3 = speedX3* -1;
  }
  if (posY3 > height) {
    speedY3 = speedY3* -1;
  }
  if (posY3 < 0) {
    speedY3 = speedY3*-1;
  }
  ellipse(posX3, posY3, 50, 50);

  fill(0,0,255);
  ellipse(n, z, 40, 40);

    n = n + (mouseX - n)/2.0;
    z = z + (mouseY - z)/2.0;

  x = posX + speedX;
  y = posY + speedY;
  x1 = posX2 + speedX2;
  y1 = posY2 + speedY2;
  x2 = posX3 + speedX3;
  y2 = posY3 + speedY3;

  if(dist(x,y,n,z) <= 50) {
    background(255,0,0);
    posX = random(1.0,499.0);
    posY = random(1.0,499.0);
  }
  if(dist(x1,y1,n,z) <= 55) {
    background(255,0,0);
    posX2 = random(1.0,499.0);
    posY2 = random(1.0,499.0);
  }
  if(dist(x2,y2,n,z) <= 45) {
    background(255,0,0);
    posX3 = random(1.0,499.0);
    posY3 = random(1.0,499.0);
  }
    fill(#FFAA99);
    textFont(f);
    text("Score: \frameCount", 0, height-2);
}

Answers

  • edited March 2014

    "i can't get it to display the frameCount as well"
    Why? Do you have an error? Bad display?

    You can reuse your text() call or make a second one at a different position.

  • here, I'll update the code to what I was trying

  • int lives = 3;
    void setup(){
      size(200,200);
      noStroke();
    }
    void draw(){
      background(0);
      fill(255);
      text(frameCount,20,20);
      for(int i=0;i<3;i++){
        fill((i<lives)?(color(0,255,0)):(color(255,0,0)));
        rect(20+30*i,40,20,20);
      }
    }
    void mousePressed(){
      lives--;
      if(lives==-1)lives=3;
    }
    
Sign In or Register to comment.