简体   繁体   English

创建可点击的网格方块

[英]Creating Clickable grid squares

I would like some help regarding a "battleship" sort of game that I am creating.我想要一些关于我正在创建的“战舰”类游戏的帮助。 I have a 7x7 grid of white squares and I have put it under the click code so that when I click on the squares, a picture appears.我有一个 7x7 的白色方块网格,我把它放在点击代码下,这样当我点击方块时,就会出现一张图片。 I have done that using if statements eg.我已经使用 if 语句完成了,例如。 if(mx>xxx && mx<xxx && my>xxx &my<xxx) etc. The main problem is occurring here: if(mx>xxx && mx<xxx && my>xxx &my<xxx)等主要问题发生在这里:

I am trying to add other white squares to go on top of the grid so that when I click on those, my score variable which is recorded on the canvas goes up.我正在尝试将其他白色方块添加到网格顶部,这样当我点击它们时,我记录在画布上的分数变量就会上升。 So when I click on one square that has RANDOMLY been placed on top of the other grid squares, the score will go up by 10 points or however much its worth.因此,当我单击一个随机放置在其他网格方块顶部的方块时,分数将上升 10 分或无论其价值多少。 As soon as I click on it, the score will go up and then my battleship will appear, as if it had been clicked on.我一点击它,分数就会上升,然后我的战舰就会出现,就像它被点击一样。 I am wondering how to do this with 4 different battleships which use use on one square from the grid to appear in.我想知道如何使用 4 艘不同的战舰来做到这一点,这些战舰使用网格中的一个方格出现。

Also, I am wondering how to end the game once my "ducks" -- which i have subbed in for the battleships -- are all found...另外,我想知道一旦我的“鸭子”——我为战列舰替换的——都被找到了,如何结束游戏......

Heres my idea:这是我的想法:

while(ducks == not found){

    REDO GAME   
}

if(ducks == found){

    display(“Congratulations”,100,100,100,100)
    ctx.drawImage(rain, 0,0,0,0)
}

Please help!请帮忙! Thank you in advance!先感谢您!

I suggest to create a separate variable game_state holding the current game state.我建议创建一个单独的变量game_state 来保存当前的游戏状态。

This game_state can be for example an integer with three values:例如,这个game_state可以是一个具有三个值的整数:

0 - setting up; 0 - 设置; 1 - in play; 1 - 在玩; 2 - finished 2 - 完成

Or anything you find usable.或者任何你觉得有用的东西。 Later you can look up & change integer to enum.稍后您可以查找并将整数更改为枚举。

Check for the win condition at the end of processing a click.在处理点击结束时检查获胜条件。 If the condition is satisfied, change the state to 'finished', present the win message.如果满足条件,则将状态更改为“已完成”,并显示获胜消息。

You can check for state 'in play' & skip processing any clicks until state of the game has changed again to 'in play', which can be done for example via some other button with 'New Game'.您可以检查状态“进行中”并跳过处理任何点击,直到游戏状态再次更改为“进行中”,这可以通过例如其他带有“新游戏”的按钮来完成。

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM