gpt4 book ai didi

python - 如何在达到特定情况时停止pygame中的计时器

转载 作者:太空宇宙 更新时间:2023-11-04 09:22:59 24 4
gpt4 key购买 nike

您好,我现在正在为内存游戏(又名纸牌匹配游戏)编写代码,我在 pygame 中使用计时器来告诉玩家花了多长时间才能揭开每张纸牌(完全匹配纸牌组中的每一对纸牌) ).

self.score = pygame.time.get_ticks() // 1000

这是我写的用来检查时间的,我想在所有卡片都被揭开后停止有什么办法可以让我停止吗?我进行了一些搜索,但没有找到适合我现在需要的东西。

https://www.youtube.com/watch?v=9-6iDRyPoVg&feature=youtu.be这就是它应该的样子。

谢谢!

最佳答案

函数 pygame.time.get_ticks() 返回调用 pygame.init() 后的毫秒数。

我认为更好的方法是记住第一次卡片点击的时间,然后将其与最后卡片点击的时间进行比较。

例如:

### Card was clicked
if ( game_start_time == 0 ):
game_start_time = pygame.time.get_ticks() # game start time

...


if ( cards_left_covered == 0 ):
# calculate the elapsed time
game_end_time = pygame.time.get_ticks()
self.score = ( game_end_time - game_start_time ) // 1000 # seconds

关于python - 如何在达到特定情况时停止pygame中的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59148966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com