Pygame,在windows中画一条线

2024-05-19 05:54:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在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()

怎么了?


Tags: 代码fromimportmaindisplaysyslineevents
2条回答

画线后,必须用pygame.display.flip更新计算机的显示。

pygame.draw.line(screen, Color_line, (60, 80), (130, 100))
pygame.display.flip()

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

在画线后执行pygame.display.flip()操作:

screen.fill(color)
pygame.display.flip()
pygame.draw.line(...)

问题是你在排队的人出现之前就把他们给遮住了。 请改为:

screen.fill(color)
pygame.draw.line(...)
pygame.display.flip()

相关问题 更多 >

    热门问题