I have some problems with my code, which I can't really figure it out. So I started programming the pong game but I don't know how to include the paddle, so the ball bounces off when hitting the paddle but goes through, when it isn't. I've tried if- & else statements and a constrain function for this but it didn't work somehow.
My code:
float ballxposition = 0; float ballyposition = 0; float speedx = 0;
float speedy = 0;
void setup() { size(1024, 768); speedx = 10; speedy = 10; }
void draw() { background(255); fill(128); rectMode(CENTER);
rect(mouseX, 730, 250, 20);
fill(0, 0, 255); ellipse(ballxposition, ballyposition, 30, 30);
ballxposition = ballxposition + speedx; ballyposition = ballyposition
+ speedy;
if (ballxposition>width||ballxposition<0) {
speedx = speedx * -1; }
if (ballyposition>height||ballyposition<0) {
speedy = speedy * -1; } }
Would really appreciate your help. Thanks.
You may find this youtube video worth watching it's created by a guy called Dan Shiffman his website is The Coding Train: http://thecodingtrain.com
Coding Challenge #67: Pong! https://www.youtube.com/watch?v=IIrC5Qcb2G4
I find his coding challenges very useful