有没有办法让一个物体跟随另一个物体?
Is there a way to make a object follow another object?
所以,我一直在为非常基础的 Space Invaders 开发一个项目。但是,我似乎无法让 Bullet
(见代码)class 跟随 Jet
class(见代码):
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)
我已经搜索了一些网站(还有你的网站),但没有找到任何内容。我有什么办法让 classes 相互跟随吗?
您可以通过更改 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)
所以,我一直在为非常基础的 Space Invaders 开发一个项目。但是,我似乎无法让 Bullet
(见代码)class 跟随 Jet
class(见代码):
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)
我已经搜索了一些网站(还有你的网站),但没有找到任何内容。我有什么办法让 classes 相互跟随吗?
您可以通过更改 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)