Игра арканоид на javascript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="description" content="Free game Arcanoid online">
<meta name="keywords" content="Arcanoid, online, game">
<meta name="author" content="Academy Step Igor Dombrovsky group 32pps21 game Arcanoid">
<title>Gamedev Canvas Workshop</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: rgb(255, 255, 255); display: block; margin: 0 auto; margin-top: 20px; border: 2px solid red; border-radius: 10px;}
.classFooter { position: fixed; bottom: 0px; height: 50; background-color: grey; color: white; font-family: georgia; width: 100%; text-align: center;}
.textFooter{position: relative; top: 50%;}
</style>
</head>
<body>
<canvas id="myCanvas" width="700" height="470"></canvas>
<script>
var speed = 1;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var x = canvas.width / 2;
var y = canvas.height - 30;
var dx = -2;
var dy = -2;
var ballRadius = 10;
var r = Math.floor(Math.random() * 255 - 1);
var g = Math.floor(Math.random() * 255 - 1);
var b = Math.floor(Math.random() * 255 - 1);
var paddleHeight = 10;
var paddleWidth = 100;
var paddleX = (canvas.width-paddleWidth)/2;
var rightPressed = false;
var leftPressed = false;
var brickRowCount = 4;
var brickColumnCount = 8;
var brickWidth = 70;
var brickHeight = 15;
var brickPadding = 10;
var brickOffsetTop = 50;
var brickOffsetLeft = 35;
var score = 0;
var lives = 5;
var lvl = 0;
var bricks = [];
for(c=0; c<brickColumnCount; c++) {
bricks[c] = [];
for(r=0; r<brickRowCount; r++) {
bricks[c][r] = { x: 0, y: 0, status: 1 };
}
}
function drawBricks() {
for(c=0; c<brickColumnCount; c++) {
for(r=0; r<brickRowCount; r++) {
if(bricks[c][r].status == 1) {
var brickX = (c*(brickWidth+brickPadding))+brickOffsetLeft;
var brickY = (r*(brickHeight+brickPadding))+brickOffsetTop;
bricks[c][r].x = brickX;
bricks[c][r].y = brickY;
ctx.beginPath();
ctx.rect(brickX, brickY, brickWidth, brickHeight);
ctx.fillStyle = "rgb("+r+","+g+","+b+")";
ctx.fill();
ctx.closePath();
}
}
}
}
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = "rgb("+r+","+g+","+b+")";
ctx.fill();
ctx.closePath();
}
function drawPaddle() {
ctx.beginPath();
ctx.rect(paddleX, canvas.height-paddleHeight, paddleWidth, paddleHeight);
ctx.fillStyle = "red";
ctx.fill();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawBricks();
drawPaddle();
drawScore();
drawLvl()
drawLives();
collisionDetection();
x += dx; // Change the angle from the paddle
y += dy;
ctx.fillStyle = "rgb("+this.r+","+this.g+","+this.b+")";
if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
dx = -dx;
r = Math.floor(Math.random() * 255 - 1);
g = Math.floor(Math.random() * 255 - 1);
b = Math.floor(Math.random() * 255 - 1);
}
if(y + dy < ballRadius) {
dy = -dy;
r = Math.floor(Math.random() * 255 - 1);
g = Math.floor(Math.random() * 255 - 1);
b = Math.floor(Math.random() * 255 - 1);
} else if(y + dy > canvas.height-ballRadius) {
if(x > paddleX && x < paddleX + paddleWidth) {
dy = -dy;
}
else {
lives--;
if(!lives) {
alert("GAME OVER SCORE = " + score);
document.location.reload();
}
else {
x = canvas.width/2;
y = canvas.height-30;
dx = 2;
dy = -2;
paddleX = (canvas.width-paddleWidth)/2;
}
}
}
if(rightPressed && paddleX < canvas.width-paddleWidth) {
paddleX += 7; // speed paddle
}
else if(leftPressed && paddleX > 0) {
paddleX -= 7; // speed paddle
}
//requestAnimationFrame(draw);
}
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
document.addEventListener("mousemove", mouseMoveHandler, false);
function keyDownHandler(e) {
if(e.keyCode == 39) {
rightPressed = true;
}
else if(e.keyCode == 37) {
leftPressed = true;
}
}
function keyUpHandler(e) {
if(e.keyCode == 39) {
rightPressed = false;
}
else if(e.keyCode == 37) {
leftPressed = false;
}
}
function mouseMoveHandler(e) {
var relativeX = e.clientX - canvas.offsetLeft;
if(relativeX > 0 && relativeX < canvas.width) {
paddleX = relativeX - paddleWidth/2;
}
}
function collisionDetection() {
for(c=0; c<brickColumnCount; c++) {
for(r=0; r<brickRowCount; r++) {
var b = bricks[c][r];
if(b.status == 1) {
if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
dy = -dy;
b.status = 0;
r = Math.floor(Math.random() * 255 - 1);
g = Math.floor(Math.random() * 255 - 1);
b = Math.floor(Math.random() * 255 - 1);
score += 15;
if(score == (brickRowCount*brickColumnCount * 15)) { // CONGRATULATIONS
alert("YOU WIN, CONGRATULATIONS! SCORE = " + score);
// document.location.reload();
lvl++;
lives = 5;
bricks = [];
for(c=0; c<brickColumnCount; c++) {
bricks[c] = [];
for(r=0; r<brickRowCount; r++) {
bricks[c][r] = { x: 0, y: 0, status: 1 };
}
}
}
}
}
}
}
}
function drawScore() {
ctx.font = "16px sans-serif";
ctx.fillStyle = "blue";
ctx.fillText("Score: " + score, 8, 20);
}
function drawLives() {
ctx.font = "16px sans-serif";
ctx.fillStyle = "red";
ctx.fillText("Lives: " + lives, canvas.width-65, 20);
}
function drawLvl() {
ctx.font = "16px sans-serif";
ctx.fillStyle = "green";
ctx.fillText("lvl: " + lvl, 345, 20);
}
//draw();
setInterval(draw, speed);
</script>
<footer class="classFooter">
<p class="textFooter">Academy Step Web course 2019 Game Arcanoid Dombrovsky I.V.
</footer>
</body>
</html>
Результат перейдите по ссылке!