I am fairly new to python (I just started at the beginning of this year). I am working on this maze game project for a school assignment in which you use the up, down, and side keys to move a red square through a maze. the goal is to get to the end without colliding with one of the many black rectangles that make up the maze. I already know of one way to make it so when the square hits one of the rectangles the game ends (text will pop up on the screen asking if the player wants to play again). the way I have learned is to use the CollideRect function for every rectangle. (c.colliderect(r)). where c = cube (x, y) and r(1,2,3,etc.) = for each rectangle. but this way is very tedious because there are many rectangles used.

is there a simpler and much more efficient way to accomplish my goal?

I wanted to use color as the defining variable (if red == black -> gameover() in a sense) but I cannot find a way to accomplish this.

I also tried by defining the rectangles or putting them all under one class as well.

please let me know if there is a way to do this!

here is my code:

import pygame
import time

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)

pygame.init()

size = (610, 410)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Maze Game")
screen.fill(white)
pygame.display.flip()

x = 20
y = 360
x_speed = 0
y_speed = 0

def cube(x, y):
    c = pygame.Rect(x + x_speed, y + y_speed, 30, 30)
    pygame.draw.rect(screen, red,[x + x_speed, y + y_speed, 30, 30])
    return c

def rectangles():
    #pygame.Rect([0, 0, 610, 410], 10)
    #border wasnt working as one of these but works further down#
    pygame.Rect(50, 60, 50, 340)
    pygame.Rect(100, 60, 50, 50)
    pygame.Rect(200, 10, 50, 300)
    pygame.Rect(250, 10, 50, 50)
    pygame.Rect(150, 150, 50, 200)
    pygame.Rect(300, 100, 50, 50)
    pygame.Rect(200, 300, 100, 50)
    pygame.Rect(350, 60, 50, 340)
    pygame.Rect(250, 200, 50, 100)
    pygame.Rect(450, 10, 50, 340)
    pygame.Rect(550, 60, 50, 340)


def gameover():
    font = pygame.font.SysFont(None, 55)
    text = font.render("Game Over! Play Again? (y or n)", True, green)
    screen.blit(text, [20, 250])
    pygame.display.flip()
    done = False
    while (done == False):
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                pygame.quit()
            elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                pygame.quit()

            if(event.type == pygame.KEYDOWN):
                if(event.key == pygame.K_y):
                    board1()
            if(event.type == pygame.KEYDOWN):
                if (event.key == pygame.K_n):
                    pygame.quit()

def board1():
    x = 15 
    y = 360
    x_speed = 0
    y_speed = 0
    done = False
    while (not done):
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                pygame.quit()
            elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                pygame.quit()

            if(event.type == pygame.KEYDOWN):
                if(event.key == pygame.K_UP):
                    y_speed = -.1
                if(event.key == pygame.K_DOWN):
                    y_speed = .1
                if(event.key == pygame.K_LEFT):
                    x_speed = -.1
                if(event.key == pygame.K_RIGHT):
                    x_speed = .1

            if(event.type == pygame.KEYUP):
                if (event.key == pygame.K_UP):
                    y_speed = 0
                if (event.key == pygame.K_DOWN):
                    y_speed = 0
                if (event.key == pygame.K_LEFT):
                    x_speed = 0
                if (event.key == pygame.K_RIGHT):
                    x_speed = 0



        screen.fill(white)
        c = cube(x, y)
        r = rectangles()
        pygame.draw.rect(screen, black, [0, 0, 610, 410], 10)
        pygame.draw.rect(screen, black, [50, 60, 50, 340])
        pygame.draw.rect(screen, black, [100, 60, 50, 50])
        pygame.draw.rect(screen, black, [200, 10, 50, 300])
        pygame.draw.rect(screen, black, [250, 10, 50, 50])
        pygame.draw.rect(screen, black, [150, 150, 50, 200])
        pygame.draw.rect(screen, black, [300, 100, 50, 50])
        pygame.draw.rect(screen, black, [200, 300, 100, 50])
        pygame.draw.rect(screen, black, [350, 60, 50, 340])
        pygame.draw.rect(screen, black, [250, 200, 50, 100])
        pygame.draw.rect(screen, black, [450, 10, 50, 340])
        pygame.draw.rect(screen, black, [550, 60, 50, 340])


        
        pygame.display.flip()

        x = x + x_speed
        y = y + y_speed

        if(c.colliderect(r)):
            gameover()
        

board1()
🟢 Solution
0

Well if you just need to check whether any of the rectangles have collided with the player then you can simply add them all to a list and go through that list checking each one for a collision. Something like this should work:

import pygame
import time

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)

pygame.init()

size = (610, 410)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Maze Game")
screen.fill(white)
pygame.display.flip()

x = 20
y = 360
x_speed = 0
y_speed = 0

def cube(x, y):
    c = pygame.Rect(x + x_speed, y + y_speed, 30, 30)
    pygame.draw.rect(screen, red,[x + x_speed, y + y_speed, 30, 30])
    return c

rects = []

def rectangles():
    #pygame.Rect([0, 0, 610, 410], 10)
    #border wasnt working as one of these but works further down#

    # add them all to a list
    rects.append(pygame.Rect(50, 60, 50, 340))
    rects.append(pygame.Rect(100, 60, 50, 50))
    rects.append(pygame.Rect(200, 10, 50, 300))
    rects.append(pygame.Rect(250, 10, 50, 50))
    rects.append(pygame.Rect(150, 150, 50, 200))
    rects.append(pygame.Rect(300, 100, 50, 50))
    rects.append(pygame.Rect(200, 300, 100, 50))
    rects.append(pygame.Rect(350, 60, 50, 340))
    rects.append(pygame.Rect(250, 200, 50, 100))
    rects.append(pygame.Rect(450, 10, 50, 340))
    rects.append(pygame.Rect(550, 60, 50, 340))


def gameover():
    font = pygame.font.SysFont(None, 55)
    text = font.render("Game Over! Play Again? (y or n)", True, green)
    screen.blit(text, [20, 250])
    pygame.display.flip()
    done = False
    while (done == False):
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                pygame.quit()
            elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                pygame.quit()

            if(event.type == pygame.KEYDOWN):
                if(event.key == pygame.K_y):
                    board1()
            if(event.type == pygame.KEYDOWN):
                if (event.key == pygame.K_n):
                    pygame.quit()

def board1():
    x = 15 
    y = 360
    x_speed = 0
    y_speed = 0
    done = False
    while (not done):
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                pygame.quit()
            elif(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                pygame.quit()

            if(event.type == pygame.KEYDOWN):
                if(event.key == pygame.K_UP):
                    y_speed = -.1
                if(event.key == pygame.K_DOWN):
                    y_speed = .1
                if(event.key == pygame.K_LEFT):
                    x_speed = -.1
                if(event.key == pygame.K_RIGHT):
                    x_speed = .1

            if(event.type == pygame.KEYUP):
                if (event.key == pygame.K_UP):
                    y_speed = 0
                if (event.key == pygame.K_DOWN):
                    y_speed = 0
                if (event.key == pygame.K_LEFT):
                    x_speed = 0
                if (event.key == pygame.K_RIGHT):
                    x_speed = 0



        screen.fill(white)
        c = cube(x, y)
        r = rectangles()

        pygame.draw.rect(screen, black, [0, 0, 610, 410], 10)
        pygame.draw.rect(screen, black, [50, 60, 50, 340])
        pygame.draw.rect(screen, black, [100, 60, 50, 50])
        pygame.draw.rect(screen, black, [200, 10, 50, 300])
        pygame.draw.rect(screen, black, [250, 10, 50, 50])
        pygame.draw.rect(screen, black, [150, 150, 50, 200])
        pygame.draw.rect(screen, black, [300, 100, 50, 50])
        pygame.draw.rect(screen, black, [200, 300, 100, 50])
        pygame.draw.rect(screen, black, [350, 60, 50, 340])
        pygame.draw.rect(screen, black, [250, 200, 50, 100])
        pygame.draw.rect(screen, black, [450, 10, 50, 340])
        pygame.draw.rect(screen, black, [550, 60, 50, 340])


        
        pygame.display.flip()

        x = x + x_speed
        y = y + y_speed

        # Check for each one of the walls
        for rect in rects:
            if c.colliderect(rect):
                print("collision")
                gameover()


board1()

However if you do want to detect by colour, its a bit more complex, but you can take a look at this: Color collision detection in Pygame.