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 = [];
- let bonus;
- let colorArray = ["red", "yellow", "royalblue", "green", "firebrick", "lightseagreen", "pink", "orange", "teal", "magenta", "darkgreen", "coral"];
- const createBricks = () => {
- for (let i = 0; i < 9; i++) {
- let rnd = Math.floor(Math.random()*12);
- let brick = new Brick(0 + i*90, 0, 80, 20, colorArray[rnd], false, false);
- brickArray.push(brick);
- }
- }
- createBricks();
- const chooseSolidBrick = () => {
- let rnd = Math.floor(Math.random()*brickArray.length);
- // console.log(rnd);
- brickArray[rnd].solid = true;
- }
- chooseSolidBrick();
- const chooseBonusBrick = () => {
- let rnd = Math.floor(Math.random()*brickArray.length);
- bonus = new Bonus(brickArray[rnd].x + 20, brickArray[rnd].y);
- brickArray[rnd].bonus = true;
- console.log(rnd);
- }
- chooseBonusBrick();
- 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;
- }
- // for(let i = 0; i < brickArray.length; i++){
- // if(x >= brickArray[i].x - 10 && x <= brickArray[i].x + brickArray[i].width + 10){
- // if(brickArray[i].solid == false) {
- // brickArray[i].width = 0;
- // }
- // if(brickArray[i].solid == true){
- // brickArray[i].color = 'gray';
- // }
- // }
- // }
- brickArray.forEach( item => {
- if(x >= item.x - 10 && x <= item.x + item.width + 10){
- if(item.solid == false) {
- item.width = 0;
- if(item.bonus == true){
- bonus.width = 40;
- bonus.height = 40;
- console.log(bonus);
- }
- }
- if(item.solid == true && item.color == 'gray'){
- item.width = 0;
- if(item.bonus == true){
- bonus.width = 40;
- bonus.height = 40;
- console.log(bonus);
- }
- }
- if(item.solid == true && item.color != 'gray'){
- item.color = 'gray';
- }
- }
- });
- moveY = -moveY;
- }
- if(bonus.width != 0){
- bonus.y += 10;
- }
- if(bonus.y >= canvas.height - 20) {
- if(bonus.x >= playerX && bonus.x <= playerX + playerWidth){
- playerWidth = playerWidth*2;
- bonus.width = 0;
- bonus.x = 999;
- }
- }
- }
- const restart = () => {
- moveX = 0;
- moveY = 0;
- y = canvas.height - 20 - radius;
- brickArray = [];
- createBricks();
- chooseSolidBrick();
- chooseBonusBrick();
- }
- 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);
- for (let i = 0; i < brickArray.length; i++) {
- context.fillStyle = brickArray[i].color;
- context.fillRect(brickArray[i].x,
- brickArray[i].y,
- brickArray[i].width,
- brickArray[i].height);
- }
- context.fillStyle = bonus.color;
- context.fillRect(bonus.x, bonus.y, bonus.width, bonus.height);
- }
- setInterval(executeTasks, 24);
- });
- class Brick{
- constructor(x, y, width, height, color, solid, bonus){
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- this.color = color;
- this.solid = solid;
- this.bonus = bonus;
- }
- }
- class Bonus{
- constructor(x, y){
- this.x = x;
- this.y = y;
- this.width = 0;
- this.height = 0;
- this.color = 'blue';
- this.type = 'increase-paddle-size';
- }
- }
- </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