【问题标题】:drawing float in pygame blit [duplicate]在pygame blit中绘制浮点数[重复]
【发布时间】:2020-08-14 12:22:15
【问题描述】:

我有一个问题:

screen.blit(tile, [(x*tilewidth) - CAMERA.x +(WIDTH/2) , (y*tileheight) - CAMERA.y + (HEIGHT/2)])

screen.blit(object.image, [object.x - CAMERA.x +(WIDTH/2), object.y - CAMERA.y + (HEIGHT/2)])

上面的代码产生错误:

DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated and may be removed in a future version of Python.
  screen.blit(tile, [(x*tilewidth) - CAMERA.x +(WIDTH/2) , (y*tileheight) - CAMERA.y + (HEIGHT/2)])

程序启动但随后由于错误而崩溃。我该如何解决这个问题?

【问题讨论】:

  • WIDTH/2 导致浮动。尝试整数除法:WIDTH // 2
  • 结果还是一样
  • 问题解决了吗?
  • 是的,你帮我

标签: python pygame


【解决方案1】:

round 坐标到整数值:

screen.blit(tile, [(x*tilewidth) - CAMERA.x +(WIDTH/2) , (y*tileheight) - CAMERA.y + (HEIGHT/2)])

screen.blit(tile, 
    [round(x*tilewidth - CAMERA.x + WIDTH/2), round(y*tileheight - CAMERA.y + HEIGHT/2)])

或使用//(地板除法)运算符(仅当所有变量都具有整数值时才有效):

screen.blit(tile, 
    [x*tilewidth - CAMERA.x + WIDTH//2, y*tileheight - CAMERA.y + HEIGHT//2])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    相关资源
    最近更新 更多