2017-10-08 9 views
0

として解釈することはできませんこれは私のコードである:'フロート' オブジェクトは整数

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ある:

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

+0

'[1] SHEET_SIZE'や 'cell_height'は整数ではありません - それはです浮動小数点数を整数にすることができます。 –

+0

これを行う方法を私に教えてください – user8740671

答えて

1

これは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).

したがって、range引数にはの整数を使用する必要があります。

私はあなたが正確にあなたのアプリケーションに必要なものを知りませんが、これらの行を変更すると、エラーを修正します:

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

'/'の代わりに '/'を使い、 'int'をスキップすることもできますなぜなら、2つのint間の '/'は浮動小数点の結果を生成するからです。 –

+0

それは感謝の男を助けた – user8740671

関連する問題