1

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

2
  • 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... Commented Oct 8, 2017 at 11:41
  • can you plz show me how to do that Commented Oct 8, 2017 at 13:03

1 Answer 1

2

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
...
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use// instead of/ and skip the int, because / between two ints generates a float result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.