0

I am working on a creating a video game, and I want a button to disappear once I click it and move onto the next screen. I can set the function of the button through my html and js file, but I am not sure how to make it disappear once clicked.

My HTML code:

<html>
<head>
<title> Pong </title>
<link type = "text/css" href = "main.css" rel = "stylesheet">
</head>
<canvas id="gameCanvas" width="1350" height="600"></canvas>
<body>
<button id = 'easy_click' onclick="EasyChange()"> Easy </button>
<button id = 'int_change' onclick="intChange()"> Intermediate </button>
<button id = "hard_change" onclick="hardChange()"> Hard </button>
</body>
<script src = "functions.js"></script>

</html>

Here is my JS code:

    var canvas;
var canvasContext;
var ballX = 50;
var ballY = 50;
var ballSpeedX = 20;
var ballSpeedY = 10
var menuScreen = false;
var name = "Developed by Jonah Johnson";
var player1Score = 0;
var player2Score = 0;
const WINNING_SCORE = 10;
var easybutton = "Easy";
var hardButton = "Hard";
var intButton = "Intermediate";
var diffLevelEasy = false;
var diffLevelMedium = true;
var diffLevelHard = false;
var showingWinScreen = true;

var paddle1Y = 250;
var paddle2Y = 250;
const PADDLE_THICKNESS = 10;
const PADDLE_HEIGHT = 100;

function calculateMousePos(evt) {
    var rect = canvas.getBoundingClientRect();
    var root = document.documentElement;
    var mouseX = evt.clientX - rect.left - root.scrollLeft;
    var mouseY = evt.clientY - rect.top - root.scrollTop;
    return {
        x:mouseX,
        y:mouseY
    };
}

//changing of difficulty
//easy changing
function EasyChange() {
    diffLevelEasy = true;
    diffLevelMedium = false;
    diffLevelHard = false;
    showingWinScreen = false;
}

function intChange() {
    diffLevelEasy = false;
    diffLevelMedium = true;
    diffLevelHard = false;
    showingWinScreen = false;

}

function hardChange() {
    diffLevelEasy = false;
    diffLevelMedium = false;
    diffLevelHard = true;
    showingWinScreen = false;
}

function handleMouseClick(evt) {
    if(showingWinScreen) {
        player1Score = 0;
        player2Score = 0;
        showingWinScreen = false;
    }
}
// getting an easy medium
function handleMouseClick(evt) {
   if(easybutton) {
       diffLevelEasy = true;
       diffLevelMedium = false;
       diffLevelHard = false;
       showingWinScreen = false;
   }
}
//getting medium
function handleMouseClick(evt) {
    if(intButton) {
        diffLevelEasy = false;
       diffLevelMedium = true;
       diffLevelHard = false;
       showingWinScreen = false;

    }
}
//getting hard
function handleMouseClick(evt) {
    if(hardButton) {
        diffLevelEasy = false;
        diffLevelMedium = false;
        diffLevelHard = true;
        showingWinScreen = false;
    }
}


window.onload = function() {
    canvas = document.getElementById('gameCanvas');
    canvasContext = canvas.getContext('2d');

    var framesPerSecond = 30;
    setInterval(function() {
            moveEverything();
            drawEverything();   
        }, 1000/framesPerSecond);

    canvas.addEventListener('mousedown', handleMouseClick);

    canvas.addEventListener('mousemove',
        function(evt) {
            var mousePos = calculateMousePos(evt);
            paddle1Y = mousePos.y - (PADDLE_HEIGHT/2);
        });
}

function ballReset() {
    if(player1Score >= WINNING_SCORE ||
        player2Score >= WINNING_SCORE) {

        showingWinScreen = true;

    }

    ballSpeedX = -ballSpeedX;
    ballX = canvas.width/2;
    ballY = canvas.height/2;
}

function computerMovement() {   
    /// ai movement
    //easy
    if(diffLevelEasy === true) {
    var paddle2YCenter = paddle2Y + (PADDLE_HEIGHT/2);
    if(paddle2YCenter < ballY - 35) {
        paddle2Y = paddle2Y + 2;
    } else if(paddle2YCenter > ballY + 35) {
        paddle2Y = paddle2Y - 2;
    }
}
    //medium
    else if(diffLevelMedium === true) {
    var paddle2YCenter = paddle2Y + (PADDLE_HEIGHT/2);
    if(paddle2YCenter < ballY - 35) {
        paddle2Y = paddle2Y + 12;
    } else if(paddle2YCenter > ballY + 35) {
        paddle2Y = paddle2Y - 12;
    }
}
    //hard
    else if(diffLevelHard === true) {
    var paddle2YCenter = paddle2Y + (PADDLE_HEIGHT/2);
    if(paddle2YCenter < ballY - 35) {
        paddle2Y = paddle2Y + 18;
    } else if(paddle2YCenter > ballY + 35) {
        paddle2Y = paddle2Y - 18;
    }
    }
}


function moveEverything() {
    if(showingWinScreen) {                         //showing win screen
        return;
    }

    computerMovement();

    ballX = ballX + ballSpeedX;
    ballY = ballY + ballSpeedY;

    if(ballX < 0) {
        if(ballY > paddle1Y &&
            ballY < paddle1Y+PADDLE_HEIGHT) {
            ballSpeedX = -ballSpeedX;

            var deltaY = ballY
                    -(paddle1Y+PADDLE_HEIGHT/2);
            ballSpeedY = deltaY * 0.35;
        } else {
            player2Score++; // must be BEFORE ballReset()
            ballReset();
        }
    }
    if(ballX > canvas.width) {
        if(ballY > paddle2Y &&
            ballY < paddle2Y+PADDLE_HEIGHT) {
            ballSpeedX = -ballSpeedX;

            var deltaY = ballY
                    -(paddle2Y+PADDLE_HEIGHT/2);
            ballSpeedY = deltaY * 0.35;
        } else {
            player1Score++; // must be BEFORE ballReset()
            ballReset();    
        }
    }
    if(ballY < 0) {
        ballSpeedY = -ballSpeedY;
    }
    if(ballY > canvas.height) {
        ballSpeedY = -ballSpeedY;
    }
}

function drawNet() {
    for(var i=0;i<canvas.height;i+=40) {
        colorRect(canvas.width/2-1,i,2,20,'white');
    }
}

function drawEverything() {
    // next line blanks out the screen with black
    colorRect(0,0,canvas.width,canvas.height,'black');


    if(showingWinScreen) {
        canvasContext.fillStyle = 'white';

        if(player1Score >= WINNING_SCORE) {
            canvasContext.fillText("Left Player Won", 350, 200);
        } else if(player2Score >= WINNING_SCORE) {
            canvasContext.fillText("Right Player Won", 350, 200);
        }

        canvasContext.fillText("click to continue", 350, 500);
        canvasContext.fillText("Difficulty:", 50, 50);
        canvasContext.fillText(easybutton, 50, 100);
        canvasContext.fillText(intButton, 50, 200);
        canvasContext.fillText(hardButton, 50, 300);
        return;
    }

    drawNet();

    // this is left player paddle
    colorRect(0,paddle1Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');

    // this is right computer paddle
    colorRect(canvas.width-PADDLE_THICKNESS,paddle2Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white');

    // next line draws the ball
    colorCircle(ballX, ballY, 10, 'white');

    canvasContext.fillText(player1Score, 100, 100);
    canvasContext.fillText(player2Score, canvas.width-100, 100);
    canvasContext.fillText(name, 100, 590);
}

function colorCircle(centerX, centerY, radius, drawColor) {
    canvasContext.fillStyle = drawColor;
    canvasContext.beginPath();
    canvasContext.arc(centerX, centerY, radius, 0,Math.PI*2,true);
    canvasContext.fill();
}

function colorRect(leftX,topY, width,height, drawColor) {
    canvasContext.fillStyle = drawColor;
    canvasContext.fillRect(leftX,topY, width,height);
}
//sets how hard the game is
if(diffLevel = diffLevelEasy) {
var paddle2YCenter = paddle2Y + (PADDLE_HEIGHT/2);
    if(paddle2YCenter < ballY - 35) {
        paddle2Y = paddle2Y + 2;
    } else if(paddle2YCenter > ballY + 35) {
        paddle2Y = paddle2Y - 2;
    }
}

I am just trying to make the button delete when clicked, so how do I do that?

1
  • set the css to hide it? Commented Oct 16, 2017 at 15:48

3 Answers 3

1

Found this on web try it

<input type="button" id="toggler" value="Toggler" onClick="action();" />
<input type="button" id="togglee" value="Togglee" />

<script>
    var hidden = false;
    function action() {
        hidden = !hidden;
        if(hidden) {
            document.getElementById('togglee').style.visibility = 'hidden';
        } else {
            document.getElementById('togglee').style.visibility = 'visible';
        }
    }
</script>

Sign up to request clarification or add additional context in comments.

Comments

0

Just call evt.target.remove() and it will remove it from the DOM.

Comments

0

For future scalability and probably, maintainability and with minimum code changes, I'd send the button as an argument to the event handler:

<button id = 'easy_click' onclick="EasyChange(this)"> Easy </button>
<button id = 'int_change' onclick="intChange(this)"> Intermediate </button>
<button id = "hard_change" onclick="hardChange(this)"> Hard </button>

Then set the style in the handler function:

function EasyChange(btn) {
    diffLevelEasy = true;
    diffLevelMedium = false;
    diffLevelHard = false;
    showingWinScreen = false;
    btn.style.display='none';

}

function intChange(btn) {
    diffLevelEasy = false;
    diffLevelMedium = true;
    diffLevelHard = false;
    showingWinScreen = false;
btn.style.display='none';
}

function hardChange(btn) {
    diffLevelEasy = false;
    diffLevelMedium = false;
    diffLevelHard = true;
    showingWinScreen = false;
    btn.style.display='none';
}

To restore the button use:

btn.style.display='none';

Alternatively, to stop the DOM jumping about use:

btn.style.visibility='hidden';

and

btn.style.visibility='visible';

Also let me know when and where it's published because web pong sounds awesome.

1 Comment

It's as awesome as it sounds. There goes my morning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.