Reputation: 3
I'm just working on a new project in Python with Tkinter. I'm a newbie in Python, so I need some help. I want to create 2D array and show it in a window.
Here is my code:
maze =[[player(), invisblock(), invisblock(), invisblock()],
[invisblock(),invisblock(),invisblock(), invisblock()],
[invisblock(), invisblock(), invisblock(), invisblock()],
[invisblock(), invisblock(), invisblock(), invisblock()], ]
Player looks like:
def player():
block = tkinter.Label(window)
block.image = tkinter.PhotoImage(file="down.png")
block['image'] = block.image
block.config(text='karel_down')
return block
And printing like:
def printt():
for i, block_row in enumerate(map):
for j, block in enumerate(block_row):
block.grid(row=i, column=j)
window.update()
window.after(250)
Everything works fine, but I have a small problem. I want to make another function like this (this is not functional right now):
def create_world(row,col):
for i in range(row):
for j in range(col):
maze[i][j] = invisblock()
So I want to fill all my maze with row and col from create_world with invisblock(). Example: create_world(10,10) and will create maze 10x10 invisblock(). I don't want to fill manually like in my first code sample (maze =[[player()...) but with a function. In short, I want to create the initial array of objects "smarter". Thanks!!
Upvotes: 0
Views: 528
Reputation: 2001
You can use list comprehensions:
def create_world(row,col):
return [[invisiblock() for i in range(col)] for j in range(row)]
maze = create_world(10, 10)
Upvotes: 2