Your code is very disgusting, but a quick fix is adding
x -= 125
y -= 33
at the beginning of your Game.reveal function.
Although, I highly suggest rewritting your game.
Hardcoded values are bad.
[QUOTE=PigeonPatrol;49034981]Your code is very disgusting, but a quick fix is adding
x -= 125
y -= 33
at the beginning of your Game.reveal function.
Although, I highly suggest rewritting your game.
Hardcoded values are bad.[/QUOTE]
I don't know what im doing wrong :/
The problem with your code is all those if statements and redundancy (what do all these magic numbers represent).
[QUOTE=BOT Ferris;49035173]I don't know what im doing wrong :/[/QUOTE]
several things
1. that giant if-block is an abomination
2. you have to offset the mouse position to align with the grid, by adding the difference between the topleft of the page (which is 0, 0) and the topleft of the canvas
3. you had a == instead of =
4. having a constant variable containing a number that is often repeated in code (like 128) is a good idea and improves readibility
if you ever find yourself copy and pasting the same code over and over with only small differences, look for patterns. code should never have to be ugly.
[code]
const GRID_SIZE = 128;
Game.reveal = function(x, y) {
var gridX = Math.floor(x / GRID_SIZE);
var gridY = Math.floor(y / GRID_SIZE);
var i = (gridY * 5) + gridX;
if(blocks[i] == 2) {
blocks[i] = 0;
} else if(blocks[i] == 3) {
blocks[i] = 1;
}
}
canvas.addEventListener('mousedown', function(e) {
console.log("Pressed");
Game.reveal(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
});[/code]
Sorry, you need to Log In to post a reply to this thread.