1
votes

This is my code:

import pygame, sys

pygame.init()

FPS = 30
clock = pygame.time.Clock()

screen = pygame.display.set_mode((480, 320))

mainsheet = pygame.image.load("walking.png")
sheet_size = mainsheet.get_size()
horiz_cells = 6
vert_cells = 5
cell_width = sheet_size[0] / horiz_cells
cell_height = sheet_size[1] / vert_cells

cell_list = []
for y in range (0, sheet_size[1], cell_height):
    for x in range (0, sheet_size[0], cell_width):
        surface = pygame.Surface((cell_width, cell_height))
        surface.blit(mainsheet, (0,0), (x, y, cell_width, cell_height))
        cell_list.append(surface)

cell_position = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if cell_position < len(cell_list) - 1:
            cell_position += 1
        else:
            cell_position = 0

screen.blit(cell_list[cell_position], (100, 10))

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

..and the error is:

Traceback (most recent call last): File "C:\Users\HP\Desktop\running.py", line 18, in for y in range (0, sheet_size[1], cell_height): TypeError: 'float' object cannot be interpreted as an integer

1
sheet_size[1] or cell_height isn't an integer - it's a float... so make them into an integer in a way that makes sense first... - Jon Clements
can you plz show me how to do that - user8740671

1 Answers

2
votes

This is from Python 3 docs:

The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method).

So, you need to use integers for the range arguments.

I don't know what you exactly need in your application, but changing these lines will fix the error:

...
cell_width = int(sheet_size[0] / horiz_cells)
cell_height = int(sheet_size[1] / vert_cells)
...

or

...
cell_width = sheet_size[0] // horiz_cells
cell_height = sheet_size[1] // vert_cells
...