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 & HelpSyntax Questions › Initializing Variables
Page Index Toggle Pages: 1
Initializing Variables (Read 741 times)
Initializing Variables
Feb 26th, 2010, 1:05pm
 
Hi,

I am a graphic designer and have been learning CSS and XHTML but I think Processing is more difficult (for me at least).

I am learning Processing through the Learning Processing book by Daniel Shiffman. At certain points, it will ask questions about Pong. I played the game and sort of understand what it is about.

On p. 49, it asks "Write out variable declaration and initialization for the game Pong.  Huh I have no idea how to answer that. The book went over Primitive types of variables: (boolean, char, byte, short, int, long, float, double) yet none of them seem applicable for a computer ping pong game. Can someone explain this to me in simple terms?  Undecided
Re: Initializing Variables
Reply #1 - Feb 26th, 2010, 1:41pm
 
Certainly. In the game of pong there's a ball and two paddles that the ball bounces between. If you miss bouncing the ball back, your opponent gets a point. Get 15 points and you win.

Now, if you were a computer, you'd have to remember several very important pieces of information to be able to simulate a game of pong.
You would need to keep track of the score for both players.
You would need to keep track of where the paddles are.
You would need to keep track of the ball's position.
You would need to keep track of the ball's velocity.

The paddles only move up and down, so you only need to remember what their Y position is. Since the paddle's position might not be a whole number, we'll use a float to remember it, like so:
float paddleAsPosition;
float paddleBsposition;
Of course, since we want the paddles to start in the middle of the screen, we need to give them initial values that makes them start there:
float paddleAsPosition = height / 2.0;
float paddleBsPosition = height / 2.0;
Without giving them these's initial values, they'd default to 0.0, which isn't in the middle of the screen!

For the scores, you would need two integer values, as scores can range between 0 and 15, and are always whole numbers.
You would define these variables like this:
int scorePlayerA; // A variable for tracking player A's score.
int scorePlayerB; // A variable for tracking player B's score.
These scores already default to 0, which is what we want. If, however, you wanted a player to lose a life when they missed the ball, you would have to set the number of lives to something other than 0:
int livesPlayerA = 15;
int livesPlayerB = 15;

And for the ball's position, two floats, x position and y position, both starting in the middle of the screen:
float ballX = width / 2.0; // Start the ball in the middle of the screen.
float ballY = height / 2.0;
And the ball's velocity:
float ballVx = 0.1; // Give the ball some initial velocity.
float ballVy = 0.2;
Re: Initializing Variables
Reply #2 - Feb 26th, 2010, 2:17pm
 
Thanks for your reply. It as helped clear some things up.  Smiley

float paddleAsPosition = height / 2.0;
float paddleBsPosition = height / 2.0;


So, 2.0 is the middle of all screens? And why does "/" need to be there?
Re: Initializing Variables
Reply #3 - Feb 26th, 2010, 2:56pm
 
The height of the Processing applet is stored in the variable "height".
I'm dividing it by two so that the paddle's position starts halfway between the top (0) and the bottom (height) of the screen.
Re: Initializing Variables
Reply #4 - Feb 26th, 2010, 11:26pm
 
erinmoll wrote on Feb 26th, 2010, 2:17pm:
So, 2.0 is the middle of all screens And why does "/" need to be there


I have a suspicion you need a very introductory level tutorial on a few things, like what an expression is.

"f / g" means "f divided by g"
"f * g" means "f times g"
+ and - are hopefully obvious (plus and minus)

So, "height / 2" means (approximately) "half the height" or in other words, about halfway down the output window (since y values start from 0 at the top of the window). "height / 2.0" is nearly the same; the difference is that, because height is an integer (whole-number) variable and "2" is an integer constant, "height / 2" specifies an "integer divide" operation (meaning the result will always be an integer), and because "2.0" is a floating-point constant (having the "." makes it so, even though the fractional part is 0), so "height / 2.0" specifies a floating-point divide operation (which can result in a value such as 99.5).

This fundamental description of "/" and "*" appears to be missing from the Learning section of this website (even the Basics section), and I might guess, given your question, also from Shiffman's book.

I do suggest that you have a look in the Basics section all the same, as it may help to explain some simple things that you might have missed reading Shiffman's book.

You might also note that the bottom of the window is actually height-1, because the top is 0, and 0 to height-1 is exactly the value of height.

-spxl

PS: I think you might be supposing that

float paddleAsPosition = height / 2.0;

is something like, in CSS:

border:5px solid red;

(meaning border-width=5px, border-style=solid, border-color=red)

but this is not the case. An assignment such as "x = blah;" is setting the variable x, which has only one value, so "x = blah blah blah blah;" means set the value of x to the result of the expression "blah blah blah blah". The blahs will generaly alternate between symbols (such as +,-,* and /, but also several others) and variable names and/or method calls.
Page Index Toggle Pages: 1