Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create rect variables in python pygame

I am programming in Python with the pygame library. I want to know how to make a rectangle variable, something like:

import ...
rect1 = #RECT VARIABLE
rect2 = #RECT VARIABLE
like image 298
David Wolters Avatar asked Jun 11 '26 09:06

David Wolters


2 Answers

Just do

pygame.Rect(left, top, width, height)

this will return you a Rect object in pygame

like image 54
Brella J Avatar answered Jun 13 '26 21:06

Brella J


Perhaps something like this will work?

import pygame
# your whole code until you need a rectangle
rect1 = pygame.draw.rect( (x_coordinate, y_coordinate), (#a rgb color), (#size of rect in pixels) )

If you want a red rectangle with 100x150 size in 0x50 position, it will be:

rect1 = pygame.draw.rect( (0, 50), (255, 0, 0), (100, 150) )

To insert it into the screen you use:

pygame.screen_you_have_created.blit(rect1, 0, 50)
like image 37
Jean Alves Avatar answered Jun 13 '26 22:06

Jean Alves



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!