gpt4 book ai didi

python - 'TypeError : integer argument expected, float '

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:57 27 4
gpt4 key购买 nike

我正在用 python 玩游戏,但有一个错误

integer argument expected, got float

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

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

最佳答案

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时做整数除法(//)而不是 float 除法(/):

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

另见 Numeric Types .

关于python - 'TypeError : integer argument expected, float ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56383056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com