Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <DOCTYPE html>
- <html>
- <head>
- <meta charset="utf8"/>
- <style>
- html, body{margin: 0; padding: 0; background: #95a5a6;}
- #start{
- width: 600px;
- height: 100px;
- font-size: 65px;
- font-family: verdana;
- text-align: center;
- position: absolute;
- cursor: pointer;
- color: #34495e;
- }
- #ball{
- width: 20px;
- height: 20px;
- position: absolute;
- background: #d35400;
- margin-top: -10px;
- margin-left: -10px;
- border-radius: 10px;
- }
- #wall{
- width: 190px;
- background: #34495e;
- margin: 0;
- padding: 0;
- }
- #points{
- width: 100px;
- height: 50px;
- position: absolute;
- top: 10;
- left: 10;
- font-size: 24px;
- font-family: verdana;
- color: #95a5a6;
- }
- #player1{
- width: 5px;
- height: 100px;
- background: #e67e22;
- position: absolute;
- border-radius: 5px;
- }
- </style>
- <script>
- var windowWidth = window.innerWidth;
- var windowHeight = window.innerHeight;
- var ballRandomStartX = Math.floor((Math.random() * (windowWidth-310)) + 300);
- var ballRandomStartY = Math.floor((Math.random() * (windowHeight/2)) + 10);
- var mouseY;
- var endGame = false;
- var pointCounter = 0;
- var xVar = -1;
- var yVar = 0.5;
- function editStart(){
- var start = document.getElementById("start");
- start.style.top = (windowHeight-100)/2;
- start.style.left = (windowWidth-600)/2;
- }
- function startGame(){
- document.getElementById("start").style.display = "none";
- var newBall = document.createElement("div");
- var wall = document.createElement("div");
- var points = document.createElement("div");
- var player1 = document.createElement("div");
- newBall.id = "ball";
- newBall.style.top = ballRandomStartY;
- newBall.style.left = ballRandomStartX;
- wall.id = "wall";
- wall.style.height = windowHeight;
- points.id = "points";
- points.innerHTML = pointCounter;
- player1.id = "player1";
- player1.style.left = window.innerWidth-100;
- player1.style.top = 0;
- document.body.appendChild(newBall);
- document.body.appendChild(wall);
- document.body.appendChild(points);
- document.body.appendChild(player1);
- }
- function moveBall(){
- var ball = document.getElementById("ball");
- if(ballRandomStartX === 200){
- xVar = 1;
- }
- if(ballRandomStartY === windowHeight-10){
- yVar = -1;
- }
- if(ballRandomStartY === 10){
- yVar = 1;
- }
- if(ballRandomStartX === windowWidth-110 && (ballRandomStartY >= mouseY && ballRandomStartY <= mouseY+100)){
- xVar = -1;
- pointCounter++;
- document.getElementById("points").innerHTML = pointCounter;
- }
- else{
- endGame = true;
- //xVar = -1;
- }
- ballRandomStartY = ballRandomStartY + yVar;
- ballRandomStartX = ballRandomStartX + xVar;
- ball.style.top = ballRandomStartY;
- ball.style.left = ballRandomStartX;
- }
- function movePlayer(){
- mouseY = event.clientY;
- var palyer1 = document.getElementById("player1");
- if(!(mouseY>windowHeight-100)){
- player1.style.top = mouseY;
- }
- }
- </script>
- </head>
- <body onmousemove="movePlayer()">
- <div id="start" onclick="startGame()">Click to START!</div>
- <script>
- editStart();
- if(endGame === false){
- setInterval(moveBall, 1);
- }
- else{
- clearInterval(moveBall);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement