Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a Line in Pygame

Tags:

python

pygame

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
screen_color = (49, 150, 100)
line_color = (255, 0, 0)

def main():
    screen=pygame.display.set_mode((width,height))
    screen.fill(screen_color)
    pygame.display.flip()
    pygame.draw.line(screen,line_color, (60, 80), (130, 100))

    while True:
        for events in pygame.event.get():
            if events.type == QUIT:
                sys.exit(0)
main()

What's wrong?

like image 258
J.Gor Avatar asked May 25 '18 17:05

J.Gor


1 Answers

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

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

like image 117
skrx Avatar answered Oct 04 '22 03:10

skrx