0

I want to create 2D minecraft test game. Got page with textures and so on... And then, I realized I need to create 25*25 map!

In this code below you will see, that I created 5 functions and 5 labels in for-loop and if you are good at this language you will see I created block breaking animation. I'm going to care about holding animation after(if you know how to create holding animation please let me know)...

Code you can test:

from tkinter import *
tk=Tk()
tk.title('Minecraft 2D')
tk.config(bg='lightblue')
tk.resizable(False, False)
app_width=1500
app_height=750
screen_width=tk.winfo_screenwidth()
screen_height=tk.winfo_screenheight()
x=(screen_width/2)-(app_width/2)
y=(screen_height/2)-(app_height/2)
tk.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
DestroyCounter=0
for i in range(10, 15):
    def Destroy1():
        global DestroyCounter
        if DestroyCounter==0:
            DestroyCounter=1
            GrassBlockSprite.config(bg='gray30')
        elif DestroyCounter==1:
            DestroyCounter=2
            GrassBlockSprite.config(bg='gray26')
        elif DestroyCounter==2:
            DestroyCounter=3
            GrassBlockSprite.config(bg='gray22')
        elif DestroyCounter==3:
            DestroyCounter=4
            GrassBlockSprite.config(bg='gray18')
        elif DestroyCounter==4:
            DestroyCounter=5
            GrassBlockSprite.place_forget()
    GrassBlockSprite=Canvas(tk, width=48, height=48, bg='brown', bd=0)
    GrassBlockSprite.place(relx=((i+0.2)/31.5), rely=0.305)
    GrassBlockSprite.bind('<Button-1>', lambda e:Destroy1())
tk.mainloop()

    

There just last block accepts animation, not other ones. How to make this animation for other blocks? How to make variables and functions that will not overlap such as a loop in a thread? Do I need to create threads? But how to create them for every block in for loop? You can answer with just these 5 blocks in code. You can fix code too if you want.

pppery
  • 3,731
  • 22
  • 33
  • 46
  • this code does not run. needs `tk.mainloop()` at the end. – D.L Apr 17 '22 at 17:06
  • it worked for me but I added it anyways. –  Apr 17 '22 at 17:23
  • function in for loop makes no sense as it writes to the same memory address 5 times. what is the intention of this ? – D.L Apr 17 '22 at 22:01
  • Do I need to create them in threads? –  Apr 18 '22 at 06:09
  • depending in wht you want to do, yes. or name each function differently `Destroy1`, `Destroy2`, `Destroy3` ... `Destroyn`. – D.L Apr 18 '22 at 07:21
  • like this: https://stackoverflow.com/questions/49631178/using-for-loop-to-define-multiple-functions-python , although it would be considered pulluting the namespace. – D.L Apr 18 '22 at 07:32
  • Does this answer your question? [How do you create different variable names while in a loop?](https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop) – Faraaz Kurawle Apr 18 '22 at 08:48
  • You can have a list with the names of the various images, then do `...bg=destroyImages[DestroyCounter]` instead of that silly `if/elif` chain. That would even let you change the number of steps. – Tim Roberts Apr 19 '22 at 23:09
  • And you don't really need a lambda. You can just pass `Destroy1`. It would have to accept the `e` parameter. – Tim Roberts Apr 19 '22 at 23:09

1 Answers1

0

Consider this as a replacement:

animation = ['gray30','gray26','gray22','gray18']

DestroyCounter=[]
GrassBlockSprite=[]

def Destroy(n):
    if DestroyCounter[n] < len(animation):
        GrassBlockSprite[n].config(bg=animation[DestroyCounter[n]])
    else:
        GrassBlockSprite[n].place_forget()
    DestroyCounter[n] += 1

for i in range(5):
    gbs=Canvas(tk, width=48, height=48, bg='brown', bd=0)
    gbs.place(relx=((i+10.2)/31.5), rely=0.305)
    gbs.bind('<Button-1>', lambda e, i=i:Destroy(i))
    GrassBlockSprite.append(gbs)
    DestroyCounter.append(0)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • The next step would be to create a `class Grass` that keeps all of this information. It gets passed a starting X location. Everything else can be internal. – Tim Roberts Apr 19 '22 at 23:38