PYTHON - How to pass a parameter?

Asked

Viewed 128 times

-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.

  • Manage the scope using a class? Any references or links to do this? Thanks!!!

  • To perform functions you must write them the way foo(), otherwise the function will not perform. Try: &#xA;def foo(s):&#xA; return (s)&#xA;print(foo)&#xA;print(foo(s))&#xA;

1 answer

0

One solution is to create a function that calls its other function; so the parameter you want to pass will be captured by the scope of the function (known as closure):

snake = mapa.create_rectangle(coordenadas, 30, 30, fill='white')
def chama_move_up()
    Move_Up(snake)
window.bind("<Up>", chama_move_up)


def Move_Up(s):
    # .... s é a snake ....

Another solution is to use classes, and put these objects as instance attributes self; so they are accessible in any class method:

class Jogo:
    # ....
    def create_snake(self, coordenadas, mapa):
        self.snake = mapa.create_rectangle(coordenadas, 30, 30, fill='white')
        window.bind("<Up>", self.move_up)
        ...

    def move_up(self):
        # aqui pode usar self.snake

Browser other questions tagged

You are not signed in. Login or sign up in order to post.