Using move_ip in Pygame 1.9.4
Question:
I’m trying to move a rectangle that I created in the far top right corner down and to the left. I’ve commented out the line that’s causing the error, which is:
invalid destination position for blit
# render box to display level
displayfont = pygame.font.SysFont(None, 30)
text = displayfont.render('level', True, (red), (white))
textrect = text.get_rect()
textrect.topright = screen.get_rect().topright
# textrect = textrect.move_ip(-50, -50) #keeps getting invalid destination for blit
screen.blit(text, textrect)
Any suggestions? Thanks in advance!
Answers:
The method pygame.Rect.move_ip
doesn’t return any value. It modifies the pygame.Rect
object itself.
So after
textrect = textrect.move_ip(-50, -50)
the value of textrect
is None
.
Further note, that the top right coordinate of a Surface
is (widht, 0). If you want to move to the center of the surface, then you have to move in the negative x direction, but in the positive y direction rather than the negative y direction.
To solve your issue it has to be:
displayfont = pygame.font.SysFont(None, 30)
text = displayfont.render('level', True, (red), (white))
textrect = text.get_rect()
textrect.topright = screen.get_rect().topright
textrect.move_ip(-50, 50)
screen.blit(text, textrect)
I’m trying to move a rectangle that I created in the far top right corner down and to the left. I’ve commented out the line that’s causing the error, which is:
invalid destination position for
blit
# render box to display level
displayfont = pygame.font.SysFont(None, 30)
text = displayfont.render('level', True, (red), (white))
textrect = text.get_rect()
textrect.topright = screen.get_rect().topright
# textrect = textrect.move_ip(-50, -50) #keeps getting invalid destination for blit
screen.blit(text, textrect)
Any suggestions? Thanks in advance!
The method pygame.Rect.move_ip
doesn’t return any value. It modifies the pygame.Rect
object itself.
So after
textrect = textrect.move_ip(-50, -50)
the value of textrect
is None
.
Further note, that the top right coordinate of a Surface
is (widht, 0). If you want to move to the center of the surface, then you have to move in the negative x direction, but in the positive y direction rather than the negative y direction.
To solve your issue it has to be:
displayfont = pygame.font.SysFont(None, 30)
text = displayfont.render('level', True, (red), (white))
textrect = text.get_rect()
textrect.topright = screen.get_rect().topright
textrect.move_ip(-50, 50)
screen.blit(text, textrect)