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.
IndexDiscussionExhibition › Trying to make my own pong
Page Index Toggle Pages: 1
Trying to make my own pong (Read 451 times)
Trying to make my own pong
Dec 4th, 2008, 9:49am
 
newbie here. so i took the source code from here(http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1129556196), and am trying to add\edit a few things. firstly, when i try to make a custom speed for the ball(with a keyPressed() function), the ball goes through the paddle, every time. now that i have a fixed increase in speed(x 5.0 to speed it up), there's a bug so that the first ball goes thru the paddle during the start of the program- and i'm tearing my hair out trying to find out why! secondly, i'm trying to randomize and change the color of the ball constantly, as the game is being played, and not just once only, when the program starts. why isn't that working? do i need a for loop, or something?

lastly, when you decrease the size of the paddle(with the right mouse button, left mouse button increases height of paddle), the small paddle doesn't go all the way down. i'm puzzled as to what kind of math would be involved in getting that done. also, i'd like to add a second player to the game. any help would be appreciated, thanks! the code;

// Global variables for the ball
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 15;  // Radius
float dy = 0;  // Direction
int a=0;
float red = random(255);
float green = random(255);
float blue = random(255);

// Global variables for the paddle
int paddle_width = 10;
int paddle_height = 60;

int dist_wall = 15;

void setup()
{
 size(640, 360);
 rectMode(RADIUS);
 ellipseMode(RADIUS);
 noStroke();
 smooth();
 ball_y = height/2;
 ball_x = 1;
}

void draw()
{
 background(51);
 
 ball_x += ball_dir * 5.0; //trying to make the speed custom here
 ball_y += dy;
 if(ball_x > width+ball_size) {
   ball_x = -width/2 - ball_size;
   ball_y = random(0, height);
   dy = 0;
 }
 
 // Constrain paddle to screen
 float paddle_y = constrain(mouseY, paddle_height, height-paddle_height);

 // Test to see if the ball is touching the paddle
 float py = width-dist_wall-paddle_width-ball_size;
 if(ball_x == py
    && ball_y > paddle_y - paddle_height - ball_size
    && ball_y < paddle_y + paddle_height + ball_size) {
   ball_dir *= -1;
   if(mouseY != pmouseY) {
     dy = (mouseY-pmouseY)/2.0;
     if(dy >  5) { dy =  5; }
     if(dy < -5) { dy = -5; }
   }
 }
 
 // If ball hits paddle or back wall, reverse direction
 if(ball_x < ball_size && ball_dir == -1) {
   ball_dir *= -1;
 }
 
 // If the ball is touching top or bottom edge, reverse direction
 if(ball_y > height-ball_size) {
   dy = dy * -1;
 }
 if(ball_y < ball_size) {
   dy = dy * -1;
 }

 // Draw ball
 fill(red, green, blue);
 ellipse(ball_x, ball_y, ball_size, ball_size);
 
 // Draw the paddle
 fill(153);
 rect(width-dist_wall, paddle_y, paddle_width, paddle_height+a);  


 if (mousePressed && (mouseButton == LEFT)) {
   if (a<50) {
   a=a+10; }

   
 }
else if (mousePressed && (mouseButton == RIGHT)) {
if (a > -50) {
 a=a-10; }
}
}

Re: Trying to make my own pong
Reply #1 - Dec 4th, 2008, 12:08pm
 
> that the first ball goes thru the paddle during the start of the program

if(ball_x == py...

that test is very precise, you might want to change it so it implements "if ball_x is equal to or beyond the bat position"

ball starts at x = 1
 ball_x = 1;
and moves 5 pixels at a time
 ball_x += ball_dir * 5.0

ie 6, 11, 16... if the bat isn't exactly at one of these numbers then ball_x == py will fail (py = 600 so...). it's also a bad idea to use == with floats anyway...

> when you decrease the size of the paddle(with the right mouse button, left mouse button increases height of paddle), the small paddle doesn't go all the way down.

 float paddle_y = constrain(mouseY, paddle_height, height-paddle_height);

this looks wrong given this:

 rect(width-dist_wall, paddle_y, paddle_width, paddle_height+a);  

you're constraining the bat based on paddle_height but drawing it based on (paddle_height + a) (and, if you look carefully, a large bat will actually reach beyond the screen edges)
Page Index Toggle Pages: 1