JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas" height="400" width="600" style="background: #333;"></canvas>
JavaScript
var game = {
canvas: document.getElementById("canvas"),
ctx: this.canvas.getContext("2d"),
cellCount: 9,
cellWidth: 30,
lineCount: 5,
mode: 7,
actions: {},
play: function(name, action, interval) {
var me = this;
this.actions[name] = setInterval(function() {
action();
me.draw();
}, interval || 1);
},
stop: function(name) {
clearInterval(this.actions[name]);
this.draw();
},
colors: ["red", "#039518", "#ff00dc", "#ff6a00", "gray", "#0094ff", "#d2ce00"],
start: function() {
this.map.init();
this.ready.init();
this.draw();
this.canvas.onclick = this.onclick;
},
over: function() {
alert("GAME OVER");
},
draw: function() {
this.ctx.clearRect(0, 0, 400, 600);
this.ctx.save();
this.map.draw();
this.ready.draw();
this.ctx.restore();
},
clicked: null,
onclick: function(e) {
var px = e.offsetX - game.map.startX;
var py = e.offsetY - game.map.startY;
if (px < 0 || py < 0 || px > game.map.width || py > game.map.height) {
return;
}
var x = parseInt(px / game.cellWidth);
var y = parseInt(py / game.cellWidth);
var bubble = game.map.getBubble(x, y);
if (bubble.color) {
if (this.clicked) {
//同一个泡不做反映
if (this.clicked.x == x && this.clicked.y == y) {
return;
}
this.clicked.stop();
}
this.clicked = bubble;
bubble.play();
}
else {
if (this.clicked) {
this.clicked.stop();
//移动clicked
game.map.move(this.clicked, bubble);
game.ready.flyin();
}
}
//console.log("x:" + x + " y:" + y);
},
getRandom: function(max) {
return...