how to draw rectangel pygame code example

Example 1: how to make a rectangle in pygame

# make the x position, y position, width and the height of the rectangle
x_pos = 20
y_pos = 20
width = 50
height = 50

# make the rectangle variable
rectangle = pygame.Rect(x_pos, y_pos, width, height)

# now add this to a surface, add a colour and the rectangle and make it a function
def make_rect():
	pygame.draw.rect(surface, (RGB colour in this tuple), rectangle)


# after this add it to your infinite loop
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
	make_rect()
    pygame.display.update()

Example 2: draw a line pygame

pygame.draw.line(surface, color, start_pos, end_pos, width)

Tags:

Misc Example