【问题标题】:'TypeError: integer argument expected, got float''TypeError:需要整数参数,得到浮点数'
【发布时间】:2019-10-16 09:26:33
【问题描述】:

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

【问题讨论】:

  • Android 不会出现此问题。

标签: python python-3.x pygame


【解决方案1】:

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

【讨论】:

  • 谢谢你,你帮了我这么多!
猜你喜欢
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多