Noah Dukehart
Noah Dukehart

Reputation: 171

How to make a grid in pygame

I am trying to create a basic snake game with Python and I am not familiar with Pygame. I have created a window and I am trying to split that window up into a grid based on the size of the window and a set square size.

def get_initial_snake( snake_length, width, height, block_size ):
    window = pygame.display.set_mode((width,height))
    background_colour = (0,0,0)
    window.fill(background_colour)

    return snake_list

What should I add inside window.fill function to create a grid based on width, height, and block_size? Any info would be helpful.

Upvotes: 16

Views: 70652

Answers (4)

fflores
fflores

Reputation: 432

Games are often developed in OOP because it is easier to present every element as a separate object that has his own attributes and methods. That's why I would like to provide an OOP based solution as well:

import pygame

class Board:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.board = [[0] * width for _ in range(height)]
        self.left = 10
        self.top = 10
        self.cell_size = 30

    def set_view(self, left, top, cell_size):
        self.left = left
        self.top = top
        self.cell_size = cell_size
    def render(self, screen):
        for y in range(self.height):
            for x in range(self.width):
                pygame.draw.rect(screen, (255,255,255), (
                    x * self.cell_size,
                    y * self.cell_size,
                    self.cell_size,
                    self.cell_size), 1)

screen = pygame.display.set_mode((500, 500))
board = Board(2, 2)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((0, 0, 0))
    board.render(screen)
    pygame.display.flip()

Upvotes: 0

Rasim Mert YILDIRIM
Rasim Mert YILDIRIM

Reputation: 1

In addition, if you want to adjust the width and height of the squares according to the resolution of the game window, you can review the code below.

import pygame

resolution = (800, 800)
screen = pygame.display.set_mode(resolution)
map_size = (10, 10)  # (rows, columns)
line_width = 3
clock = pygame.time.Clock()  # to set max FPS

def evaluate_dimensions():
    # Evaluate the width and the height of the squares.
    square_width = (resolution[0] / map_size[0]) - line_width * ((map_size[0] + 1) / map_size[0])
    square_height = (resolution[1] / map_size[1]) - line_width * ((map_size[1] + 1) / map_size[1])
    return (square_width, square_height)

def convert_column_to_x(column, square_width):
    x = line_width * (column + 1) + square_width * column
    return x

def convert_row_to_y(row, square_height):
    y = line_width * (row + 1) + square_height * row
    return y

def draw_squares():
    square_width, square_height = evaluate_dimensions()
    for row in range(map_size[0]):
        for column in range(map_size[1]):
            color = (100, 100, 100)  # (R, G, B)
            x = convert_column_to_x(column, square_width)
            y = convert_row_to_y(row, square_height)
            geometry = (x, y, square_width, square_height)
            pygame.draw.rect(screen, color, geometry)

while True:
    clock.tick(60)  # max FPS = 60
    screen.fill((0, 0, 0))  # Fill screen with black color.
    draw_squares()
    pygame.display.flip()  # Update the screen.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

Upvotes: 0

theshubhagrwl
theshubhagrwl

Reputation: 1026

Using the for loop as a reference from the answer: https://stackoverflow.com/a/33963521/9715289

This is what I did when I was trying to make a snake game.

BLACK = (0, 0, 0)
WHITE = (200, 200, 200)
WINDOW_HEIGHT = 400
WINDOW_WIDTH = 400


def main():
    global SCREEN, CLOCK
    pygame.init()
    SCREEN = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    CLOCK = pygame.time.Clock()
    SCREEN.fill(BLACK)

    while True:
        drawGrid()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        pygame.display.update()


def drawGrid():
    blockSize = 20 #Set the size of the grid block
    for x in range(0, WINDOW_WIDTH, blockSize):
        for y in range(0, WINDOW_HEIGHT, blockSize):
            rect = pygame.Rect(x, y, blockSize, blockSize)
            pygame.draw.rect(SCREEN, WHITE, rect, 1)

How the result looks like:

How the result looks like

Upvotes: 20

furas
furas

Reputation: 142641

You can draw rectangles

for y in range(height):
    for x in range(width):
        rect = pygame.Rect(x*block_size, y*block_size, block_size, block_size)
        pygame.draw.rect(window, color, rect)

I assumed that the height and width is the number of blocks.


if you need one pixel gap between rectangles then use

rect = pygame.Rect(x*(block_size+1), y*(block_size+1), block_size, block_size)

To draw snake you can use list and head_color, tail_color

snake = [(0,0), (0,1), (1,1), (1,2), (1,3)]

# head

x, y = snake[0]
rect = pygame.Rect(x*block_size, y*block_size, block_size, block_size)
pygame.draw.rect(window, head_color, rect)

# tail

for x, y in snake[1:]:
    rect = pygame.Rect(x*block_size, y*block_size, block_size, block_size)
    pygame.draw.rect(window, tail_color, rect)

Upvotes: 6

Related Questions