如何将一些代码 Python2 转换为 Python3.x?



我正在游戏中处理动画,但是我遇到了一个错误。你能帮我吗?

还是我需要添加所有代码?

class Animation:
def __init__(self, x, y, sprites=None, time=100):
self.x = x
self.y = y
self.sprites = sprites
self.time = time
self.work_time = 0
self.skip_frame = 0
self.frame = 0
def update(self, dt):
self.work_time += dt
self.skip_frame = self.work_time // self.time
if self.skip_frame > 0:
self.work_time = self.work_time % self.time
self.frame += self.skip_frame
if self.frame >= len(self.sprites):
self.frame = 0
def get_sprite(self):
return self.sprites[self.frame]
Traceback (most recent call last):
File "C:UsersZyzzDesktopgamebin.py", line 210, in <module>
target.update(dt)
File "C:UsersZyzzDesktopgamebin.py", line 98, in update
self.skip_frame = self.work_time // self.time
TypeError: unsupported operand type(s) for //: 'int' and 'module'

我在你的代码中看到的与 python2/python3 中的代码无关。

这里self.time = time,时间似乎是导入的模块。
您正在尝试self.skip_frame = self.work_time // self.timeself.work_timedef __init__(...)
前面用 0 初始化 它试图在 int(0( 和模块(时间(之间进行操作,这是不可接受的。

但是根据您的问题标题,如果您希望您的代码从 python2.x 迁移到 python3.x 兼容,则有一个可用的包 2to3
您可以使用python-tools安装 2to3

$ 2to3 example.py
$ 2to3 -w example.py # -w for write the changes back to file

最新更新