Making paddles for Pong game?????
in
Programming Questions
•
1 years ago
Hi guys,
I'm not great with Processing but I need help. I've been trying to create a 4 player Pong game, but so far I can only get 2 paddles to show up and fully work. I want the 2 new paddles to be where the first 2 are, as if players are playing in doubles (like double-players playing tennis).
If anyone is able to help with actually drawing the paddles up and getting the ball to bounce off the 2 new paddles, I'd sell my soul to you. I'm so keen on getting this to work.
This is what I have so far:
// Global variables for the ball
float paddle_left;
float distWALL = 620;
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 10; // Radius
float dy = 0; // Direction
float lastry;
float paddle_right =10;
//variables for the paddle
int paddle_width = 8;
int paddle_height = 50;
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 * 1.0;
ball_y += dy;
if (ball_x > width+ball_size) {
ball_x = -width/2 - ball_size;
ball_y = random(0, height);
dy = 0;
}
// Testing to see if the ball is touching right paddle
float py = width-dist_wall-paddle_width-ball_size;
if (ball_x == py
&& ball_y > paddle_right - paddle_height - ball_size
&& ball_y < paddle_right + paddle_height + ball_size) {
ball_dir *= -1;
if (paddle_right != paddle_right-5) {
dy = (paddle_right-paddle_right-5)/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 ball touches top or bottom edge, reverse direction
if (ball_y > height-ball_size) {
dy = dy * -1;
}
if (ball_y < ball_size) {
dy = dy * -1;
}
// Testing to see if the ball is touching left paddle
float PY = width-distWALL+paddle_width+ball_size;
if (ball_x == PY
&& ball_y > paddle_left - paddle_height - ball_size
&& ball_y < paddle_left + paddle_height + ball_size) {
ball_dir *= -1;
if (paddle_left != paddle_left-5) {
dy = (paddle_left-paddle_left-5)/2.0;
if (dy > 5) {
dy = 5;
}
if (dy < -5) {
dy = -5;
}
}
}
// Draw ball
fill(255);
ellipse(ball_x, ball_y, ball_size, ball_size);
// Draw the paddle
fill(153);
rect(width-dist_wall, paddle_right, paddle_width, paddle_height);
rect(width-distWALL, paddle_left, paddle_width, paddle_height);
}
// Keypressing options
void keyPressed() {
if (keyCode == UP) {
lastry = paddle_right;
paddle_right=paddle_right-5;
background(0);
}
if (keyCode == DOWN) {
lastry = paddle_right;
paddle_right=paddle_right+5;
background(0);
}
if (key == 'a') {
lastry = paddle_left;
paddle_left=paddle_left-5;
background(0);
}
if (key == 'z') {
lastry = paddle_left;
paddle_left=paddle_left+5;
background(0);
}
if (paddle_right <= 0) //stop paddle from leaving top of screen
{
paddle_right = paddle_right+5;
}
if (paddle_right >= height-10) //stop paddle from leaving bottom of screen
{
paddle_right=height-15;
}
}
THANK YOU in advanced =) Much appreciated
I'm not great with Processing but I need help. I've been trying to create a 4 player Pong game, but so far I can only get 2 paddles to show up and fully work. I want the 2 new paddles to be where the first 2 are, as if players are playing in doubles (like double-players playing tennis).
If anyone is able to help with actually drawing the paddles up and getting the ball to bounce off the 2 new paddles, I'd sell my soul to you. I'm so keen on getting this to work.
This is what I have so far:
// Global variables for the ball
float paddle_left;
float distWALL = 620;
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 10; // Radius
float dy = 0; // Direction
float lastry;
float paddle_right =10;
//variables for the paddle
int paddle_width = 8;
int paddle_height = 50;
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 * 1.0;
ball_y += dy;
if (ball_x > width+ball_size) {
ball_x = -width/2 - ball_size;
ball_y = random(0, height);
dy = 0;
}
// Testing to see if the ball is touching right paddle
float py = width-dist_wall-paddle_width-ball_size;
if (ball_x == py
&& ball_y > paddle_right - paddle_height - ball_size
&& ball_y < paddle_right + paddle_height + ball_size) {
ball_dir *= -1;
if (paddle_right != paddle_right-5) {
dy = (paddle_right-paddle_right-5)/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 ball touches top or bottom edge, reverse direction
if (ball_y > height-ball_size) {
dy = dy * -1;
}
if (ball_y < ball_size) {
dy = dy * -1;
}
// Testing to see if the ball is touching left paddle
float PY = width-distWALL+paddle_width+ball_size;
if (ball_x == PY
&& ball_y > paddle_left - paddle_height - ball_size
&& ball_y < paddle_left + paddle_height + ball_size) {
ball_dir *= -1;
if (paddle_left != paddle_left-5) {
dy = (paddle_left-paddle_left-5)/2.0;
if (dy > 5) {
dy = 5;
}
if (dy < -5) {
dy = -5;
}
}
}
// Draw ball
fill(255);
ellipse(ball_x, ball_y, ball_size, ball_size);
// Draw the paddle
fill(153);
rect(width-dist_wall, paddle_right, paddle_width, paddle_height);
rect(width-distWALL, paddle_left, paddle_width, paddle_height);
}
// Keypressing options
void keyPressed() {
if (keyCode == UP) {
lastry = paddle_right;
paddle_right=paddle_right-5;
background(0);
}
if (keyCode == DOWN) {
lastry = paddle_right;
paddle_right=paddle_right+5;
background(0);
}
if (key == 'a') {
lastry = paddle_left;
paddle_left=paddle_left-5;
background(0);
}
if (key == 'z') {
lastry = paddle_left;
paddle_left=paddle_left+5;
background(0);
}
if (paddle_right <= 0) //stop paddle from leaving top of screen
{
paddle_right = paddle_right+5;
}
if (paddle_right >= height-10) //stop paddle from leaving bottom of screen
{
paddle_right=height-15;
}
}
THANK YOU in advanced =) Much appreciated
1