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

时间:2019-12-14 21:50:39

标签: python tkinter tkinter-canvas

因此,我一直在为一个非常基本的太空侵略者设计项目。但是,我似乎无法让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,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)


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

1 个答案:

答案 0 :(得分:0)

您可以通过更改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, 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)