How can i display the seconds? (And some other beginner questions)

Hi folks,

i just discovered Processing and learning it since 3 days. It is my first programming experience, so please forgive me if the questions are dumb. :\">

I would be really happy if you can help me.

Cheers :-h

Issues:

-I could not display the seconds. I just tried append the result of frameRate/60 to IntList inventoryTime and display it as text on the screen and if ball is outside of height inventoryTime.clear(); but i think it is was just stupid idea.

-My score screen doesn't work perfectly. I think another stupid idea of me.

-Ball doesn't return if it hits the racket. No idea about this, even it should be a stupid idea. I could think no solution for this.

void setup() {
  size(600, 400);
  background(#C70039);
  smooth();
  noStroke();
}

float ballX = 0;
float ballY = 0;
float speedX = random(2, 4);
float speedY = random(2, 5);
color cWon = #C70039;
color cLost = #FF5733;
int score = 0;

void draw() {
  IntList timeInventory;
  timeInventory = new IntList();
  timeInventory.append(frameCount/60);    
  background(cWon);
  println (timeInventory);
  println (ballY);

  if (ballX < 0 || ballX > width) speedX = -speedX;
  {
    ballX += speedX;
    ballY += speedY;
    ellipse(ballX, ballY, height/15, height/15);

    float boardLimit = constrain(mouseX, width/6, width/1.2);
    rectMode (CENTER);
    rect (boardLimit, height/1.05, width/3, height/20, height/3);

    textSize(height/25);
    textAlign(TOP);
    text("Time:" + timeInventory.get(0) +"s", height/40, height/20);

    float paddle = 1000/(score+10);
    float distance = abs (mouseX-ballX);
    if (distance < paddle) score += 1;
    textSize(height/28);
    text("Score:" + score, height/40, height/10);
  }
  if (ballY > height) {
    background(cLost);
    textSize(height/10);
    textAlign(CENTER);
    text("TRY AGAIN", width/2, height/2);
    timeInventory.clear();
  }
}

void mouseClicked() {
  if (ballY > height) {
    ballX=random(0, width);
    ballY=0;
  }
}
Tagged:

Answers

  • For time look at millis

    Take a variable startTime to store the millis when you started

    For the paddle: check if ball is near screen border and if ball x is > paddle side and ball x < paddle side

    If yes reflect, if not restart (ball missed paddle)

  • In your score screen, I think it's just a basic silly mistake - your second parameter probably should've been width/40, not height/40 L-)

  • Chrisir, you are just amazing! I've learned a lot from your comments in this forum because you almost have a post in threads which i found after google search. But i still lack on theory, i can imagine what you mean but still cannot write the right codes. Can you post the fixed codes (not all, just how much you can for this time).

    Lord, oh yes just a little mistake but it doesn't affect the issues.

  • edited April 2017

    My library may come of use for your time issue - https://github.com/Lord-of-the-Galaxy/Timing-Utilities. Please ask me if you think the documentation is incomplete.

  • Thank you but i am not able to use any library, i am still trying to learn basics. :/

  • Oh, I see.
    You will have to learn about OOP to be able to use any libraries.
    If you have time, read this - https://processing.org/tutorials/objects/

  • Answer ✓
        float ballX = 0;
        float ballY = 0;
        float speedX = random(2, 4);
        float speedY = random(2, 5);
        color cWon = #C70039;
        color cLost = #FF5733;
        int score = 0;
    
        int startTime;
    
        // ------------------------------------------------
    
        void setup() {
          size(600, 400);
          background(#C70039);
          smooth();
          noStroke();
          noCursor(); 
    
          startTime=millis();
        }
    
    
        void draw() {
          IntList timeInventory;
          float boardLimit ;
    
          timeInventory = new IntList();
          timeInventory.append((millis()-startTime)/1000);    
          background(cWon);
          println (timeInventory);
          println (ballY);
    
          // ----
          if (ballX < 0 || ballX > width) 
            speedX = -speedX;
          if (ballY < height/30 ) 
            speedY = abs(speedY); // always positive result
    
          // ----
          {
            // draw ball
            ballX += speedX;
            ballY += speedY;
            ellipse(ballX, ballY, height/15, height/15);
    
            boardLimit = constrain(mouseX, width/6, width/1.2);
            rectMode (CENTER);
            rect (boardLimit, height/1.05, 
              width/3, height/20, 
              height/3);
    
            textSize(height/25);
            textAlign(TOP);
            text("Time:" + timeInventory.get(0) +"s", height/40, height/20);
    
            float paddle = 1000/(score+10);
            float distance = abs (mouseX-ballX);
            if (distance < paddle) score += 1;
            textSize(height/28);
            text("Score:" + score, height/40, height/10);
          }
    
          if (ballY >  height/1.05 - height/20) {
            // reflect on paddle!!!!!
            if (ballX>boardLimit-width/6 &&
              ballX<boardLimit+width/6) {
              speedY=-abs(speedY);
            } else {
              background(cLost);
              textSize(height/10);
              textAlign(CENTER);
              text("TRY AGAIN", width/2, height/2);
              timeInventory.clear();
            }//else
          }//outer if
          //
        }
    
        void mouseClicked() {
          if (ballY > height) {
            ballX=random(0, width);
            ballY=0;
          }
        }
    
  • edited April 2017 Answer ✓

    Remark 1

    I am not sure why you do all these width/6, width/1.2....

    so it works on different screen sizes?

    I guess it would be better style to define those things once and for all in setup() like

    paddleWidth = width/3; 
    paddleWidthHalf = width/6; 
    

    etc.

    Remark 2

    I hope you realize the {....} block from line 41 to 62 is not connected to the if part but stands alone. Not sure why you use { } here at all.

    Remark 3

    As for the score....:

    move this score += 1; after line 68 instead of 59

  • You are my hero Chrisir, thank you so much!

    All these width/6 etc. were for case ''fullScreen();''.

    i've learned too many things from you again. Cheers!

  • edited April 2017 Answer ✓

    This

          if (ballX < 0 || ballX > width) 
            speedX = -speedX;
    

    can lead to occasionally stuttering in the reverse zone.

    Instead split into two ifs and use abs(...) and -abs(...) as shown above.

Sign In or Register to comment.