cobra281
YaBB Newbies
Offline
Posts: 1
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; } } }