I am relatively new to Pygame and I'm attempting to create a Pacman game for a project. I'm having trouble with collision detection between Pacman and this block (imported image). Pacman goes right through the block which I don't want to happen; I want it so that Pacman can't pass through it. I've searched many websites and many forums and tried many different methods that I've seen other people have used but I can't seem to get my head around collision detection. Below is my current code.
def Pacman():
pygame.init()
# Creating screen
global screen
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
screen = pygame.display.set_mode((0,0), pygame.RESIZABLE)
# Setting window caption
pygame.display.set_caption('Pacman')
# Loading two images
pacman_image = pygame.image.load("pacmanphoto.png").convert_alpha()
block_image = pygame.image.load("blockphoto.png").convert_alpha()
rect1 = pacman_image.get_rect()
rect2 = block_image.get_rect()
rect1.x = 100
rect1.y = 200
rect2.x = 300
rect2.y = 400
clock = pygame.time.Clock()
x = 10
y = 10
pygame.key.set_repeat(10, 10)
# Movement
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y -= 10
if event.key == pygame.K_DOWN:
y += 10
if event.key == pygame.K_RIGHT:
x += 10
if event.key == pygame.K_LEFT:
x -= 10
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if rect1.colliderect(rect2):
y -=10
##### where collision detection should be ####
# Displaying images on screen
screen.blit(pacman_image, (x,y))
screen.blit(block_image, (50,50))
clock.tick(12)
pygame.display.update()
screen.fill(BLACK)
If anyone can help me in any way please let me know :) Thanks