python - 未绘制矩形

标签 python pygame drawrectangle

运行此代码时:

import pygame,time

GREEN = (30, 156, 38)
WHITE = (255,255,255)

pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))
time.sleep(3)

Pygame 显示黑屏 3 秒,但不绘制矩形。 我正在使用 Atom 使用 script 包来运行代码

最佳答案

您必须实现一个应用程序循环。典型的 PyGame 应用程序循环必须:

import pygame

GREEN = (30, 156, 38)
WHITE = (255,255,255)

pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()

# applicaition loop
run = True
while run:
    #  limit frames per second
    clock.tick(60)

    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    # clear display
    screen.fill(WHITE)

    # draw objects
    pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))

    # update display
    pygame.display.flip()

pygame.quit()
exit()

注意,您必须进行事件处理。请参阅pygame.event.get()分别pygame.event.pump() :

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

关于python - 未绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66891768/

相关文章:

python - 如何将列表拆分为由分隔符确定的 block ?

python - pygame矩形碰撞小于图像

python - 行走动画速度

python - 按钮释放 Tkinter

c# - System.Drawing.Graphics,绘制表格?

python - 剥离 pandas 数据帧索引 '\n' 和空格

python - Pandas to_xml() 设置 xsi 值

python - Airflow BashOperator 找不到 Bash

c# - 只绘制矩形的角