You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame开发谷歌断网恐龙游戏:鼠标静止时帧率骤降为0求助

Pygame鼠标静止时帧率降为0的解决方案

我是Pygame新手,正在复刻谷歌断网时的恐龙小游戏。发现当鼠标在动画窗口内停止移动时,帧率会降为0,网上没找到相关解决方案,希望实现无论鼠标是否移动,游戏均正常运行。代码如下:

import pygame
from sys import exit


pygame.init()
pygame.display.set_caption("Runner")
clock = pygame.time.Clock()
display_surface = pygame.display.set_mode((800, 400))

#data values
score = 0
x_bound = 800
y_bound = 400
floor_y = 300
speed = 10
player_gravity = 0

test_font = pygame.font.Font('font/Pixeltype.ttf', 50)
score_surface = test_font.render(f'Score: {score}', False, (64, 64, 64))

sky_surface = pygame.image.load('graphics/Sky.png').convert()
ground_surface = pygame.image.load('graphics/ground.png').convert()
snail_surface = pygame.image.load('graphics/snail/snail1.png').convert_alpha()
player_surface = pygame.image.load('graphics/player/player_walk_1.png').convert_alpha()

#initializing rectangles
player_surface_rect = player_surface.get_rect(midbottom=(80, 300))
snail_surface_rect = snail_surface.get_rect(midbottom=(600, 300))
score_surface_rect = score_surface.get_rect(midtop=(400, 50))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.KEYDOWN:
            if event.type == 768: #768 --> spacebar
                player_gravity = -15


        display_surface.blit(sky_surface, (0, 0))
        display_surface.blit(ground_surface, (0, 300))

        #score 
        pygame.draw.rect(display_surface, '#c0e8ec', score_surface_rect)
        pygame.draw.rect(display_surface, '#c0e8ec', score_surface_rect, 5)
        display_surface.blit(score_surface, score_surface_rect)

        #snail
        snail_surface_rect.x -= speed
        if snail_surface_rect.right <= 0:
            snail_surface_rect.left = x_bound
        display_surface.blit(snail_surface, snail_surface_rect)

        #player
        #jump
        player_gravity += 1
        player_surface_rect.y += player_gravity

        #creating floor
        if player_surface_rect.bottom > floor_y:
            player_surface_rect.bottom = floor_y

        display_surface.blit(player_surface, player_surface_rect)

        
        pygame.display.update()
        clock.tick(60)

我是跟着Clear Code的Pygame教程进行开发的。


问题根源

你把所有游戏渲染、逻辑代码都放在了**事件循环(for event in pygame.event.get())**内部。只有当有事件触发(比如鼠标移动、按键按下)时,这些代码才会执行;鼠标静止时没有事件产生,代码就不运行,导致帧率直接降到0。

另外还有一个按键判断的错误:KEYDOWN事件里,判断按键应该用event.key,而不是event.type,你写的event.type ==768是错误的,正确的空格按键判断应该是event.key == pygame.K_SPACE。

修改后的代码

import pygame
from sys import exit


pygame.init()
pygame.display.set_caption("Runner")
clock = pygame.time.Clock()
display_surface = pygame.display.set_mode((800, 400))

#data values
score = 0
x_bound = 800
y_bound = 400
floor_y = 300
speed = 10
player_gravity = 0

test_font = pygame.font.Font('font/Pixeltype.ttf', 50)
score_surface = test_font.render(f'Score: {score}', False, (64, 64, 64))

sky_surface = pygame.image.load('graphics/Sky.png').convert()
ground_surface = pygame.image.load('graphics/ground.png').convert()
snail_surface = pygame.image.load('graphics/snail/snail1.png').convert_alpha()
player_surface = pygame.image.load('graphics/player/player_walk_1.png').convert_alpha()

#initializing rectangles
player_surface_rect = player_surface.get_rect(midbottom=(80, 300))
snail_surface_rect = snail_surface.get_rect(midbottom=(600, 300))
score_surface_rect = score_surface.get_rect(midtop=(400, 50))

while True:
    # 事件处理单独放在循环里
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.KEYDOWN:
            # 修正按键判断逻辑
            if event.key == pygame.K_SPACE:
                player_gravity = -15

    # 所有渲染、逻辑代码移出事件循环,放到主while循环中
    display_surface.blit(sky_surface, (0, 0))
    display_surface.blit(ground_surface, (0, 300))

    #score 
    pygame.draw.rect(display_surface, '#c0e8ec', score_surface_rect)
    pygame.draw.rect(display_surface, '#c0e8ec', score_surface_rect, 5)
    display_surface.blit(score_surface, score_surface_rect)

    #snail
    snail_surface_rect.x -= speed
    if snail_surface_rect.right <= 0:
        snail_surface_rect.left = x_bound
    display_surface.blit(snail_surface, snail_surface_rect)

    #player
    #jump
    player_gravity += 1
    player_surface_rect.y += player_gravity

    #creating floor
    if player_surface_rect.bottom > floor_y:
        player_surface_rect.bottom = floor_y

    display_surface.blit(player_surface, player_surface_rect)

    pygame.display.update()
    clock.tick(60)

说明

修改后,事件处理只负责响应用户输入(关闭窗口、按键),而游戏的渲染、角色移动、重力计算等逻辑会在主循环的每一次迭代中执行,不管有没有事件触发,这样就能保证游戏始终以60帧的速度运行,不受鼠标移动状态影响。

内容的提问来源于stack exchange,提问作者Vrishank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 11:50:55