We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Displaying Text Problem
Page Index Toggle Pages: 1
Displaying Text Problem (Read 1017 times)
Displaying Text Problem
Apr 7th, 2010, 7:14pm
 
Hello. I am new to programming in processing. I recently made a small game, but I cannot make the score display using the text() command. In the game, the user controls a small ellipse with the arrow keys and tries to catch and evading box.
Here is the code:
//created by indianyar
PFont font;
int Time=(minute()*60+second());
int Boxes=0;
int A=100;
int X=100;
int R = int(random(1,199));
int S = int(random(1,199));
int Present=0;
void setup()
{
 size(200, 200, P2D);
 background(0,100,170);
 rect(R,S,10,10);
 font = loadFont("TimesNewRomanPSMT-16.vlw");
 textFont(font);
 fill(240);
}
void draw()
{
background(0,100,170);
Present= minute()*60+second();
if(int(abs(Time-Present))>=60)
{
println("it came here");
delay(250);
println("it came here");
delay(600);
text("You caught this many boxes:", 30, 40);
text(Boxes, 30, 60);
save("evidence.tiff");
println("it came here");
delay(250);
background(0,100,170);
print(Boxes);
exit();
}
keyPressed();
}
void keyPressed()
{
 background(0,100,170);
 rect(R,S,10,10);
 int J = int(random(-10,10));
 int K = int(random(-10,10));
 if((R+J)>=200)
 {
  J=0;
 }
 if((R+J)<=0)
 {
  J=0;
 }
 if((S+K)>=200)
 {
  K=0;
 }
 if((S+K)<=0)
 {
  K=0;
 }

 if (key == CODED) {
   if (keyCode == UP)
   {
     R=R+J;
     S=S+K;
     X=X-1;
     if(X==0)
     {
       X=X+1;
     }
     ellipse(A,X,5,5);
     rect(R,S,10,10);  
   }
   else if (keyCode == DOWN)
   {
     R=R+J;
     S=S+K;
     X=X+1;
     if(X==200)
     {
       X=X-1;
     }
     ellipse(A,X,5,5);
     rect(R,S,10,10);  
   }
   else if (keyCode == LEFT)
   {
     R=R+J;
     S=S+K;
     A=A-1;
     if(A==0)
     {
       A=A+1;
     }
     ellipse(A,X,5,5);
     rect(R,S,10,10);  
   }
   else if (keyCode == RIGHT)
   {
     R=R+J;
     S=S+K;
     if(A==200)
     {
       A=A-1;
     }
     A=A+1;
     ellipse(A,X,5,5);
     rect(R,S,10,10);  
   }
 }
if ((((R+5)>=(A+2.5))&&((R-5)<=(A-2.5))&&((S+5)>=(X+2.5))&&((S-5)<=(X-2.5))) == true)
{
R = int(random(1,199));
S = int(random(1,199));
rect(R,S,10,10);
Boxes=Boxes+1;
}
}
I tested the code by adding print statements, and the program reached all the print statements. I also tested it by using the save function. The saved image shows a screen with the text, but when the user plays the game the user cannot view the score. I am really confused now. I added delay statements, but they did not help.
-
Indianyar
Re: Displaying Text Problem
Reply #1 - Apr 8th, 2010, 1:06am
 
I haven't tested your code, but bear in mind that text is also affected by fill and stroke settings - it may simply be a matter of adding fill([an appropriate colour]) (and maybe stroke too) before your call to text...
Re: Displaying Text Problem
Reply #2 - Apr 8th, 2010, 1:24am
 
Also: don't use delay(). And don't call keyPressed, Processing does that automatically.
Re: Displaying Text Problem
Reply #3 - Apr 8th, 2010, 2:31pm
 
If I remove the keyPressed(); call the game moves a lot slower. The speed is almost reduced by one half. If I do not delay the program how will the player be able to see the text()? Right after the text command comes the exit() command.
-
Indianyar
Re: Displaying Text Problem
Reply #4 - Apr 8th, 2010, 2:54pm
 
I added the stroke() command. Even with stroke it does not display.
-
Indianyar
Re: Displaying Text Problem
Reply #5 - Apr 9th, 2010, 1:23am
 
indianyar101 wrote on Apr 8th, 2010, 2:31pm:
If I do not delay the program how will the player be able to see the text()

That's a logic hard to grasp for lot of people, as it isn't so intuitive.
You won't see graphics made in draw() before the end of draw(). So delay() just slows down your application but doesn't allow text to be seen.
The idea is to have a time counter, either using millis() for something accurate, or frameCount for something more approximative.
You need also a boolean.
The logic is as follows:
When you must display text, get the current time (or frame count), and set the boolean. When the boolean is set, just add text() call to draw().
On each iteration/call of draw(), compare time minus start time to the duration of display. Once it is greater, just reset the boolean to false: the text is no longer displayed.
Re: Displaying Text Problem
Reply #6 - Apr 9th, 2010, 4:28pm
 
Thank you PhiLho, you are a very brilliant person. Also, thank you to blindfish. You guys both helped me a lot. I guess I had a conceptual error in the code. Thank you. If you want to see the finished game go to www.indianyar.com
-
indianyar
Page Index Toggle Pages: 1