Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- body{
- /*margin:0px;*/
- }
- </style>
- <script>
- document.addEventListener('DOMContentLoaded', function(){
- const canvas = document.getElementById("canvasId");
- const context = canvas.getContext("2d");
- let moveX = 0; // скорост по широчина
- let moveY = 0; // скорост по височина
- let radius = 20; // радиус на топче
- let playerX = 0; // позиция по широчина на палката
- let playerWidth = 100; // широчина на палката
- let x = canvas.width/2; // позиция на център на окръжност
- let y = canvas.height - 20 - radius; // minus visochinata na palkata позиция на център на окръжност
- let brickArray = [];
- const createBricks = () => {
- for (let i = 0; i < 9; i++) {
- let brick = new Brick(0 + i*90, 0, 80, 20);
- brickArray.push(brick);
- }
- }
- createBricks();
- console.log(brickArray);
- canvas.addEventListener('mousemove', (e) => {
- let mouse = mouse_player1(e);
- // context.beginPath();
- // context.fillStyle = "red";
- // context.arc(mouse.x, mouse.y, 20, 0, 2*Math.PI);
- // context.fill();
- // context.closePath();
- playerX = mouse.x - playerWidth/2;
- if(moveX == 0) {
- x = mouse.x;
- }
- });
- canvas.addEventListener('click', (e) => {
- // moveX = moveX == 0 ? (Math.floor(Math.random()*2) == 1 ? 10 : -10) : moveX;
- if(moveX == 0){
- if(Math.floor(Math.random()*2) == 1) {
- moveX = 10;
- } else {
- moveX = -10;
- }
- }
- // moveY = moveY == 0 ? moveY = -14 : moveY;
- if(moveY == 0) {
- moveY = -14;
- }
- });
- const mouse_player1 = (e) => {
- return {
- x: e.layerX,
- y: e.layerY,
- }
- }
- const executeTasks = () =>{
- moveTask();
- drawTask();
- }
- const moveTask = () => {
- x += moveX;
- y += moveY;
- if( x >= canvas.width - radius) {
- moveX = -moveX;
- }
- if( x <= 0 + radius ) {
- moveX = -moveX;
- }
- if(y >= canvas.height - radius*1.55){
- if(x >= playerX - 10 && x <= playerX + playerWidth + 10){
- moveY = -moveY;
- } else {
- console.log('game over');
- restart();
- }
- }
- if(y <= 0 + radius ){
- if(x >= 0 - 10 && x <= 0 + 80 + 10){
- brickArray[0].width = 0;
- }
- moveY = -moveY;
- }
- }
- const restart = () => {
- moveX = 0;
- moveY = 0;
- y = canvas.height - 20 - radius;
- brickArray = [];
- createBricks();
- }
- const drawTask = () => {
- context.clearRect(0, 0, canvas.width, canvas.height);
- context.fillStyle = "green";
- context.beginPath();
- context.arc(x, y, radius, 0, Math.PI*2);
- context.fill();
- context.closePath();
- context.fillStyle = "white";
- context.fillRect(playerX, 580, playerWidth, 20);
- context.fillStyle = "red";
- for (let i = 0; i < brickArray.length; i++) {
- context.fillRect(brickArray[i].x,
- brickArray[i].y,
- brickArray[i].width,
- brickArray[i].height);
- }
- }
- setInterval(executeTasks, 24);
- });
- class Brick{
- constructor(x, y, width, height){
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- }
- }
- </script>
- <style>
- #canvasId{
- background:black;
- }
- </style>
- </head>
- <body>
- <canvas id="canvasId" width="800" height="600"></canvas>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement