python 如何在Pygame中创建网格

ve7v8dk2  于 2023-05-16  发布在  Python
关注(0)|答案(3)|浏览(183)

我正在尝试用Python创建一个基本的蛇游戏,我不熟悉Pygame。我已经创建了一个窗口,我正试图将该窗口分割成一个网格的基础上,窗口的大小和一套广场的大小。

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

我应该在window.fill函数中添加什么来创建一个基于宽度、高度和block_size的网格?任何信息都会有帮助。

aamkag61

aamkag611#

使用for循环作为答案的引用:https://stackoverflow.com/a/33963521/9715289
这就是我在做一个蛇游戏时所做的。

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)

结果是什么样子的:

pqwbnv8z

pqwbnv8z2#

你可以画矩形

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)

我假设heightwidth是块的数量。
如果矩形之间需要一个像素间隔,则使用

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

要画蛇,你可以使用list和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)
zvokhttg

zvokhttg3#

此外,如果你想根据游戏窗口的分辨率调整方块的宽度和高度,你可以查看下面的代码。

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()

相关问题