Pygame 磁贴生成无法正常工作

Pygame tile generation wont work properly

我的目标是制作一个图块生成器,其中每个图块都有一个 X 和 Y 位置。它应该能够生成填满整个屏幕的图块。好吧,至少那是我期望它做的。它不会在顶行生成任何图块,除了角落中的一个。

我试过更改每个图块位置的计算方式,但没有奏效。也没有错误。

#Tile Generator
import pygame
import random
pygame.init()
wh = 500
ww = 500
grid_size = 10
display = pygame.display.set_mode((ww,wh))
pygame.display.set_caption("Tile Generator")
clock = pygame.time.Clock()

end = False

class Tile:
    def __init__(self, type, posx, posy):
        self.type = type
        self.size = (ww/grid_size, wh/grid_size)
        self.posx = posx
        self.posy = posy
        self.color_list = [(84, 219, 22), (64, 166, 17), (50, 117, 19)]
        self.color = random.choice(self.color_list)
    def draw(self):
        pygame.draw.rect(display,self.color,(self.posx, self.posy, self.size[0], self.size[1]))


tiles = []

def generate():
    xgen_num = 0
    ygen_num = 0
    for x in range(grid_size):
        for y in range(grid_size):
            tiles.append(Tile("grass",xgen_num,ygen_num))
            if ygen_num != wh:
                ygen_num += wh/grid_size
            else:
                ygen_num = wh/grid_size
        xgen_num += ww/grid_size

generate()
for t in tiles:
    print(f"{t.posx} {t.posy}")

while not end:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            end = True


    display.fill((255,255,255))
    for t in tiles:
        t.draw()

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

pygame.quit()
quit()

输出:

pygame 2.0.1 (SDL 2.0.14, Python 3.9.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
0 0
0 50.0
0 100.0
0 150.0
0 200.0
0 250.0
0 300.0
0 350.0
0 400.0
0 450.0
50.0 500.0
50.0 50.0
50.0 100.0
50.0 150.0
50.0 200.0
50.0 250.0
50.0 300.0
50.0 350.0
50.0 400.0
50.0 450.0
100.0 500.0
100.0 50.0
100.0 100.0
100.0 150.0
100.0 200.0
100.0 250.0
100.0 300.0
100.0 350.0
100.0 400.0
100.0 450.0
150.0 500.0
150.0 50.0
150.0 100.0
150.0 150.0
150.0 200.0
150.0 250.0
150.0 300.0
150.0 350.0
150.0 400.0
150.0 450.0
200.0 500.0
200.0 50.0
200.0 100.0
200.0 150.0
200.0 200.0
200.0 250.0
200.0 300.0
200.0 350.0
200.0 400.0
200.0 450.0
250.0 500.0
250.0 50.0
250.0 100.0
250.0 150.0
250.0 200.0
250.0 250.0
250.0 300.0
250.0 350.0
250.0 400.0
250.0 450.0
300.0 500.0
300.0 50.0
300.0 100.0
300.0 150.0
300.0 200.0
300.0 250.0
300.0 300.0
300.0 350.0
300.0 400.0
300.0 450.0
350.0 500.0
350.0 50.0
350.0 100.0
350.0 150.0
350.0 200.0
350.0 250.0
350.0 300.0
350.0 350.0
350.0 400.0
350.0 450.0
400.0 500.0
400.0 50.0
400.0 100.0
400.0 150.0
400.0 200.0
400.0 250.0
400.0 300.0
400.0 350.0
400.0 400.0
400.0 450.0
450.0 500.0
450.0 50.0
450.0 100.0
450.0 150.0
450.0 200.0
450.0 250.0
450.0 300.0
450.0 350.0
450.0 400.0
450.0 450.0

每行从0开始,所以ygen_num需要在外层循环中设置为0:

def generate():
    xgen_num = 0
    for x in range(grid_size):
        ygen_num = 0
        for y in range(grid_size):
            tiles.append(Tile("grass",xgen_num,ygen_num))
            ygen_num += wh/grid_size
        xgen_num += ww/grid_size

或者,您可以在循环中计算坐标:

def generate():
    for ix in range(grid_size):
        for iy in range(grid_size):
            x = ix * ww // grid_size
            y = iy * wh // grid_size
            tiles.append(Tile("grass", x, y))