提问者:小点点

'TypeError:需要整数参数,得到浮点'


我正在用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个答案

匿名用户

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

使用intround来投射self。位置从浮点值到整数值:

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)

另请参阅数字类型。