Pong Game with Processing??
in
Programming Questions
•
2 years ago
I've been making a pong game, however one little piece that isn't going right is the ball.
The ball goes straight through the left paddle and doesn't hit it at all, whereas the ball hits the right paddle fine.
I can't figure out exactly where I've gone wrong, it's driving me insane!!
I'm new, could someone please point out the obvious to me? I'm still a beginner and don't understand much 'processing talk'.
p.s. sorry if i post this in the wrong forum, hopefully it's suited into the correct one though
This is my code, please point out what i haven't done and what it should be?
// Global variables for the ball
float paddle_left;
float distWALL = 620;
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 15; // Radius
float dy = 0; // Direction
float lastry;
float paddle_right =10;
//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 * 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 right 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;
}
}
The ball goes straight through the left paddle and doesn't hit it at all, whereas the ball hits the right paddle fine.
I can't figure out exactly where I've gone wrong, it's driving me insane!!
I'm new, could someone please point out the obvious to me? I'm still a beginner and don't understand much 'processing talk'.
p.s. sorry if i post this in the wrong forum, hopefully it's suited into the correct one though
This is my code, please point out what i haven't done and what it should be?
// Global variables for the ball
float paddle_left;
float distWALL = 620;
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 15; // Radius
float dy = 0; // Direction
float lastry;
float paddle_right =10;
//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 * 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 right 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;
}
}
1