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):
JavaScript
x
72
72
1
from tkinter import *
2
import time as t
3
4
class Ufo:
5
def __init__(self,canvas,color):
6
self.canvas = canvas
7
self.id = canvas.create_polygon(0,0,0,10,20,10,20,0,fill=color)
8
self.canvas.move(self.id, 245,50)
9
self.x = 0.5
10
self.y = 0
11
self.canvas_width = self.canvas.winfo_width()
12
13
def draw(self):
14
self.canvas.move(self.id,self.x,self.y)
15
pos = self.canvas.coords(self.id)
16
if pos[0] <= 0:
17
self.x = 0.5
18
if pos[6] >= self.canvas_width:
19
self.x = -0.5
20
21
class Jet:
22
def __init__(self,canvas,color):
23
self.canvas = canvas
24
self.id = canvas.create_polygon(0,0,10,-20,20,0,fill=color)
25
self.canvas.move(self.id,245,250)
26
self.x = 0
27
self.canvas_width = self.canvas.winfo_width()
28
canvas.bind_all('<KeyPress-Left>',self.turn_left)
29
canvas.bind_all('<KeyPress-Right>',self.turn_right)
30
def draw(self):
31
self.canvas.move(self.id,self.x,0)
32
pos = self.canvas.coords(self.id)
33
if pos[0] <= 0:
34
self.x = 0
35
if pos[4] >= self.canvas_width:
36
self.x = 0
37
38
def turn_left(self,evt):
39
self.x = -1
40
t.sleep(0.01)
41
def turn_right(self,evt):
42
self.x = 1
43
t.sleep(0.01)
44
45
class Bullet:
46
def __init__(self,canvas,color):
47
self.canvas = canvas
48
self.id = canvas.create_oval(10,10,25,25,fill=color)
49
self.canvas.move(self.id,245,250)
50
51
def draw(self):
52
self.canvas.move(self.id,1,0)
53
54
tk = Tk()
55
tk.title('Space Invaders')
56
tk.resizable(0,0)
57
tk.wm_attributes('-topmost',1)
58
canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
59
canvas.pack()
60
tk.update()
61
ufo = Ufo(canvas,'gray')
62
jet = Jet(canvas,'blue')
63
bullet = Bullet(canvas,'yellow')
64
while 1:
65
ufo.draw()
66
jet.draw()
67
tk.update_idletasks()
68
tk.update()
69
t.sleep(0.01)
70
71
72
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.
JavaScript
1
79
79
1
from tkinter import *
2
import time as t
3
4
5
class Ufo:
6
def __init__(self, canvas, color):
7
self.canvas = canvas
8
self.id = canvas.create_polygon(0, 0, 0, 10, 20, 10, 20, 0, fill=color)
9
self.canvas.move(self.id, 245, 50)
10
self.x = 0
11
self.y = 0 # ADDED
12
self.canvas_width = self.canvas.winfo_width()
13
14
def draw(self):
15
self.canvas.move(self.id, self.x, 0)
16
pos = self.canvas.coords(self.id)
17
if pos[0] <= 0:
18
self.x = 0
19
if pos[4] >= self.canvas_width:
20
self.x = 0
21
22
23
class Jet:
24
def __init__(self, canvas, color):
25
self.canvas = canvas
26
self.id = canvas.create_polygon(0,0, 10,-20, 20,0, fill=color)
27
self.canvas.move(self.id, 245, 250)
28
self.x = 0
29
self.canvas_width = self.canvas.winfo_width()
30
canvas.bind_all('<KeyPress-Left>', self.turn_left)
31
canvas.bind_all('<KeyPress-Right>', self.turn_right)
32
33
def draw(self):
34
self.canvas.move(self.id, self.x, 0)
35
pos = self.canvas.coords(self.id)
36
if pos[0] <= 0:
37
self.x = 0
38
if pos[4] >= self.canvas_width:
39
self.x = 0
40
41
def turn_left(self, evt):
42
self.x = -1
43
t.sleep(0.01)
44
45
def turn_right(self, evt):
46
self.x = 1
47
t.sleep(0.01)
48
49
50
class Bullet:
51
def __init__(self, canvas, color):
52
self.canvas = canvas
53
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
54
self.canvas.move(self.id, 245, 250)
55
56
def draw(self, x):
57
self.canvas.move(self.id, x, 0)
58
59
60
tk = Tk()
61
tk.title('Space Invaders')
62
tk.resizable(0, 0)
63
tk.wm_attributes('-topmost', 1)
64
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
65
canvas.pack()
66
tk.update()
67
68
ufo = Ufo(canvas, 'gray')
69
jet = Jet(canvas, 'blue')
70
bullet = Bullet(canvas, 'yellow')
71
72
while 1:
73
ufo.draw()
74
jet.draw()
75
bullet.draw(jet.x)
76
tk.update_idletasks()
77
tk.update()
78
t.sleep(0.01)
79