简体   繁体   English

我的 Pygame window 只有在我在 window 上移动我的 cursor 时才会更新

[英]My Pygame window only updates when i am moving my cursor around on the window

My Pygame window will only update if my cursor is moving and is above the window.我的 Pygame window 只有在我的 cursor 正在移动并且高于 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.我尝试四处移动东西,但没有任何效果。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM