0

I've tried everything I can think of for this PyGame collision detection with the "maze.png" image. (player collides with maze) The player will still just go through the maze. This code is just the maze (image) in the position I want it and a player (rect) which can be moved with the arrow keys. Modify this code in any way to make the player collide with the maze. I need help! Thanks.

    #Imports needed libraries.
import pygame
#Initialises PyGame.
pygame.init()
#Sets window resolution, icon & title. Also gets required images.
win = pygame.display.set_mode((512,320))
pygame.display.set_caption("Maze Game")
window_icon = pygame.image.load("C:\\Users\\chizl\\Documents\\Images\\project_icon.png")
pygame.display.set_icon(window_icon)
level = pygame.image.load("C:\\Users\\chizl\\Documents\\Images\\bg-1.png")
#The necessary values for the player.
x = 48
y = 48
width = 16
height = 16
vel = 16
keys = pygame.key.get_pressed()
#While loop for window (put all code here).
run = True
while run:
    pygame.time.delay(128)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    #Key presses.
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= vel
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel
        
    #Window properties.
    win.fill((255,255,255))
    win.blit(level, (0,0))
    pygame.draw.rect(win, (0,0,0), (x, y, width, height))
    pygame.display.update() 
    
pygame.quit()
#CL 2022
  • Examine the color of the pixels underneath the player rectangle, and use that information to determine if you hit a wall or not. You'll need to decide what colors represent walls/spaces. White and black are reasonable choices. That would be a first step. Right now, you're not using your `level` (presumably the maze) image at all. So it appears you haven't even tried _anything_, let alone "everything". – paddy Feb 10 '22 at 02:57
  • @paddy Thanks for that idea. I'll try it out. – pythonbetter Feb 10 '22 at 20:45

0 Answers0