randomly mapping divs

后端 未结 4 1241
闹比i
闹比i 2021-02-19 21:11

I am creating a new \"whack-a-mole\" style game where the children have to hit the correct numbers in accordance to the question. So far it is going really well, I have a timer,

4条回答
  •  滥情空心
    2021-02-19 21:27

    As @aug suggests, you should know where you cannot place things at draw-time, and only place them at valid positions. The easiest way to do this is to keep currently-occupied positions handy to check them against proposed locations.

    I suggest something like

    // locations of current divs; elements like {x: 10, y: 40}
    var boxes = [];
    
    // p point; b box top-left corner; w and h width and height
    function inside(p, w, h, b) {
         return (p.x >= b.x) && (p.y >= b.y) && (p.x < b.x + w) && (p.y < b.y + h);
    }
    
    // a and b box top-left corners; w and h width and height; m is margin
    function overlaps(a, b, w, h, m) {
         var corners = [a, {x:a.x+w, y:a.y}, {x:a.x, y:a.y+h}, {x:a.x+w, y:a.y+h}];
         var bWithMargins = {x:b.x-m, y:b.y-m};
         for (var i=0; i

    Warning: untested code, beware the typos.

提交回复
热议问题