Skip to content

Commit

Permalink
make Rectangle and move with arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
fpdjsns committed Jul 23, 2018
1 parent 26a4c6f commit 8ead63a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
Binary file added Document/~$Tetris plan.pptx
Binary file not shown.
Empty file added Tetris/css/main.css
Empty file.
62 changes: 62 additions & 0 deletions Tetris/js/main.js
@@ -0,0 +1,62 @@
var blockType = [
{name : 'I', color : 'red'},
{name : 'L', color : 'green'},
{name : 'J', color : 'blue'},
{name : 'T', color : 'yellow'},
{name : 'O', color : 'skyblue'},
{name : 'S', color : 'gray'},
{name : 'Z', color : 'purple'},
]

var nowBlock;
var canvas = document.getElementById('game');

const BEGIN_X = 30;
const BEGIN_Y = 30;
const SPEED = 2;

if(canvas.getContext){
var ctx = canvas.getContext('2d');
}else{
console.log('browser not supported canvas');
}

$(window).load(function(){
console.log('load');
drawNewBlock(2);
});

var drawNewBlock = function(blockTypeIndex){
nowBlock = new Block(blockTypeIndex, BEGIN_X, BEGIN_Y);
nowBlock.drawBlock(BEGIN_X, BEGIN_Y);
}

$(document).keydown(function(e){
if (e.keyCode == 37) {
console.log("left");
nowBlock.drawBlock(nowBlock.x-SPEED, nowBlock.y);
} else if(e.keyCode == 38) {
console.log("up");
nowBlock.drawBlock(nowBlock.x, nowBlock.y-SPEED);
} else if(e.keyCode == 39) {
console.log("right");
nowBlock.drawBlock(nowBlock.x+SPEED, nowBlock.y);
} else if(e.keyCode == 40) {
console.log("down");
nowBlock.drawBlock(nowBlock.x, nowBlock.y+SPEED);
}
});

function Block(blockTypeIndex, x, y){
this.type = blockType[blockTypeIndex];
this.x = x;
this.y = y;
ctx.fillStyle = this.type.color;

this.drawBlock = function(nx, ny){
ctx.clearRect(this.x, this.y, 50, 50);
this.x = nx;
this.y = ny;
ctx.fillRect(nx, ny, 50, 50);
}
}
16 changes: 16 additions & 0 deletions Tetris/main.html
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Tetris</title>
</head>
<body>
<div id="main">
<canvas id="game" width="150" height="150"></canvas>
</div>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/stylesheet" src="css/main.css"></script>
</body>
</html>

0 comments on commit 8ead63a

Please sign in to comment.