鼠标上/下之间的秒表

时间:2019-05-09 15:10:56

标签: python pygame pygame-clock

我正在尝试通过while循环中的简单秒表来测试鼠标按下和鼠标按下事件之间的时间。鼠标按下事件工作正常,但是当我释放鼠标以进行鼠标按下时,秒数继续增加并且没有停止。

from pygame import *
import time
screen = display.set_mode((160, 90))
sec = 0
while True:
    new_event = event.poll()
    if new_event.type == MOUSEBUTTONDOWN:
        while True: # Basic stopwatch started
            time.sleep(1)
            sec += 1
            print(sec)
            # In loop, when mouse button released,
            # supposed to end stopwatch
            if new_event.type == MOUSEBUTTONUP:
                break
    display.update()

我希望秒表在释放鼠标后结束。例如。如果仅单击鼠标,则秒数应为1。如果按住鼠标5秒钟,则不应超过5。

1 个答案:

答案 0 :(得分:1)

使用pygame.time.get_ticks获取自Add-Type -AssemblyName System.Collections $dt = New-Object system.Data.DataTable [void]$dt.Columns.Add('kind',[string]::empty.GetType() ) [void]$dt.Columns.Add('name',[string]::empty.GetType() ) [void]$dt.Columns.Add('value',[string]::empty.GetType() ) # Add new rows: $newRow = $dt.NewRow() $newRow.kind = 'status' $newRow.name = 'Selected Operation(s):' $newRow.value = 'None' [void]$dt.Rows.Add( $newRow ) $newRow = $dt.NewRow() $newRow.kind = 'status' $newRow.name = 'Selected' $newRow.value = 'None' [void]$dt.Rows.Add( $newRow ) # Find row(s): $rows = $dt.Select("kind = 'status'") "Found:" $dt # Change first row by condition: $rows = $dt.Select("kind = 'status'") $rows[0].value = 'test' [void]$dt.AcceptChanges() "Changed one row:" $dt # Change all rows: $rows = $dt.Select("") $rows | % { $_.value = 'new' } [void]$dt.AcceptChanges() "Changed all:" $dt # Change all rows by condition: $rows = $dt.Select("name = 'Selected'") $rows | % { $_.value = 'newer' } [void]$dt.AcceptChanges() "Changed by condition:" $dt 被调用以来的毫秒数。
存储pygame.init()时的毫秒数,并在主循环中计算时间差:

MOUSEBUTTONDOWN