Skip to content
Advertisement

Is there a way to make a object follow another object?

So, I have been working on a project for a very basic Space Invaders. But, I can’t seem to get the Bullet (see code) class to follow the Jet class(see code):

from tkinter import *
import time as t

class Ufo:
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_polygon(0,0,0,10,20,10,20,0,fill=color)
        self.canvas.move(self.id, 245,50)
        self.x = 0.5
        self.y = 0
        self.canvas_width = self.canvas.winfo_width()

    def draw(self):
        self.canvas.move(self.id,self.x,self.y)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0.5
        if pos[6] >= self.canvas_width:
            self.x = -0.5

class Jet:
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_polygon(0,0,10,-20,20,0,fill=color)
        self.canvas.move(self.id,245,250)
        self.x = 0
        self.canvas_width = self.canvas.winfo_width()
        canvas.bind_all('<KeyPress-Left>',self.turn_left)
        canvas.bind_all('<KeyPress-Right>',self.turn_right)
    def draw(self):
        self.canvas.move(self.id,self.x,0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        if pos[4] >= self.canvas_width:
            self.x = 0

    def turn_left(self,evt):
        self.x = -1
        t.sleep(0.01)
    def turn_right(self,evt):
        self.x = 1
        t.sleep(0.01)

class Bullet:
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_oval(10,10,25,25,fill=color)
        self.canvas.move(self.id,245,250)

    def draw(self):
        self.canvas.move(self.id,1,0)

tk = Tk()
tk.title('Space Invaders')
tk.resizable(0,0)
tk.wm_attributes('-topmost',1)
canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
canvas.pack()
tk.update()
ufo = Ufo(canvas,'gray')
jet = Jet(canvas,'blue')
bullet = Bullet(canvas,'yellow')
while 1:
    ufo.draw()
    jet.draw()
    tk.update_idletasks()
    tk.update()
    t.sleep(0.01)


I have already searched some websites (and yours, too) and couldn’t find anything. Do I have any way to make the classes follow each other?

Advertisement

Answer

You could do it by changing Bullet.draw() to accept an x argument, and pass it the jet object’s x in the loop.

Note I also modified your code so it follows the PEP 8 – Style Guide for Python Code to make it more readable. Strongly suggest you read and start following it.

from tkinter import *
import time as t


class Ufo:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_polygon(0, 0, 0, 10, 20, 10, 20, 0, fill=color)
        self.canvas.move(self.id, 245, 50)
        self.x = 0
        self.y = 0  # ADDED
        self.canvas_width = self.canvas.winfo_width()

    def draw(self):
        self.canvas.move(self.id, self.x, 0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        if pos[4] >= self.canvas_width:
            self.x = 0


class Jet:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_polygon(0,0, 10,-20, 20,0, fill=color)
        self.canvas.move(self.id, 245, 250)
        self.x = 0
        self.canvas_width = self.canvas.winfo_width()
        canvas.bind_all('<KeyPress-Left>', self.turn_left)
        canvas.bind_all('<KeyPress-Right>', self.turn_right)

    def draw(self):
        self.canvas.move(self.id, self.x, 0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        if pos[4] >= self.canvas_width:
            self.x = 0

    def turn_left(self, evt):
        self.x = -1
        t.sleep(0.01)

    def turn_right(self, evt):
        self.x = 1
        t.sleep(0.01)


class Bullet:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
        self.canvas.move(self.id, 245, 250)

    def draw(self, x):
        self.canvas.move(self.id, x, 0)


tk = Tk()
tk.title('Space Invaders')
tk.resizable(0, 0)
tk.wm_attributes('-topmost', 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

ufo = Ufo(canvas, 'gray')
jet = Jet(canvas, 'blue')
bullet = Bullet(canvas, 'yellow')

while 1:
    ufo.draw()
    jet.draw()
    bullet.draw(jet.x)
    tk.update_idletasks()
    tk.update()
    t.sleep(0.01)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement