当我在窗口上移动光标时,我的pygame窗口仅更新

发布于 2025-01-19 09:26:39 字数 669 浏览 0 评论 0原文

仅当我的光标移动且位于窗口上方时,我的 Pygame 窗口才会更新。

import pygame

window_length = 1000
window_height = 600
dimensions = (window_length, window_height)
window = pygame.display.set_mode(dimensions)

WHITE = (255, 255, 255)
BLACK = (0,0,0)

main = True

x = 0
y = 0

while main:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            main = False
            
        x += 1
            
        #redraw background
        pygame.draw.rect(window, BLACK, (0,0, window_length, window_height))
        pygame.draw.rect(window, WHITE, (x, y, 100, 100))
        pygame.display.update()

pygame.quit()

我尝试移动东西但没有任何效果。

My Pygame window will only update if my cursor is moving and is above the window.

import pygame

window_length = 1000
window_height = 600
dimensions = (window_length, window_height)
window = pygame.display.set_mode(dimensions)

WHITE = (255, 255, 255)
BLACK = (0,0,0)

main = True

x = 0
y = 0

while main:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            main = False
            
        x += 1
            
        #redraw background
        pygame.draw.rect(window, BLACK, (0,0, window_length, window_height))
        pygame.draw.rect(window, WHITE, (x, y, 100, 100))
        pygame.display.update()

pygame.quit()

I tried moving things around but nothing worked.

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

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

发布评论

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

评论(1

新一帅帅 2025-01-26 09:26:39

取消缩进事件循环下的代码。现在,您仅在有事件需要循环时才执行绘图代码,例如鼠标移动。

它应该看起来像:

while main:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            main = False

    x += 1

    #redraw background
    pygame.draw.rect(window, BLACK, (0,0, window_length, window_height))
    pygame.draw.rect(window, WHITE, (x, y, 100, 100))
    pygame.display.update()

Unindent the code under the event loop. Right now, you're only doing the drawing code when there are events to loops through, like your mouse moving.

It should look like:

while main:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            main = False

    x += 1

    #redraw background
    pygame.draw.rect(window, BLACK, (0,0, window_length, window_height))
    pygame.draw.rect(window, WHITE, (x, y, 100, 100))
    pygame.display.update()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文