Javascript:3次尝试游戏逻辑

发布于 2024-12-10 15:04:50 字数 539 浏览 0 评论 0原文

我正在构建一个 javascript+css 游戏,它显示 3 个心形图标,代表游戏允许用户进行的 3 次尝试。

我有 3 种类型的心:

正常的心代表未使用的尝试

突出显示的心代表成功的尝试

交叉的心代表失败的尝试

另外我还有一个名为“结果”的变量,用于存储 0(尝试失败)或 1(尝试成功) )

你能帮我描述一下 3 次尝试的逻辑吗?这意味着根据用户操作改变屏幕上的心形图标。 我首先展示了游戏开始时必须展示的 3 个正常的红心,但我不知道如何遵循,我被卡住了!

组合可以是(O 是正常心脏,V 是成功尝试心脏,X 是失败心脏):

OOO

XOO

VOO

XXO

VXO

XVO

VVO

XXV

XVX

VXV

VXX

VVX

XVV

XXX

VVV

感谢一百万

I'm building a javascript+css game that shows 3 heart icons representing the 3 attempts the game allows the user to have.

I have 3 types of hearts:

a normal one representing a not used attempt

a highlighted one representing a successful attempt

a crossed one representing a failed attempt

Also I have a variable called "result" that stores 0 (attempt failed) or 1 (attempt successful)

Could you please help me with representing the 3 attempts logic? Meaning changing the heart icons on the screen depending on the user actions.
I've started by showing the 3 normal hearts that have to show on game start but I don't know how to follow, I'm stuck!

The combinations can be (being O a normal heart, V a successful attempt heart and X a failed one):

OOO

XOO

VOO

XXO

VXO

XVO

VVO

XXV

XVX

VXV

VXX

VVX

XVV

XXX

VVV

Thanks a million

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2024-12-17 15:04:50

因此,在任何时候,您都可以使用 3 个数字来表示红心的状态,其中每个数字可以是 1,2 或 3。假设 1 代表正常的红心,2 代表突出显示的红心,3 代表十字红心。

所以你可以将心的状态存储为整数。在游戏开始时,所有红心都是普通红心,因此:

var state = new Array();
state[0] = 1
state[1] = 1
state[2] = 1

现在,只需使用另一个变量(例如 attemptNumber)来跟踪用户正在进行的尝试。因此,在游戏开始时,attemptNumber=1。用户完成该尝试后,如果尝试成功,只需将 state[attemptNumber] 设置为 2,如果失败,则设置为 3,然后将 attemptsNumber 加 1。现在,用户仅获得 3尝试,第三次尝试后,attemptNumber 等于 4。因此,只需将整个游戏放入 while 循环中,如 while (attemptNumber < 4)(玩游戏)。

至于 CSS,根据 state 的值更改每颗心的图标。

So at any point, you can represent the state of the hearts using 3 numbers, where each number can be a 1,2 or 3. Lets say 1 represents a normal heart, 2 represents a highlighted heart, and 3 represents a crossed heart.

So you can store the state of the hearts as an integer. At the start of the game, all of the hearts are normal hearts, so:

var state = new Array();
state[0] = 1
state[1] = 1
state[2] = 1

Now, just keep track of which attempt the user is on using another variable, say attemptNumber. So, at the start of the game, attemptNumber=1. After the user has finished that attempt, just set state[attemptNumber] equal to 2 if the attempt was successful, or 3 if it failed, and then increment attemptNumber by 1. Now, the user only gets 3 attempts, and after the third attempt, attemptNumber is equal to 4. So just put the whole game in a while loop like while (attemptNumber < 4) (play the game).

As for the CSS, change the icon for each heart depending on the value of state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文