1

I created a dot game where there is randomly chosen a dot and expanded to neighbouring a. Due to the fact that I don't delete the dot,s that get replaced but just paint them over my game gets slower over time.

I tried by deleting everything, but this also deletes the score bar what i.d like to keep through the game. This is my code.

from tkinter import*
from random import*
import time

raam = Tk()
raam.iconbitmap(default='täpimäng.ico')
raam.title("Täpi Mäng")
tahvel = Canvas(raam, width=600, height=600, background="black")
tahvel.grid()
raam.resizable(0,0)
#-----------------Muutujad--------------------------
e=0
lugeja=0
x=10
y=10
xx=20
yy=20
värvid=["red", "blue", "yellow","green","grey","purple","pink","white","aqua","hotpink"]
värvid2=[0]*10
asukoht =[x[:] for x in [[1] * 51] * 51]
#---------------kood----------------------------------
tahvel.create_rectangle(0,0,300,600,fill="blue")

tahvel.create_rectangle(300,0,600,600,fill="red")
tahvel.create_text(200,300,text="uus mäng",fill="white")
tahvel.create_text(400,300,text="jätka",fill="white")
def hiir(event):
    global e
    global salvestatud
    if e==0:
        if event.x<300:
            e=1
            tahvel.delete("all")
            return algus()
        elif event.x>300:
            e=1
            tahvel.delete("all")
            return jätka()
    if e==1:
        if event.x>499 and event.y>499:
            salvestatud=tahvel.create_text(550,400,text="salvestatud",fill="red")
            raam.update()
            return salvesta_mäng()
def algus():
    global x
    global xx
    global y
    global yy
    global värvid
    global värvid2
    global asukoht
    while x<500:
        while y<500:
            a=int(randint(0,9))
            tahvel.create_oval(x,y,xx,yy,fill=värvid[a])
            b=int(x/10)
            c=int(y/10)
            asukoht[b][c]=a
            värvid2[a]+=1
            y+=10
            yy+=10
            raam.update()
        x+=10
        xx+=10
        y=10
        yy=20
        raam.update()
    return värv()
    
def värv():
    global värvid
    global asukoht
    global värvid2
    global lugeja
    d=1
    
    tahvel.create_oval(520,20,530,30,fill="red")
    tahvel.create_oval(520,40,530,50,fill="blue")

    punane=tahvel.create_text(545,25,text=värvid2[0],fill="white")
    sinine=tahvel.create_text(545,45,text=värvid2[1],fill="white")
    
    tahvel.create_oval(520,60,530,70,fill="yellow")
    tahvel.create_oval(520,80,530,90,fill="green")
    
    kollane=tahvel.create_text(545,65,text=värvid2[2],fill="white")
    roheline=tahvel.create_text(545,85,text=värvid2[3],fill="white")

    tahvel.create_oval(520,100,530,110,fill="grey")
    tahvel.create_oval(520,130,530,120,fill="purple")
    
    hall=tahvel.create_text(545,105,text=värvid2[4],fill="white")
    lilla=tahvel.create_text(545,125,text=värvid2[5],fill="white")
    
    tahvel.create_oval(520,140,530,150,fill="pink")
    tahvel.create_oval(520,170,530,160,fill="white")
    
    roosa=tahvel.create_text(545,145,text=värvid2[6],fill="white")
    valge=tahvel.create_text(545,165,text=värvid2[7],fill="white")

    tahvel.create_oval(520,180,530,190,fill="aqua")
    tahvel.create_oval(520,200,530,210,fill="hotpink")
    
    helesinine=tahvel.create_text(545,185,text=värvid2[8],fill="white")
    roosa2=tahvel.create_text(545,205,text=värvid2[9],fill="white")

    while d==1:
        lugeja+=1
        b=int(randint(1,49))
        c=int(randint(1,49))
        if asukoht[b][c]==asukoht[b+1][c] and asukoht[b][c]==asukoht[b-1][c] and asukoht[b][c]==asukoht[b][c+1] and asukoht[b][c]==asukoht[b][c-1]:
            b=50
            c=50
        if b>=49 or b<=1:
            0
        else:
            x=b*10+10
            xx=x+10
            y=c*10
            yy=y+10
            värvid2[asukoht[b+1][c]]-=1
            tahvel.create_oval(x,y,xx,yy,fill=värvid[asukoht[b][c]])
            asukoht[b+1][c]=asukoht[b][c]
            värvid2[asukoht[b][c]]+=1
        if b<=1 or b>=49:
            0
        else:
            x=b*10-10
            xx=x+10
            y=c*10
            yy=y+10
            värvid2[asukoht[b+1][c]]-=1
            tahvel.create_oval(x,y,xx,yy,fill=värvid[asukoht[b][c]])
            asukoht[b-1][c]=asukoht[b][c]
            värvid2[asukoht[b][c]]+=1
        if c>=49 or c<=1:
            0
        else:
            x=b*10
            xx=x+10
            y=c*10-10
            yy=y+10
            värvid2[asukoht[b][c+1]]-=1
            tahvel.create_oval(x,y,xx,yy,fill=värvid[asukoht[b][c]])
            asukoht[b][c-1]=asukoht[b][c]
            värvid2[asukoht[b][c]]+=1
        if c>=49 or c<=1:
            0
        else:
            x=b*10
            xx=x+10
            y=c*10+10
            yy=y+10
            värvid2[asukoht[b][c+1]]-=1
            tahvel.create_oval(x,y,xx,yy,fill=värvid[asukoht[b][c]])
            asukoht[b][c+1]=asukoht[b][c]
            värvid2[asukoht[b][c]]+=1
        raam.update()
        tahvel.delete(punane,sinine,kollane,roheline,hall,lilla,roosa,valge,helesinine,roosa2)
        punane=tahvel.create_text(545,25,text=värvid2[0],fill="white")
        sinine=tahvel.create_text(545,45,text=värvid2[1],fill="white")
        kollane=tahvel.create_text(545,65,text=värvid2[2],fill="white")
        roheline=tahvel.create_text(545,85,text=värvid2[3],fill="white")
        hall=tahvel.create_text(545,105,text=värvid2[4],fill="white")
        lilla=tahvel.create_text(545,125,text=värvid2[5],fill="white")
        roosa=tahvel.create_text(545,145,text=värvid2[6],fill="white")
        valge=tahvel.create_text(545,165,text=värvid2[7],fill="white")
        helesinine=tahvel.create_text(545,185,text=värvid2[8],fill="white")
        roosa2=tahvel.create_text(545,205,text=värvid2[9],fill="white")

        if värvid2[0]==2401:
            d=2
            tahvel.create_text(300,300,text="punane võitis",fill="white")
        if värvid2[1]==2401:
            d=2
            tahvel.create_text(300,300,text="sinine võitis",fill="white")
        if värvid2[2]==2401:
            d=2
            tahvel.create_text(300,300,text="kollane võitis",fill="white")
        if värvid2[3]==2401:
            d=2
            tahvel.create_text(300,300,text="roheline võitis",fill="white")
        if värvid2[4]==2401:
            d=2
            tahvel.create_text(300,300,text="hall võitis",fill="white")
        if värvid2[5]==2401:
            d=2
            tahvel.create_text(300,300,text="lilla võitis",fill="white")
        if värvid2[6]==2401:
            d=2
            tahvel.create_text(300,300,text="hele roosa võitis",fill="white")
        if värvid2[7]==2401:
            d=2
            tahvel.create_text(300,300,text="valge võitis",fill="black")
        if värvid2[8]==2401:
            d=2
            tahvel.create_text(300,300,text="helesinine võitis",fill="white")
        if värvid2[9]==2401:
            d=2
            tahvel.create_text(300,300,text="roosa võitis",fill="white")
        tahvel.create_rectangle(500,500,600,600,fill="red")
        tahvel.create_text(550,550,text="salvesta mäng",fill="white")
        if lugeja==100:
            f = open("täpimäng_asukohad", "w")
            x=1
            y=1
            while x<50:
                while y<50:
                    f.write(str(asukoht[x][y]))
                    y+=1
                f.write("\n")
                y=1
                x+=1
            f.close
            lugeja=0
            tahvel.delete("all")
            return taasta()
def taasta():
    värvid2=[0]*10
    global asukoht
    x=10
    xx=20
    y=10
    yy=20
    while x<500:
        while y<500:
            a=int(asukoht[int(x/10)][int(y/10)])
            tahvel.create_oval(x,y,xx,yy,fill=värvid[a])
            b=int(x/10)
            c=int(y/10)
            värvid2[a]+=1
            y+=10
            yy+=10
            raam.update()
        x+=10
        xx+=10
        y=10
        yy=20
        raam.update()
    return värv()
def jätka():
    global asukoht
    f = open("täpimäng_asukohad", "r")
    x=1
    y=1
    while x<50:
        while y<50:
            asukoht[x][y]=int(f.read(1))
            y+=1
        y=1
        x+=1
        f.read(1)
    f.close
    return taasta()
def salvesta_mäng():
    global asukoht
    time.sleep(0.1)
    tahvel.delete(salvestatud)
    f = open("täpimäng_asukohad", "w")
    x=1
    y=1
    while x<50:
        while y<50:
            f.write(str(asukoht[x][y]))
            y+=1
        f.write("\n")
        y=1
        x+=1
    f.close
tahvel.bind_all("<1>",hiir)
raam.mainloop()
#laua suurus:49*49

Is there a way to delete everything except the scoreboard without having to use names. I'd like to stay to the tkinter and not use any additional libraries.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
ott vain
  • 11
  • 6
  • I am not sure about the "without having to use names" part of your question. Do you want to avoid storing your objects and then explicitly deleting them? (which would be the straight forward way, see e.g. https://stackoverflow.com/a/23692118/9501624). – Christian Karcher Apr 25 '23 at 05:25

0 Answers0