Pygame 低帧率但立即等待输入
Pygame low frame rate but waiting on input instantly
我想用 pygame 在 python 中创建简单的游戏。有一些按钮,但有时没有反应。我想要低帧率,因为它的游戏很简单,但我也想让按钮像往常一样等待点击。可能吗?
好像如果我在那 60/1000 毫秒内不单击它就不起作用,对吗?如果我是,那么还有其他可能性 运行 以 60 fps 的速度玩游戏并像往常一样等待输入吗?
用它来捕捉点击:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680:
button_hod_clicked = True
在主循环中使用它:
clock.tick(FPS)
结构:
def check_buttons(pos):
button_hod_clicked = False
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680:
button_hod_clicked = True
return button_hod_clicked
def main():
run = True
striedanie = True
while run:
clock.tick(FPS)
pos = pygame.mouse.get_pos()
button_hod_clicked = check_buttons(pos)
if button_hod_clicked:
if striedanie == True:
player1_position, kolko_hodil = player_movement(player1_position)
striedanie = False
else:
player2_position, kolko_hodil = player_movement(player2_position)
striedanie = True
time.sleep(1)
不是每帧都获取鼠标位置,如果事件处理程序检测到 MOUSEBUTTONUP
并调用 check_buttons
函数,则只获取鼠标位置。
def check_buttons(pos):
return pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680
button_hod_clicked = False
def main():
global button_hod_clicked
...
...
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
button_hod_clicked = check_buttons(pos)
if button_hod_clicked:
...
让我知道这是否有效。
注意:time.sleep
暂停了整个程序,因此它冻结了 window。
我想用 pygame 在 python 中创建简单的游戏。有一些按钮,但有时没有反应。我想要低帧率,因为它的游戏很简单,但我也想让按钮像往常一样等待点击。可能吗?
好像如果我在那 60/1000 毫秒内不单击它就不起作用,对吗?如果我是,那么还有其他可能性 运行 以 60 fps 的速度玩游戏并像往常一样等待输入吗?
用它来捕捉点击:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680:
button_hod_clicked = True
在主循环中使用它:
clock.tick(FPS)
结构:
def check_buttons(pos):
button_hod_clicked = False
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680:
button_hod_clicked = True
return button_hod_clicked
def main():
run = True
striedanie = True
while run:
clock.tick(FPS)
pos = pygame.mouse.get_pos()
button_hod_clicked = check_buttons(pos)
if button_hod_clicked:
if striedanie == True:
player1_position, kolko_hodil = player_movement(player1_position)
striedanie = False
else:
player2_position, kolko_hodil = player_movement(player2_position)
striedanie = True
time.sleep(1)
不是每帧都获取鼠标位置,如果事件处理程序检测到 MOUSEBUTTONUP
并调用 check_buttons
函数,则只获取鼠标位置。
def check_buttons(pos):
return pos[0] > 500 and pos[0] < 600 and pos[1] > 620 and pos[1] < 680
button_hod_clicked = False
def main():
global button_hod_clicked
...
...
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
button_hod_clicked = check_buttons(pos)
if button_hod_clicked:
...
让我知道这是否有效。
注意:time.sleep
暂停了整个程序,因此它冻结了 window。