How would I remove the clouds from the canvas after the red ray hits? Also inste
ID: 656495 • Letter: H
Question
How would I remove the clouds from the canvas after the red ray hits? Also instead of the Clouds if I want to import a picture from google how would I do it?
from Spotter import Spotter
from Cloud import Cloud
from Tkinter import tkinter, Tk, StringVar, Button, Entry
from random import randint
import time
import math
class Cloudspotting (Spotter):
# create the buttons
def __init__(self, root):
Spotter.__init__(self, root)
# the result binding variable for entry
self.result = StringVar()
self.result.set('')
# create the buttons and result entry
self.startBtn = Button(self.frame, text='Start', command=self.start)
self.stopBtn = Button(self.frame, text='Stop', command=self.stop)
self.clearBtn = Button(self.frame, text='Clear', command=self.clear)
self.resultEntry = Entry(self.frame, textvariable=self.result, state='readonly')
# display the buttons and entry on the frame
self.startBtn.grid(row=2, column=1)
self.stopBtn.grid(row=2, column=2)
self.clearBtn.grid(row=2, column=3)
self.resultEntry.grid(row=2, column=4, columnspan=2)
# clouds
self.clouds = None
# start the cloud spotting, create a bunch of clouds
# and put them onto the canvas
def start(self):
if not self.clouds:
self.clouds = []
# create a random number of clouds
num = 10
for i in range(num):
# create the cloud thread
c = Cloud(400, 200)
# create the cloud GUI (circle)
circle = self.canvas.create_oval(c.x - c.radius, c.y - c.radius, c.x + c.radius, c.y + c.radius, fill='blue')
self.clouds.append((c, circle))
# register the gui component for the circle to update
c.registerGUI(self.canvas, circle)
c.start()
Cloud.paused = False
pass
# stop the animation
def stop(self):
Cloud.paused = True
# stop the animation and clear all clouds
def clear(self):
Cloud.paused = True
for thread, circle in self.clouds:
self.canvas.delete(circle)
thread.life = 0
Cloud.paused = False
self.clouds = None
self.result.set('')
# override the up key handler to count the intersect clouds
def upKey(self, event):
Spotter.upKey(self, event)
# no clouds yet
if not self.clouds: return
Cloud.paused = True
time.sleep(0.1)
count = 0
# check each clouds
for thread, circle in self.clouds:
if thread.life > 0:
# check if intersect with the ray
x, y = thread.x, thread.y
# adjust the x, y with the offset so the origin
# is in the center bottom.
x -= 400 / 2
y = 200 - 10 - y
# calculate the distance
dist = abs(math.sin(self.angle) * x - math.cos(self.angle) * y)
if dist <= thread.radius:
count += 1
self.result.set('%d' % (count))
def main():
root = Tk()
sp = Cloudspotting(root)
root.mainloop()
if __name__ == '__main__':
main()
Explanation / Answer
Import picture
import io
import base64
from Tkinter import tkinter,Tk
from urllib2 import urlopen
except ImportError:
root = Tk()
width = 520
height = 320
xCor = 80
yCor = 100
root.geometry("%dx%d+%d+%d" % (width, height, xCor, yCor))
image_url =
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.