-1
I’m trying to assemble the game of the copper using python, but I came across a problem that completely blocked my code. I used the Tkinter library and all its functions to create the game, follow the code:
import tkinter
import random
from time import sleep
window = tkinter.Tk()
width = 800
height = 600
#FUNÇÃO QUE CRIA O MAPA
def Create_map(width, height):
mapa = tkinter.Canvas(width= width, height= height, bg='black')
mapa.pack()
coordenadas = [10,10]
Create_snake(coordenadas, mapa)
Create_fruits(coordenadas, mapa, width, height)
#FUNÇÃO QUE CRIA A COBRA
def Create_snake(coordenadas, mapa):
coordenadas = coordenadas
snake = mapa.create_rectangle(coordenadas, 30, 30, fill='white')
window.bind("<Up>", Move_Up)
window.bind("<Down>", Move_Down)
window.bind("<Left>", Move_Left)
window.bind("<Right>", Move_Right)
#FUNÇÃO QUE CRIA AS FRUTAS
def Create_fruits(coordenadas, mapa, width, height):
newcoord = [random.randint(10, width - 30), random.randint(10, height - 30)]
fruits = mapa.create_rectangle(newcoord, newcoord[0] + 20, newcoord[1] + 20, fill='pink')
#FUNÇÃO QUE MOVE A COBRA
def Move_Up(key):
print(key)
def Move_Down(key):
print(key)
def Move_Left(key):
print(key)
def Move_Right(key):
print(key)
Create_map(width, height)
window.mainloop()
I am using the functions Move_up, _Down, _Left and _Right to update the position of the snake, but when using bind() I can’t pass any parameters to the functions, that is, I don’t have access to the Snake widget to update the coordinate and, then update the snake’s position. Does anyone have any idea what to do to pass the parameter in the functions they have Move??
When using functions, you can leave the object
snake
as a global, since you will need to update it from several functions; but you can manage this scope also using a class.– Woss
Manage the scope using a class? Any references or links to do this? Thanks!!!
– Guilherme Gomes
To perform functions you must write them the way
foo()
, otherwise the function will not perform. Try:
def foo(s):
 return (s)
print(foo)
print(foo(s))

– Lucas Maraal