So here is two parts of pygame python code:
a = self.img.get_rect(topleft = (self.x, self.y))
print(a.topleft)
The output of this code is :
(200, 189)
Another pygame code:
a = self.img.get_rect(topleft = (self.x, self.y)).center
print(a)
The output of this code:
(234, 213)
Note: The value of self.x is 200 and self.y is 200
So now my question is how after we put .center after the pygame rect object or the variable a when i print the value of a it changes and what does a .center after a pygame rect object do?
pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position. The position of the rectangle can be specified by a keyword argument. For example, the top left of the rectangle can be specified with the keyword argument topleft.
The pygame.Rect object has various virtual attriubtes:
The Rect object has several virtual attributes which can be used to move and align the Rect:
x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h
You actually set the top left corner of a rectangle, the size of the image:
self.img.get_rect(topleft = (self.x, self.y))
Finally, you get the center point of this rectangle by reading the center attribute. Since the size of the image is not 0, the center differs from the topleft.
This question already has answers here:
Pygame Drawing a Rectangle
(6 answers)
Closed 2 years ago.
i want to creat a game like Pong but just one player from downside, and multiple box from up side, when the ball hit the box, the box will disappear. I can't remember name of that game but i don't know hot to create multiple rectangles. I can draw it by easy way
Use pygame.draw.rect to draw a rectangle:
pygame.draw.rect(screen, color, (x, y, width, height))
Use nested loops to draw create the locations of multiple rectangles
rectwidth = 40
rectheight = 40
rectdist = 10
block_positions = []
for i in range(10):
for j in range(2)
x = 100 + i * (rectdist + rectwidth)
y = 100 + j * (rectdist + rectheight)
block_positions.append((x, y))
Draw the rectangles in a loop:
for x, y in block_positions:
pygame.draw.rect(screen, (255, 255, 255), (x, y, rectwidth, rectheight))
This question already has an answer here:
Pygame is running slow
(1 answer)
Closed 2 years ago.
I am creating a 3d pong game using pygame. I wanted to add a thick black border layer to the rectangle to make it more stylish. Here's what I tried:
pygame.draw.rect(screen, (0,0,255), (x,y,150,150), 0)
pygame.draw.rect(screen, (0,0,0), (x-1,y-1,155,155), 1)
pygame.draw.rect(screen, (0,0,0), (x-2,y-2,155,155), 1)
pygame.draw.rect(screen, (0,0,0), (x-3,y-3,155,155), 1)
pygame.draw.rect(screen, (0,0,0), (x-4,y-4,155,155), 1)
It worked but as the game I am trying to create is a 3d game this method was time consuming. Please tell me if there is any inbuilt method in pygame to draw borders around a rectangle. Sorry for my English.
You could also put it in a function, and make it more concise with for loops. First, you'll note that the four rectangles you drew were in a nice, easy pattern, so you could compact the drawing of the four rectangles like this:
pygame.draw.rect(surface, (0,0,255), (x,y,150,150), 0)
for i in range(4):
pygame.draw.rect(surface, (0,0,0), (x-i,y-i,155,155), 1)
Then, because pygame draw functions do not need to be run in the global scope, you can put all this into a function:
def drawStyleRect(surface):
pygame.draw.rect(surface, (0,0,255), (x,y,150,150), 0)
for i in range(4):
pygame.draw.rect(surface, (0,0,0), (x-i,y-i,155,155), 1)
Then, in your mainloop, all you have to do is run:
while not done:
...
drawStyleRect(screen) # Or whatever you named the returned surface of 'pygame.display.set_mode()'
...
You could even put the drawing function in a separate module, if you really wanted to.
Create a function that creates a pygame.Surface object with per pixel alpha (SRCALPHA) and draw the rectangle and the border on the surface:
def create_rect(width, height, border, color, border_color):
surf = pygame.Surface((width+border*2, height+border*2), pygame.SRCALPHA)
pygame.draw.rect(surf, color, (border, border, width, height), 0)
for i in range(1, border):
pygame.draw.rect(surf, border_color, (border-i, border-i, width+5, height+5), 1)
return surf
Create all the surfaces before the main application loop and blit them in the loop:
rect_surf1 = create_rect(150, 150, 5, (0, 0, 255), (0, 0, 0))
# [...]
run = True
while run:
# [...]
screen.blit(rect_surf1, (x, y))
# [...]
This question already has an answer here:
How do i properly rescale an image on PyGame without it being badly cropped?
(1 answer)
Closed 2 years ago.
I've wrote this but i want that the image fill all the screen. What i need to do?
def load_image(name):
img = pygame.image.load(name)
return img
bg1 = load_image("Backgrounds/Back1.png")
screen.blit(bg1, (WIDTH/2, HEIGHT/2))
pygame.display.update()
Create a full screen display (see pygame.display.set_mode()):
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
Use pygame.transform.smoothscale() to scale the scaled image to the size of the window
bg1 = load_image("Backgrounds/Back1.png")
bg_scaled = pygame.transform.smoothscale(bg1, screen.get_size())
screen.blit(bg_scaled, (0, 0))
This question already has answers here:
How do I rotate an image around its center using Pygame?
(6 answers)
Closed 3 years ago.
I'm trying to build a simple game and so far I'm just learning the basics.
I'm trying to draw a rectangle tilted 45°, but I couldn't figure how to keep it centered even after reading some previous question here on SO.
So I tried making a rectangle that keeps rotating; this is the corresponding code.
alpha=0
while True:
w, h=screen.get_size()
s=pygame.Surface((w/2, h))
pygame.draw.rect(s, col, (300,150,50,10))
s=pygame.transform.rotozoom(s, alpha, 1)
alpha+=2
s.set_colorkey((0,0,0))
background.blit(s, (0, 0))
# flip screen, etc
The surface should keep rotating forever around some center (I wanted to use this to understand clearly which it was), but it moves in an irregular way.
This is the video of what happens [...].
EDIT:marked as duplicate, I'm removing the video link
Rotating a rectangle (not image) in pygame provides you an answer:
Example code:
import pygame as py
# define constants
WIDTH = 500
HEIGHT = 500
FPS = 30
# define colors
BLACK = (0 , 0 , 0)
GREEN = (0 , 255 , 0)
# initialize pygame and create screen
py.init()
screen = py.display.set_mode((WIDTH , HEIGHT))
# for setting FPS
clock = py.time.Clock()
rot = 0
rot_speed = 3
# define a surface (RECTANGLE)
image_orig = py.Surface((100 , 100))
# for making transparent background while rotating an image
image_orig.set_colorkey(BLACK)
# fill the rectangle / surface with green color
image_orig.fill(GREEN)
# creating a copy of orignal image for smooth rotation
image = image_orig.copy()
image.set_colorkey(BLACK)
# define rect for placing the rectangle at the desired position
rect = image.get_rect()
rect.center = (WIDTH // 2 , HEIGHT // 2)
# keep rotating the rectangle until running is set to False
running = True
while running:
# set FPS
clock.tick(FPS)
# clear the screen every time before drawing new objects
screen.fill(BLACK)
# check for the exit
for event in py.event.get():
if event.type == py.QUIT:
running = False
# making a copy of the old center of the rectangle
old_center = rect.center
# defining angle of the rotation
rot = (rot + rot_speed) % 360
# rotating the orignal image
new_image = py.transform.rotate(image_orig , rot)
rect = new_image.get_rect()
# set the rotated rectangle to the old center
rect.center = old_center
# drawing the rotated rectangle to the screen
screen.blit(new_image , rect)
# flipping the display after drawing everything
py.display.flip()
py.quit()