Min Woo Kim
Min Woo Kim

Reputation: 29

rect move dosen't work

import pygame, random
pygame.init()
screen = pygame.display.set_mode((700,500))
ball = pygame.image.load('C://python32/ball.jpg')
brick = pygame.image.load('C://python32/brick.jpg')
rect1 = ball.get_rect()
rect2 = brick.get_rect()
screen.fill((255,255,198))
screen.blit(ball,rect1)
screen.blit(brick,rect2)
pygame.display.flip()
if rect1.colliderect(rect2):
    x=random.randrange(0,550)
    y=random.randrange(0,350)
    rect2.move(x,y)            #<-------This part
    pygame.display.flip()

I have 2 images ball and brick. When I load them on pygame the 2 images collide. So the if rect1.colliderect(rect2) should work. I tested that by putting is a print function. But the rect2.move dosen't work it doesn't show the change on the pygame. What is wrong?

Upvotes: 1

Views: 718

Answers (1)

skrx
skrx

Reputation: 20438

pygame.Rect.move creates a new rect object with the new position which you have to assign to a variable if you want to use it later.

If you just want to modify the existing rect, you can call the move_ip method (ip stands for in-place), so that the rect is still the same object with a new position.

Upvotes: 1

Related Questions