tkinter - python-how to make an object 'suicide'? -


when created object in tkinter, how can make object destroy embedded command in object? code this; i'm coding game collects coins want make coins disappear after collecting them.

from tkinter import* import time import random import math color=['gray','skyblue','orange','green','yellow','blue'] score=[0,0,0,0,0,0] class player:     //player class. wasd keys class coin:     def __init__(self,canvas,player,i):         self.color=color[i]         self.canvas=canvas         self.player=player             self.id=canvas.create_rectangle(10,10,20,15,fill=self.color,state='normal',width=0)         self.canvas_width=self.canvas.winfo_width()         self.canvas_height=self.canvas.winfo_height()         self.x=random.randint(0,self.canvas_width-50)         self.y=random.randint(0,self.canvas_height-50)         self.canvas.move(self.id,self.x,self.y)         self.i=i         self.getc=false      def draw(self):         self.canvas.move(self.id,self.x,self.y)         pos=self.canvas.coords(self.id)         if pos[1]<=0:             lv=random.randint(1,3)             self.y=lv*self.i         if pos[3]>=self.canvas_height:             lv=random.randint(-3,-1)             self.y=lv*self.i         if pos[0]<=0:             lh=random.randint(1,3)             self.x=lh*self.i         if pos[2]>=self.canvas_width:             lh=random.randint(-3,-1)             self.x=lh*self.i         if self.hit_player(pos)==true:             self.getc=true      def hit_player(self,pos):         player_pos=self.canvas.coords(self.player.id)         if pos[2]>=player_pos[0] , pos[0] <=player_pos[2]:             if pos[3]>=player_pos[1] , pos[1]<= player_pos[3]:                 return true         return false      def scoring(self):         if self.getc==true:             score[0]=round(score[0]+0.01*pow(2,self.i),2)             score[self.i]=round(score[self.i]+0.01,2)             self.getc=false tk=tk() tk.title("game") tk.resizable(0,0) tk.wm_attributes("-topmost",1)  canvas=canvas(tk,width=500,height=400,bd=0,highlightthickness=0) canvas.pack() tk.update()  player=player(canvas,'blue') coin=[] k in range(50):     l=6-int(math.log2(random.randint(2,32)))     coin.append(coin(canvas,player,l)) inv=canvas.create_text(250,200,text=score,state='hidden') while 1:     if player.toggle==true:         canvas.itemconfig(inv,state='normal')     if player.toggle==false:         canvas.itemconfig(inv,state='hidden')     k in range(50):         coin[k].scoring()         coin[k].draw()     canvas.itemconfig(inv,text=score)     if player.q==true:         break     player.draw()     tk.update_idletasks()     tk.update()     time.sleep(0.01) 

here simple example coins disappear when player click on them:

from tkinter import tk, canvas random import random  root = tk() can = canvas(root, width=400, height=400, bg="white") can.pack()  def collect(id):     can.delete(id)  in range(10):     x = random()*380     y = random()*380     id=can.create_oval(x, y, x+20, y+20, fill="yellow")     can.tag_bind(id, "<button-1>", lambda event, j=id: collect(j))  root.mainloop() 

Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -