如何在我的蛇应用程序中的网格之间填充颜色

How to fill in colour between the grid in my snake application

我遇到了这个问题,我可以为我的蛇画一个网格,但我无法在 it.It 之间填充颜色似乎问题是因为表面上的网格覆盖了实际颜色背景,但我不知道如何解决这个问题,任何了解更多 pygame 的人都可以帮忙。 请参阅我的代码的第 74 行,这是它应该的颜色。

import pygame
import time
import random

pygame.init()

display_width = 960
display_height = 540
display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Snake")

pureblue = (0,0,255)
purered = (255,0,0)
puregreen = (0,255,0)
green = (0,150,0)
white = (255,255,255)
black = (1,1,1)
grey = (75,75,75)
darkgrey = (50,50,50)


clock = pygame.time.Clock()

snake_block = 10
snake_speed = 5

font_style = pygame.font.SysFont(None, 50)

def drawGrid(surf):
    blockSize = snake_block 
    for x in range(display_width):
        for y in range(display_height):
            rect = pygame.Rect(x*blockSize, y*blockSize,blockSize, blockSize)
            pygame.draw.rect(surf, darkgrey, rect, 1)
grid_surf = pygame.Surface(display.get_size())
drawGrid(grid_surf)

def message(msg, colour):
    text = font_style.render(msg, True, colour)
    display.blit(text, [display_width/2, display_height/2])

def SnakeGameLoop():
    game_over = False

    X = display_width/2
    Y = display_height/2

    X_change = 0
    Y_change = 0

    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    X_change = -snake_block
                    Y_change = 0
                elif event.key == pygame.K_RIGHT:
                    X_change = snake_block
                    Y_change = 0
                elif event.key == pygame.K_UP:
                    X_change = 0
                    Y_change = -snake_block
                elif event.key == pygame.K_DOWN:
                    X_change = 0
                    Y_change = snake_block

        if X >= display_width or X < 0 or Y >= display_height or Y < 0:
            game_over = True

        X += X_change
        Y += Y_change
        display.fill(grey)
        display.blit(grid_surf, (0,0))

        pygame.draw.rect(display,puregreen,[X,Y,snake_block,snake_block])

        pygame.display.update()
        clock.tick(snake_speed)

    message("You lost", purered)
    pygame.display.update()
    time.sleep(2)

    pygame.quit()
    quit()
SnakeGameLoop()

您需要 fill 网格 表面 具有背景颜色:

def drawGrid(surf):

    surf.fill(grey) # <--- ADD

    blockSize = snake_block 
    for x in range(display_width):
        for y in range(display_height):
            rect = pygame.Rect(x*blockSize, y*blockSize,blockSize, blockSize)
            pygame.draw.rect(surf, darkgrey, rect, 1)

grid_surf = pygame.Surface(display.get_size())
drawGrid(grid_surf)

由于网格 Surface 覆盖了整个显示,因此无需在应用程序循环中清除显示:

def SnakeGameLoop():
    # [...]

    while not game_over:
        # [...]

        # display.fill(grey) <--- DELETE

        display.blit(grid_surf, (0,0))

        pygame.draw.rect(display,puregreen,[X,Y,snake_block,snake_block])

        pygame.display.update()
 
        # [...]