HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> <style type="text/css"> body { background: #202028; color: #fff; font-family: sans-serif; font-size: 2em; text-align: center; } cavas{ border: solid .2em #fff; height: 90vh; } </style> </head> <body> <div id="score"></div> <canvas id="tetris" width="240" height="400"></canvas> <script src="tetris1.js"></script> </body> </html> | cs |
TETRIS1.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | const canvas = document.getElementById('tetris'); const context = canvas.getContext('2d'); context.scale(20,20); function arenaSweap(){ let rowCount = 1; outer: for(let y = arena.length - 1; y > 0; --y){ for(let x = 0; x< arena[y].length; ++x){ if(arena[y][x] === 0){ continue outer; } } const row = arena.splice(y,1)[0].fill(0); arena.unshift(row); ++y; player.score += rowCount * 10; rowCount *= 2; } } function collide(arena, player){ ///collide : 충돌하다. 쌓는 부분, createMatrix와는 어떠한 차이점이 있는지 알아보자 const [m, o] = [player.matrix, player.pos]; for(let y = 0; y < m.length; ++y){ for(let x = 0; x<m[y].length; ++x){ if(m[y][x] !== 0 && (arena[y+ o.y]&& arena[y+ o.y][x +o.x]) !== 0){ return true; } } } return false; } function createMatrix(w,h){ //width, height [ we save all the stock pieces so in order to facilitate this. const matrix =[]; while (h--){ matrix.push(new Array(w).fill(0)); } return matrix; } function createPiece(type){ if(type === 'T'){ return [ [0, 0, 0], [1, 1, 1], [0, 1, 0], ]; } else if(type === 'O'){ return [ [2, 2], [2, 2], ]; }else if(type === 'L'){ return [ [0, 3, 0], [0, 3, 0], [0, 3, 3], ]; }else if(type === 'J'){ return [ [0, 4, 0], [0, 4, 0], [4, 4, 0], ]; }else if(type === 'I'){ return [ [0, 5, 0, 0], [0, 5, 0, 0], [0, 5, 0, 0], [0, 5, 0, 0], ]; }else if(type === 'S'){ return [ [0, 6, 6], [6, 6, 0], [0, 0, 0] ]; }else if(type === 'Z'){ return [ [7, 7, 0], [0, 7, 7], [0, 0, 0], ]; } } //matrix 모양을 입힘. function draw(){ context.fillStyle = '#000'; context.fillRect(0, 0, canvas.width, canvas.height); drawMatrix(arena, {x: 0, y: 0}); drawMatrix(player.matrix, player.pos); } function drawMatrix(matrix, offset){ matrix.forEach((row, y) => { row.forEach((value, x) => { if(value !== 0){ context.fillStyle = colors[value]; context.fillRect(x + offset.x, y + offset.y, 1, 1); } }); }); } function merge(arena, player){ player.matrix.forEach((row,y) => { row.forEach((value, x) =>{ if(value !== 0){ arena[y + player.pos.y][x + player.pos.x] = value; } }); }); } function playerDrop(){ player.pos.y++; //function collide() - ++가 다 되면 0으로 y값을 바꿔 줌 *** if(collide(arena, player)){ player.pos.y--; merge(arena,player); playerReset(); arenaSweap(); updateScore(); } dropCounter = 0; } function playerMove(dir){ player.pos.x += dir; if(collide(arena, player)){ player.pos.x -= dir; } } function playerReset(){ const pieces = 'ILJOTSZ'; player.matrix = createPiece(pieces[pieces.length * Math.random() | 0]); player.pos.y = 0; player.pos.x = (arena[0].length / 2 | 0)- (player.matrix[0].length /2 | 0); if (collide(arena, player)){ arena.forEach(row => row.fill(0)); player.score = 0; updateScore(); } } function playerRotate(dir){ const pos = player.pos.x; let offset = 1; rotate(player.matrix, dir); while(collide(arena, player)){ player.pos.x += offset; offset = -(offset + (offset > 0 ? 1 : -1)); if (offset > player.matrix[0].length){ rotate(player.matrix, -dir); player.pos.x = pos; return; } } } function rotate(matrix, dir){ for(let y = 0; y<matrix.length; ++y){ for(let x = 0; x < y; ++x){ [ matrix[x][y], matrix[y][x] ] =[ matrix[y][x], matrix[x][y], ]; } } if(dir > 0){ matrix.forEach(row => row.reverse()); } else { matrix.reverse(); } } let dropCounter = 0; let dropInterval = 1000; let lastTime = 0; function updateScore(){ document.getElementById('score').innerText = player.score; } function update(time = 0){//time.. 떨어지는 것 설정 const deltaTime = time - lastTime; lastTime = time; dropCounter += deltaTime; if(dropCounter > dropInterval){ playerDrop(); /*player.pos.y++; dropCounter = 0;*/ } draw(); requestAnimationFrame(update); } const colors = [ null, 'red', 'blue', 'violet', 'green', 'purple', 'orange', 'pink', ] const arena = createMatrix(12, 20); console.log(arena); //console.table(arena);// 테이블 안됨 const player ={ pos: {x: 0, y: 0}, matrix: null, score: 0, } document.addEventListener('keydown', event =>{ //키다운 이벤트 인식 console.log(event); if(event.keyCode === 37){ playerMove(-1); // player.pos.x--; }else if(event.keyCode === 39){ playerMove(1); // player.pos.x++; }else if(event.keyCode === 40){ playerDrop(); }else if(event.keyCode === 81 ){ playerRotate( 1); } }); playerReset(); updateScore(); update(); | cs |
https://www.youtube.com/watch?v=H2aW5V46khA&t=1287s
반응형