因此,我一直在为一个非常基本的太空侵略者设计项目。但是,我似乎无法让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)
我已经搜索了一些网站(还有您的网站),却找不到任何东西。我有什么办法可以使各个班级互相跟随吗?