0

I'm working with animation in my game, but I've got an error.. Can you help me pls?)

Or do I need to add all of my code?

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:\Users\Zyzz\Desktop\game\bin.py", line 210, in <module>
    target.update(dt)
  File "C:\Users\Zyzz\Desktop\game\bin.py", line 98, in update
    self.skip_frame = self.work_time // self.time
TypeError: unsupported operand type(s) for //: 'int' and 'module'
4
  • This has more to do with how you called Animation(..) constructors. Commented Aug 9, 2017 at 13:26
  • I need to change a name of my constructors? Commented Aug 9, 2017 at 13:32
  • Noo... It's how these are called. With the time parameter... Commented Aug 9, 2017 at 13:37
  • No, you need to change how you instantiate them. What are you passing as time when you create Animation? That is the module the error is complaining about, and it would happen regardless of Python version. Of course, it may happen that because of something elsewhere in the code, Python 3 is passing a module where Python 2 was passing an int. Commented Aug 9, 2017 at 13:38

1 Answer 1

2

What I can see in your code it is nothing related to code in python2/python3.

Here self.time = time , time seems to be the imported module.
You are trying self.skip_frame = self.work_time // self.time where self.work_time is initialized with 0 earlier in def __init__(...)
It is trying to do operation between int(0) and a module(time) which is not acceptable.

But as per your question header if you want your code to migrate from python2.x to python3.x compatible there is a package available 2to3
You can install 2to3 using python-tools

$ 2to3 example.py
$ 2to3 -w example.py # -w for write the changes back to file
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.