有没有办法让一个物体跟随另一个物体?

因此,我一直在为一个非常基本的太空侵略者设计项目。但是,我似乎无法让Bullet类(请参见代码)跟随Jet类(请参见代码):

from tkinter import *
import time as t

class Ufo:
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_polygon(0,10,20,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,-20,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,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,color):
        self.canvas = canvas
        self.id = canvas.create_oval(10,25,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)


我已经搜索了一些网站(还有您的网站),却找不到任何东西。我有什么办法可以使各个班级互相跟随吗?

zzz34486199 回答:有没有办法让一个物体跟随另一个物体?

您可以通过更改Bullet.draw()来接受x自变量,并在循环中将jet对象的x传递给它来实现。

请注意,我还修改了您的代码,使其遵循PEP 8 - Style Guide for Python Code,以使其更具可读性。强烈建议您阅读并开始关注它。

from tkinter import *
import time as t


class Ufo:
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_polygon(0,10,20,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,-20,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,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,color):
        self.canvas = canvas
        self.id = canvas.create_oval(10,25,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)
本文链接:https://www.f2er.com/2920485.html

大家都在问