'TypeError:预期为整数参数,浮点数'

时间:2019-05-30 17:37:56

标签: python python-3.x pygame

我正在用python做游戏,但出现一个错误

  

预期为整数参数,为浮点数

,我不明白为什么。给我错误的行是:

pygame.draw.circle(screen, (128, 128, 128), 
    (self.location[0]-1, self.location[1]-1), self.size+1)
class Player(object):
    def __init__(self, name, colour):
        self.name = name
        self.colour = colour
        self.level = 1
        self.feed = 0
        self.size = 2
        self.speed = 6
        self.location = (SCREEN_SIZE[0]/2, SCREEN_SIZE[1]/2)
        self.destination = self.location
        self.stopDis = 5    #Stopping distance

    def render(self, screen):
        pygame.draw.circle(screen, (128, 128, 128),
            (self.location[0]-1, self.location[1]-1), self.size+1)  #Error here
        pygame.draw.circle(screen, self.colour, self.location, self.size)   #Draw circle

1 个答案:

答案 0 :(得分:3)

pygame.draw.circle的第三个参数必须是整数坐标(元组(x, y)),但除法的结果(/)是浮点值。

使用introundself.location从浮点值转换为整数值:

pygame.draw.circle(screen, (128, 128, 128), 
    (int(self.location[0]-1), int(self.location[1]-1)), self.size+1)

或者在计算//时进行整数除法(/)而不是浮点除法(self.location):

self.location = (SCREEN_SIZE[0] // 2, SCREEN_SIZE[1] // 2)

另请参见Numeric Types