池袋にて創業45年。法人実績6,150件 個人実績10,600件 相続実績1,080件 弊社はお陰様で信頼と実績があります。
税理士法人KOSUGEは東池袋、池袋、豊島区、北区、練馬区、埼玉県を中心に活動しております。相続、その他税務に関する相談やそして会社起業などのお手伝いも積極的に行っています。池袋 税理士
池袋にて創業45年。法人実績6,150件 個人実績10,600件 相続実績1,080件 弊社はお陰様で信頼と実績があります。
事務所名 | 税理士法人KOSUGE |
所在地 | 〒170-0013 東京都豊島区東池袋1-25-2 朝日生命池袋ビル2F |
電話番号 | 03-3984-5547 |
FAX番号 | 03-3590-8004 |
業務内容 |
|
ここに文章を入力<!DOCTYPE html>
<html>
<head>
<title>ブロック崩し</title>
<style>
#canvas {
border: solid 1px black;
}
</style>
</head>
<body>
<canvas id="canvas" width="480"
height="320"></canvas>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// パドルの設定
const paddleWidth = 75;
const paddleHeight = 10;
let paddleX = (canvas.width - paddleWidth) / 2;
// ボールの設定
let ballX = canvas.width / 2;
let ballY = canvas.height - 30;
let ballRadius = 10;
let dx = 2;
let dy = -2;
// ブロックの設定
const brickRowCount = 3;
const brickColumnCount = 5;
const brickWidth = 75;
const brickHeight = 20;
const brickPadding = 10;
const brickOffsetTop = 30;
const brickOffsetLeft = 30;
const bricks = [];
for(let c = 0; c < brickColumnCount; c++) {
bricks[c] = [];
for(let r = 0; r < brickRowCount; r++) {
bricks[c][r] = { x: 0, y: 0, status: 1 };
}
}
// スコアの設定
let score = 0;
// パドルの描画
function drawPaddle() {
ctx.beginPath();
ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth,
paddleHeight);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
// ボールの描画
function drawBall() {
ctx.beginPath();
ctx.arc(ballX, ballY, ballRadius, 0, Math.PI*2);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
// ブロックの描画
function drawBricks() {
for(let c = 0; c < brickColumnCount; c++) {
for(let r = 0; r < brickRowCount; r++) {
if(bricks[c][r].status == 1) {
let brickX = (c * (brickWidth + brickPadding)) + brickOffsetLeft;
let 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 = "#0095DD";
ctx.fill();
ctx.closePath();
}
}
}
}
// スコアの描画
function drawScore() {
ctx.font = "16px Arial";
ctx.fillStyle = "#0095DD";
ctx.fillText("Score: " + score, 8, 20}
// 衝突判定
function collisionDetection() {
for(let c = 0; c < brickColumnCount; c++) {
for(let r = 0; r < brickRowCount; r++) {
let b = bricks[c][r];
if(b.status == 1) {
if(ballX > b.x && ballX < b.x + brickWidth &&
ballY > b.y && ballY < b.y + brickHeight) {
dy = -dy;
b.status = 0;
score++;
if(score == brickRowCount * brickColumnCount) {
alert("Congratulations! You cleared the game!");
document.location.reload();
}
}
}
}
}
}
// ゲームオーバーの描画
function drawGameOver() {
ctx.font = "30px Arial";
ctx.fillStyle = "red";
ctx.fillText("Game Over", canvas.width/2 - 80,
canvas.height/2);
}
// ゲームループ
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPaddle();
drawBall();
drawBricks();
drawScore();
collisionDetection();
if(ballX + dx > canvas.width - ballRadius || ballX + dx <
ballRadius) {
dx = -dx;
}
if(ballY + dy < ballRadius) {
dy = -dy;
}
else if(ballY + dy > canvas.height - ballRadius) {
if(ballX > paddleX && ballX < paddleX + paddleWidth) {
dy = -dy;
}
else {
drawGameOver();
clearInterval(interval);
}
}
if(rightPressed && paddleX < canvas.width - paddleWidth) {
paddleX += 7;
}
else if(leftPressed && paddleX > 0) {
paddleX -= 7;
}
ballX += dx;
ballY += dy;
}
// イベントハンドラの設定
let rightPressed = false;
let leftPressed = false;
document.addEventListener("keydown",
keyDownHandler, false);
document.addEventListener("keyup",
keyUpHandler, false);
function keyDownHandler(e) {
if(e.key == "Right" || e.key == "ArrowRight") {
rightPressed = true;
}
else if(e.key == "Left" || e.key == "ArrowLeft") {
leftPressed = true;
}
}
function keyUpHandler(e) {
if(e.key == "Right" || e.key == "ArrowRight") {
rightPressed = false;
}
else if(e.key == "Left" || e.key == "ArrowLeft") {
leftPressed = false;
}
}
// ゲームの開始
let interval = setInterval(draw, 10);してください