在 Pygame 中画一条线

J.G*_*Gor 6 python pygame

我想用 Python 画一条线,但是当我运行下面的代码时,这条线永远不会出现。事实上,我需要用 4x4 部分制作一个字段,但让我们从一行开始。我的代码:

import sys, pygame
from pygame.locals import*

width=1000
height=500
Color_screen=(49,150,100)
Color_line=(255,0,0)

def main():
    screen=pygame.display.set_mode((width,height))
    screen.fill(Color_screen)
    pygame.display.flip()
    pygame.draw.line(screen,Color_line,(60,80),(130,100))
    while True:
        for events in pygame.event.get():
            if events.type == QUIT:

                sys.exit(0)
main()
Run Code Online (Sandbox Code Playgroud)

怎么了?

skr*_*krx 13

pygame.display.flip绘制线条后,您必须更新计算机的显示。

pygame.draw.line(screen, Color_line, (60, 80), (130, 100))
pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)

这通常在 while 循环的底部每帧执行一次。