Created
February 1, 2019 02:59
-
-
Save willyandan/5714cde16297c5957fc3fe75ec1f7688 to your computer and use it in GitHub Desktop.
pong.js - loop verificando pontuação
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
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 passou 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(ball_x+10 > w) { | |
p1_points++ | |
initBall() | |
} | |
else if(ball_x < 0){ | |
p2_points ++ | |
initBall() | |
} | |
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