问题描述
我正在尝试用 Python 创建一个基本的蛇游戏,但我不熟悉 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
我应该在 window.fill 函数中添加什么来创建基于宽度、高度和 block_size 的网格?任何信息都会有所帮助.
What should I add inside window.fill function to create a grid based on width, height, and block_size? Any info would be helpful.
推荐答案
使用 for 循环作为参考答案:https://stackoverflow.com/a/33963521/9715289
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)
结果如何:
这篇关于如何在pygame中制作网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!