Skip to content

Instantly share code, notes, and snippets.

@willyandan
Created February 1, 2019 00:42
Show Gist options
  • Save willyandan/2daacb518950ca77957c04cfc977b734 to your computer and use it in GitHub Desktop.
Save willyandan/2daacb518950ca77957c04cfc977b734 to your computer and use it in GitHub Desktop.
pong.js - função loop com a lógica para colidir com a barra e com o teto
function loop(){
//Verifica se a bola está colidindo com o barra do player 1
if(ball_x >= p1_x && ball_x <= p1_x + 10 && ball_y >= p1_y && ball_y <= p1_y + p_h){
ball_x_orientation = 1
}
//Verifica se a bola está colidindo com o barra do player 2
else if(ball_x >= p2_x && ball_x <= p2_x + 10 && ball_y >= p2_y && ball_y <= p2_y + p_h){
ball_x_orientation = -1
}
// verifica se a bola bateu no chão ou no teto
if(ball_y + 10 >= h || ball_y <= 0) ball_y_orientation *= -1
//move a bola no eixo X e Y
ball_x += 5 * ball_x_orientation
ball_y += 5 * ball_y_orientation
if(p1_key == 87 && p1_y > 0){
p1_y -= 10
}else if(p1_key == 83 && p1_y + p_h < h){
p1_y += 10
}
if(p2_key == 38 && p2_y > 0){
p2_y -= 10
}else if(p2_key == 40 && p2_y + p_h < h){
p2_y += 10
}
draw()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment