在 Pygame 中画一条线 [英] Draw a Line in Pygame

查看:94
本文介绍了在 Pygame 中画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to draw a line in Python, but when I run the code below, this line never appears. In fact, I need to make a field with 4x4 sections, but let's begin with a line. My code:

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

怎么了?

推荐答案

你必须在画线后用 pygame.display.flip 更新你电脑的显示.

You have to update the display of your computer with pygame.display.flip after you draw the line.

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

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

That's usually done at the bottom of the while loop once per frame.

这篇关于在 Pygame 中画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆