@@ -65,93 +65,93 @@ <h4 class="dwnld">Download</h4>
6565 <!-- CODE -->
6666 < pre class ="code ">
6767
68- < span class ="comment "> # This game is from the official documentation of freegames
69- # https://pypi.org/project/freegames/
70-
71- # pip install freegames
72-
73- # Tap on Screen to Fire
74-
75- # import modules </ span >
76- < span class ="in-built "> from</ span > random < span class ="in-built "> import</ span > < span class ="function "> randrange</ span >
77- < span class ="in-built "> import</ span > turtle as t
78- < span class ="in-built "> from</ span > freegames import < span class ="function "> vector</ span >
79-
80- < span class ="comment "> # Set window title, color and icon</ span >
81- t.< span class ="function "> title</ span > ("Canon Fire")
82- < span class ="variable "> root</ span > = t.< span class ="function "> Screen</ span > ()._root
83- < span class ="variable "> root</ span > .< span class ="function "> iconbitmap</ span > ("logo-ico.ico")
84- t.< span class ="function "> bgcolor</ span > ('#99ffbb')
85-
86- < span class ="variable "> ball</ span > = < span class ="function "> vector</ span > (-200, -200)
87- < span class ="variable "> speed</ span > = < span class ="function "> vector</ span > (0, 0)
88- < span class ="variable "> targets</ span > = []
89-
90- < span class ="comment ">
91- # Functions
92- # Respond to screen tap
93- </ span >
94- < span class ="in-built "> def</ span > < span class ="function "> tap</ span > (x, y):
95- < span class ="in-built "> if</ span > not < span class ="function "> inside</ span > (< span class ="variable "> ball</ span > ):
96- < span class ="variable "> ball</ span > .x = -199
97- < span class ="variable "> ball</ span > .y = -199
98- < span class ="variable "> speed</ span > .x = (x + 200) / 25
99- < span class ="variable "> speed</ span > .y = (y + 200) / 25
100-
101- < span class ="comment "> # Return True if xy within screen</ span >
102- < span class ="in-built "> def</ span > < span class ="function "> inside</ span > (xy):
103- return -200 < xy .x < 200 and -200 < xy .y < 200
104-
105- < span class ="comment "> # Draw ball and targets</ span >
106- < span class ="in-built "> def</ span > < span class ="function "> draw</ span > ():
107- t.< span class ="function "> clear</ span > ()
108-
109- < span class ="in-built "> for</ span > < span class ="variable "> target</ span > < span class ="in-built "> in</ span > < span class ="variable "> targets</ span > :
110- t.< span class ="function "> goto</ span > (< span class ="variable "> target</ span > .x, < span class ="variable "> target</ span > .y)
111- t.< span class ="function "> dot</ span > (20, '#8000ff')
112-
113- < span class ="in-built "> if</ span > < span class ="function "> inside</ span > (< span class ="variable "> ball</ span > ):
114- t.< span class ="function "> goto</ span > (< span class ="variable "> ball</ span > .x, < span class ="variable "> ball</ span > .y)
115- t.< span class ="function "> dot</ span > (6, '#cc0000')
116-
117- t.< span class ="function "> update</ span > ()
118-
119- < span class ="comment "> # Move ball and < span class ="variable "> targets</ span > </ span >
120- < span class ="in-built "> def</ span > < span class ="function "> Screen</ span > ():
121- < span class ="in-built "> if</ span > < span class ="function "> randrange</ span > (40) == 0:
122- y = < span class ="function "> randrange</ span > (-150, 150)
123- < span class ="variable "> target</ span > = < span class ="function "> vector</ span > (200, y)
124- < span class ="variable "> targets</ span > .append(< span class ="variable "> target</ span > )
125-
126- < span class ="in-built "> for</ span > < span class ="variable "> target</ span > < span class ="in-built "> in</ span > targets:
127- < span class ="variable "> target</ span > .x -= 0.5
128-
129- < span class ="in-built "> if</ span > < span class ="function "> inside</ span > (< span class ="variable "> ball</ span > ):
130- < span class ="variable "> speed</ span > .y -= 0.35
131- < span class ="variable "> ball</ span > .< span class ="function "> Screen</ span > (< span class ="variable "> speed</ span > )
132-
133- dupe = < span class ="variable "> targets</ span > .copy()
134- < span class ="variable "> targets</ span > .< span class ="function "> clear</ span > ()
135-
136- < span class ="in-built "> for</ span > < span class ="variable "> target</ span > < span class ="in-built "> in</ span > dupe:
137- < span class ="in-built "> if</ span > abs(< span class ="variable "> target</ span > - < span class ="variable "> ball</ span > ) > 13:
138- < span class ="variable "> targets</ span > .append(< span class ="variable "> target</ span > )
139-
140- < span class ="function "> draw</ span > ()
141-
142- < span class ="in-built "> for</ span > < span class ="variable "> target</ span > < span class ="in-built "> in</ span > < span class ="variable "> targets</ span > :
143- < span class ="in-built "> if</ span > not < span class ="function "> inside</ span > (< span class ="variable "> target</ span > ):
144- return
145-
146- t.< span class ="function "> ontimer</ span > (< span class ="function "> Screen</ span > , 50)
147-
148- t.< span class ="function "> setup</ span > (420, 420, 370, 0)
149- t.< span class ="function "> hideturtle</ span > ()
150- t.< span class ="function "> up</ span > ()
151- t.< span class ="function "> tracer</ span > (False)
152- t.< span class ="function "> onscreenclick</ span > (< span class ="function "> tap</ span > )
153- < span class ="function "> Screen</ span > ()
154- t.< span class ="function "> done</ span > ()
68+ < span class ="comment "> # This game is from the official documentation of freegames
69+ # https://pypi.org/project/freegames/
70+
71+ # pip install freegames
72+
73+ # Tap on Screen to Fire
74+
75+ # import modules </ span >
76+ < span class ="in-built "> from</ span > random < span class ="in-built "> import</ span > < span class ="function "> randrange</ span >
77+ < span class ="in-built "> import</ span > turtle as t
78+ < span class ="in-built "> from</ span > freegames import < span class ="function "> vector</ span >
79+
80+ < span class ="comment "> # Set window title, color and icon</ span >
81+ t.< span class ="function "> title</ span > ("Canon Fire")
82+ < span class ="variable "> root</ span > = t.< span class ="function "> Screen</ span > ()._root
83+ < span class ="variable "> root</ span > .< span class ="function "> iconbitmap</ span > ("logo-ico.ico")
84+ t.< span class ="function "> bgcolor</ span > ('#99ffbb')
85+
86+ < span class ="variable "> ball</ span > = < span class ="function "> vector</ span > (-200, -200)
87+ < span class ="variable "> speed</ span > = < span class ="function "> vector</ span > (0, 0)
88+ < span class ="variable "> targets</ span > = []
89+
90+ < span class ="comment ">
91+ # Functions
92+ # Respond to screen tap
93+ </ span >
94+ < span class ="in-built "> def</ span > < span class ="function "> tap</ span > (x, y):
95+ < span class ="in-built "> if</ span > not < span class ="function "> inside</ span > (< span class ="variable "> ball</ span > ):
96+ < span class ="variable "> ball</ span > .x = -199
97+ < span class ="variable "> ball</ span > .y = -199
98+ < span class ="variable "> speed</ span > .x = (x + 200) / 25
99+ < span class ="variable "> speed</ span > .y = (y + 200) / 25
100+
101+ < span class ="comment "> # Return True if xy within screen</ span >
102+ < span class ="in-built "> def</ span > < span class ="function "> inside</ span > (xy):
103+ return -200 < xy .x < 200 and -200 < xy .y < 200
104+
105+ < span class ="comment "> # Draw ball and targets</ span >
106+ < span class ="in-built "> def</ span > < span class ="function "> draw</ span > ():
107+ t.< span class ="function "> clear</ span > ()
108+
109+ < span class ="in-built "> for</ span > < span class ="variable "> target</ span > < span class ="in-built "> in</ span > < span class ="variable "> targets</ span > :
110+ t.< span class ="function "> goto</ span > (< span class ="variable "> target</ span > .x, < span class ="variable "> target</ span > .y)
111+ t.< span class ="function "> dot</ span > (20, '#8000ff')
112+
113+ < span class ="in-built "> if</ span > < span class ="function "> inside</ span > (< span class ="variable "> ball</ span > ):
114+ t.< span class ="function "> goto</ span > (< span class ="variable "> ball</ span > .x, < span class ="variable "> ball</ span > .y)
115+ t.< span class ="function "> dot</ span > (6, '#cc0000')
116+
117+ t.< span class ="function "> update</ span > ()
118+
119+ < span class ="comment "> # Move ball and < span class ="variable "> targets</ span > </ span >
120+ < span class ="in-built "> def</ span > < span class ="function "> Screen</ span > ():
121+ < span class ="in-built "> if</ span > < span class ="function "> randrange</ span > (40) == 0:
122+ y = < span class ="function "> randrange</ span > (-150, 150)
123+ < span class ="variable "> target</ span > = < span class ="function "> vector</ span > (200, y)
124+ < span class ="variable "> targets</ span > .append(< span class ="variable "> target</ span > )
125+
126+ < span class ="in-built "> for</ span > < span class ="variable "> target</ span > < span class ="in-built "> in</ span > targets:
127+ < span class ="variable "> target</ span > .x -= 0.5
128+
129+ < span class ="in-built "> if</ span > < span class ="function "> inside</ span > (< span class ="variable "> ball</ span > ):
130+ < span class ="variable "> speed</ span > .y -= 0.35
131+ < span class ="variable "> ball</ span > .< span class ="function "> Screen</ span > (< span class ="variable "> speed</ span > )
132+
133+ dupe = < span class ="variable "> targets</ span > .copy()
134+ < span class ="variable "> targets</ span > .< span class ="function "> clear</ span > ()
135+
136+ < span class ="in-built "> for</ span > < span class ="variable "> target</ span > < span class ="in-built "> in</ span > dupe:
137+ < span class ="in-built "> if</ span > abs(< span class ="variable "> target</ span > - < span class ="variable "> ball</ span > ) > 13:
138+ < span class ="variable "> targets</ span > .append(< span class ="variable "> target</ span > )
139+
140+ < span class ="function "> draw</ span > ()
141+
142+ < span class ="in-built "> for</ span > < span class ="variable "> target</ span > < span class ="in-built "> in</ span > < span class ="variable "> targets</ span > :
143+ < span class ="in-built "> if</ span > not < span class ="function "> inside</ span > (< span class ="variable "> target</ span > ):
144+ return
145+
146+ t.< span class ="function "> ontimer</ span > (< span class ="function "> Screen</ span > , 50)
147+
148+ t.< span class ="function "> setup</ span > (420, 420, 370, 0)
149+ t.< span class ="function "> hideturtle</ span > ()
150+ t.< span class ="function "> up</ span > ()
151+ t.< span class ="function "> tracer</ span > (False)
152+ t.< span class ="function "> onscreenclick</ span > (< span class ="function "> tap</ span > )
153+ < span class ="function "> Screen</ span > ()
154+ t.< span class ="function "> done</ span > ()
155155
156156 </ pre >
157157
0 commit comments