Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a better collision effect #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

var canvas;
var canvasContext;
var ballX = 50;
Expand Down Expand Up @@ -52,7 +52,7 @@ var showingWinScreen = false;

function ballReset() {
if(player1score >= WINNING_SCORE || player2score >= WINNING_SCORE){

showingWinScreen = true;
}
ballSpeedX = -ballSpeedX
Expand All @@ -63,47 +63,54 @@ var showingWinScreen = false;
function computerMovement() {
var paddle2YCenter = paddle2Y + (PADDLE_HEIEGHT/2);
if(paddle2YCenter < ballY-35){
paddle2Y += 6;
paddle2Y += 6;
}
else if(paddle2YCenter > ballY+35)
paddle2Y -= 6;

}

function moveEverything(){
if(showingWinScreen){
return;
}
computerMovement();
ballX += ballSpeedX;
ballY += ballSpeedY;

if(ballX > canvas.width){
if(ballY > paddle2Y &&
ballY < paddle2Y+PADDLE_HEIEGHT)

if(ballX > canvas.width - 30){ //this give the illusion of collision
if(ballY > paddle2Y &&
ballY < paddle2Y+PADDLE_HEIEGHT) {
ballSpeedX = -ballSpeedX;
ballX = canvas.width - 30;

}
else{
player1score++;
ballReset();

}}
if(ballX < 0){
if(ballX < 30){ //this give the illusion of collision
//
if(ballY > paddle1Y &&
ballY < paddle1Y+PADDLE_HEIEGHT)
if(ballY > paddle1Y &&
ballY < paddle1Y+PADDLE_HEIEGHT) {
ballSpeedX = -ballSpeedX;
ballX = 30;
}
else{
player2score++;
ballReset();

}}
ballX += ballSpeedX;
if(ballY > canvas.height){
ballSpeedY = -ballSpeedY;

}
if(ballY < 0){
ballSpeedY = -ballSpeedY;
}

ballY += ballSpeedY;
}


Expand All @@ -112,7 +119,7 @@ var showingWinScreen = false;
colorRect(canvas.width/2 - 1, i, 2,20,'white');
}
}
function drawEverything() {
function drawEverything() {
colorRect(0, 0, canvas.width, canvas.height, 'green'); //blank screen
if(showingWinScreen){
canvasContext.fillStyle = 'white';
Expand All @@ -126,12 +133,12 @@ var showingWinScreen = false;
return;
}
drawNet();
colorRect(0, paddle1Y, 10, PADDLE_HEIEGHT,'white') //left player paddle
colorRect(0, paddle1Y, 10, PADDLE_HEIEGHT,'white') //left player paddle
colorRect(canvas.width-10, paddle2Y, 10, PADDLE_HEIEGHT,'white')
colorCircle(ballX, ballY, 10,'white'); //draws the ball
canvasContext.fillText(player1score, 100, 100);
canvasContext.fillText(player2score, canvas.width-100, 100)

}

function colorRect(leftX,topY, width,height, drawColor){
Expand All @@ -144,4 +151,4 @@ var showingWinScreen = false;
canvasContext.beginPath();
canvasContext.arc(centerX, centerY, radius, 0,Math.PI*2, true);
canvasContext.fill();
}
}