Python Forum
Moving with objects simultaneously - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Moving with objects simultaneously (/thread-17691.html)



Moving with objects simultaneously - kom2 - Apr-20-2019

Hello,

I'm currently working on my project and stucked at this point:

I want to create a canvas, where different objects will move (a lot of game NPCs). But whilst they are moving, I want to be able to do a different things in canvas, like buying items for a game and so on. And the point I got stuck in makes me feel that the way how do I want to let canvas do different things simultaneously is quiet wrong.

class Class1:

    def __init__(self):
        self.canvas = tkinter.Canvas(width=500, height=500, bg="white")
        self.canvas.pack()
        number = x
        self.create_npc(number)
        ...

    def create_npc(self):
        for i in range(number):
            self.canvas.after(3000)
            self.npc = NPC(self)

    ...

class NPC:

    def __init__(self, class1):
        self.main = class1
        self.place_npc()
        self.move_npc()

    def place_npc(self):
        self.object_npc = self.main.canvas.create_image(some_x, some_y, image=self.main.cars['self.car_image_1']) #there is a dict with all car images

    def move_npc(self):
        while True:
            self.main.canvas.move(self.object, 10, 0)
So I want to let this object move, after it is created in 'def create_npc' as one of 'number' NPCs. But instead, it creates just one NPC, and actually my tkinter canvas never opens, because while True is still running. Can you help me please? Is there any way how could I say "donĀ“t care this method is running, just continue with executing next lines of program"?


RE: Moving with objects simultaneously - SheeppOSU - Apr-20-2019

use multi-threading
https://www.youtube.com/watch?v=PJ4t2U15ACo&t=490s