Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pygame tile generation wont work properly

My goal is to make a tile generator where each tile has an X and Y position. It should be able to generate tiles that fill the entire screen. Well, at least that’s what I expect it to do. It doesn’t generate any tiles in the top row except for one in the corner.
Picture of tile generator

I have tried changing the way each tile’s position is calculated, but it hasn’t worked. There are no errors either.

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

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Each row starts at 0, so ygen_num needs to be set to 0 in the outer loop:

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

Alternatively, you can calculate the coordinates in the loop:

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading