Last active
March 1, 2023 19:40
-
-
Save dc74089/4094da7928839063ae06 to your computer and use it in GitHub Desktop.
Simple Pong Game in Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float x, y, speedX, speedY; | |
float diam = 10; | |
float rectSize = 200; | |
void setup() { | |
fullScreen(); | |
fill(0, 255, 0); | |
reset(); | |
} | |
void reset() { | |
x = width/2; | |
y = height/2; | |
speedX = random(3, 5); | |
speedY = random(3, 5); | |
} | |
void draw() { | |
background(0); | |
ellipse(x, y, diam, diam); | |
rect(0, 0, 20, height); | |
rect(width-30, mouseY-rectSize/2, 10, rectSize); | |
x += speedX; | |
y += speedY; | |
// if ball hits movable bar, invert X direction | |
if ( x > width-30 && x < width -20 && y > mouseY-rectSize/2 && y < mouseY+rectSize/2 ) { | |
speedX = speedX * -1; | |
} | |
// if ball hits wall, change direction of X | |
if (x < 25) { | |
speedX *= -1.1; | |
speedY *= 1.1; | |
x += speedX; | |
} | |
// if ball hits up or down, change direction of Y | |
if ( y > height || y < 0 ) { | |
speedY *= -1; | |
} | |
} | |
void mousePressed() { | |
reset(); | |
} |
Rainster239
commented
Oct 14, 2019
via email
You have 2 options: limit the speed or make a system within the bar hitbox
is bigger so the ball can't go through the bar. Hope it helps.
El lun., 14 oct. 2019 a las 11:51, VevarN (<[email protected]>)
escribió:
… I tried this and it´s great, just that when the ball reaches a certain
speed, it goes straight through the moveable bar. Any ideas on how to fix?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/4094da7928839063ae06?email_source=notifications&email_token=AMPOJMJ6YVSMUVQI5IRM2SLQOQ6LJA5CNFSM4H4KY6TKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF2OAQ#gistcomment-3054600>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMPOJMK6W5KPF64GCMIVK23QOQ6LJANCNFSM4H4KY6TA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment