How do I change the colour of entire blocks in Pygame?

177 Views Asked by At

I am trying to create a game where I need to draw a maze. I am doing this by changing whole blocks of the screen into a different colour. However, this does not seem to work. I have an array which I use to reference where the coloured blocks should be. Please can I get help fixing this. Thank you.

Here is my code:

import pygame
import numpy as np

pygame.init()
pygame.font.init()
running = True


def exit_game():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

white = (255, 255, 255)
grey3 = (64, 64, 64)
bgcolour = white
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
screen.fill(bgcolour)  # Colour of the screen
pygame.display.set_caption("Maze")  # Title of the screen

maze = np.asarray([
          [0, 0, 1, 1, 1, 1, 1],
          [0, 0, 1, 1, 1, 1, 1],
          [1, 0, 1, 0, 0, 0, 1],
          [1, 0, 0, 0, 0, 0, 1],
          [1, 1, 0, 0, 1, 0, 1],
          [1, 1, 1, 0, 1, 0, 1],
          [1, 1, 1, 1, 1, 0, 1]])

def draw_rect():
   for x in range(0, screen_width, block_sizex):
       for y in range(0, screen_height, block_sizey):
           for y0 in range(len(maze)):
               for x0 in range(len(maze[0])):
                   if maze[y0][x0] == True:
                       x1 = (x0 * wall_width) + 1
                       y1 = (y0 * wall_height) + 1
                       rect = pygame.Rect(x1, y1, block_sizex, block_sizey)
                       pygame.draw.rect(screen, grey3, rect)

while running:
    draw_rect()
    exit_game()
    pygame.display.flip()


1

There are 1 best solutions below

0
Rabbid76 On BEST ANSWER

This code works fine when you define block_sizex, block_sizey, wall_width, wall_height.

e.g.

block_sizex, block_sizey = 32, 32
wall_width, wall_height = 32, 32

Complete example:

import pygame
import numpy as np

pygame.init()
pygame.font.init()
running = True

def exit_game():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

white = (255, 255, 255)
grey3 = (64, 64, 64)
block_sizex, block_sizey = 32, 32
wall_width, wall_height = 32, 32
bgcolour = white
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Maze")  # Title of the screen

maze = np.asarray([
          [0, 0, 1, 1, 1, 1, 1],
          [0, 0, 1, 1, 1, 1, 1],
          [1, 0, 1, 0, 0, 0, 1],
          [1, 0, 0, 0, 0, 0, 1],
          [1, 1, 0, 0, 1, 0, 1],
          [1, 1, 1, 0, 1, 0, 1],
          [1, 1, 1, 1, 1, 0, 1]])

def draw_rect():
   for x in range(0, screen_width, block_sizex):
       for y in range(0, screen_height, block_sizey):
           for y0 in range(len(maze)):
               for x0 in range(len(maze[0])):
                   if maze[y0][x0] == True:
                       x1 = (x0 * wall_width) + 1
                       y1 = (y0 * wall_height) + 1
                       rect = pygame.Rect(x1, y1, block_sizex, block_sizey)
                       pygame.draw.rect(screen, grey3, rect)

while running:
    screen.fill(bgcolour)  # Colour of the screen
    draw_rect()
    exit_game()
    pygame.display.flip()