Pygame doesn't let me use float for rect.move, but I need it

后端 未结 2 957
逝去的感伤
逝去的感伤 2020-11-27 23:36

I\'ve recently recreated a version of Lunar Lander (you know, the old retro game) in Python 3 and Pygame: my lander moves (̀̀̀rect.move) each frame along the y

相关标签:
2条回答
  • 2020-11-28 00:30

    Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data:

    The coordinates for Rect objects are all integers. [...]

    If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the pygame.Rect object. round the coordinates and assign it to the location (e.g. .topleft) of the rectangle:

    x, y = # floating point coordinates
    rect.topleft = round(x), round(y)
    
    0 讨论(0)
  • 2020-11-28 00:30

    I haven't tested it myself so I am sorry if this does not work but I think python is taking the number as a string so instead of int(variable)

    You should do float(variable)

    0 讨论(0)
提交回复
热议问题