有没有办法在 Python 中的另一个函数内终止一个函数

huangapple go评论69阅读模式
英文:

is there a way to terminate a function inside another function in python

问题

以下是您要翻译的代码部分:

count = 1
from time import *
direction = None
eaten = False
WIDGET_SIZE = 40
def declare_up():
    global direction
    if count == 1:
        direction = "up"
def declare_down():
    global direction
    if count == 1:
        direction = "down"
def declare_right():
    global direction
    if count == 1:
        direction = "right"
def declare_left():
    global direction
    if count == 1:
        direction = "left"

class Snake():
    def __init__(self, canvas, topx, topy):
        self.canvas = canvas
        self.topx = topx
        self.topy = topy
        self.bottomx = WIDGET_SIZE
        self.bottomy = WIDGET_SIZE
        self.snake = self.canvas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill='green')
        self.squares = [self.snake]
    def move_down(self, event):
        global eaten
        self.topy += 40
        self.bottomy += 40
        if not eaten:
            self.canvas.delete(self.squares[-1])
        self.new_snake = self.canvas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill='green')
        self.squares.append(self.new_snake)
        window.after(100, self.move_down, event)
        window.update()
    def move_up(self, event):
        global eaten
        self.topy -= 40
        self.bottomy -= 40
        if not eaten:
            self.canvas.delete(self.squares[-1])
        self.new_snake = self.canvas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill='green')
        self.squares.append(self.new_snake)
        window.after(100, self.move_up, event)
        window.update()
    def move_right(self, event):
        global eaten
        self.topx += 40
        self.bottomx += 40
        if not eaten:
            self.canvas.delete(self.squares[-1])
        self.new_snake = self.canvas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill='green')
        self.squares.append(self.new_snake)
        window.after(100, self.move_right, event)
        window.update()
    def move_left(self, event):
        global eaten
        self.topx -= 40
        self.bottomx -= 40
        if not eaten:
            self.canvas delete(self.squares[-1])
        self.new_snake = self.canvas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill='green')
        self.squares.append(self.new_snake)
        window.after(100, self.move_left, event)

from tkinter import *
window = Tk()

window.geometry('520x520')
window.config(background='black')
window.resizable(False, False)
canvas = Canvas(window, width=520, height=520, bg='black')
game_snake = Snake(canvas, 0, 0)

window.bind('<Down>', game_snake.move_down)
window.bind('<Up>', game_snake.move_up)
window.bind('<Right>', game_snake.move_right)
window.bind('<Left>', game_snake.move_left)
canvas.pack()
window.mainloop()

希望这有所帮助。

英文:

I am making Snake and I want it to move in the x direction and in the y direction (separately).

I made a class and gave it a method of move up, down, and left, if I bind the window with the up arrow key the function up should work and I did the same with all the other arrows, but it turns out if I press up then down, the function up is working at the same time as down.

Is there a way or me to stop the function down when I press the up arrow key?

count=1
from time import *
direction=None
eaten=False
WIDGIT_SIZE=40
def declareup():
global direction
if count==1:
direction=&quot;up&quot;
def declaredown():
global direction
if count==1:
direction=&quot;down&quot;
def declareright():
global direction
if count==1:
direction=&quot;right&quot;
def declareleft():
global direction
if count==1:
direction=&quot;left&quot;
class Snake():
def __init__(self,canvas,topx,topy):
self.cavas=canvas
self.topx=topx
self.topy=topy
self.bottomx=WIDGIT_SIZE
self.bottomy=WIDGIT_SIZE
self.snake=self.cavas.create_rectangle(self.topx,self.topy,self.bottomx,self.bottomy,fill=&#39;green&#39;)
self.squares = [self.snake]
def movedown(self,event):
global eaten
self.topy+=40
self.bottomy+=40
if not eaten:
self.cavas.delete(self.squares[-1])
self.newsnake = self.cavas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill=&#39;green&#39;)
self.squares.append(self.newsnake)
window.after(100, self.movedown, event)
window.update()
def moveup(self,event):
global eaten
self.topy-=40
self.bottomy-=40
print(self.topy,self.bottomy)
if not eaten:
self.cavas.delete(self.squares[-1])
self.newsnake = self.cavas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill=&#39;green&#39;)
self.squares.append(self.newsnake)
window.after(100,self.moveup,event)
window.update()
def moveright(self,event):
global eaten
self.topx+=40
self.bottomx+=40
if not eaten:
self.cavas.delete(self.squares[-1])
self.newsnake = self.cavas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill=&#39;green&#39;)
self.squares.append(self.newsnake)
window.after(100, self.moveright, event)
window.update()
def moveleft(self,event):
global eaten
self.topx-=40
self.bottomx-=40
if not eaten:
self.cavas.delete(self.squares[-1])
self.newsnake = self.cavas.create_rectangle(self.topx, self.topy, self.bottomx, self.bottomy, fill=&#39;green&#39;)
self.squares.append(self.newsnake)
window.after(100, self.moveleft, event)
window.update()
from tkinter import *
window=Tk()
window.geometry(&#39;520x520&#39;)
window.config(background=&#39;black&#39;)
window.resizable(False,False)
canvas=Canvas(window,width=520,height=520,bg=&#39;black&#39;)
gamesnake=Snake(canvas,0,0)
window.bind(&#39;&lt;Down&gt;&#39;,gamesnake.movedown)
window.bind(&#39;&lt;Up&gt;&#39;,gamesnake.moveup)
window.bind(&#39;&lt;Right&gt;&#39;,gamesnake.moveright)
window.bind(&#39;&lt;Left&gt;&#39;,gamesnake.moveleft)
canvas.pack()
window.mainloop()

答案1

得分: 0

不,你不能也不应该终止函数。这是因为你在键事件处理程序中安排了函数,并且它们会累积。

所以,像这样做。

...
class Snake():
    def __init__(self,canvas,topx,topy):
        ...
        self.next_action = None
        def update():
            if self.next_action:
                self.next_action(None)
            window.after(100, update)
        update()

    def movedown(self,event):
        ...
        self.squares.append(self.newsnake)
        window.update()
        self.next_action = self.movedown

    def moveup(self,event):
        ...
        self.squares.append(self.newsnake)
        window.update()
        self.next_action = self.moveup
...
英文:

No you can't and shouldn't terminate functions. It's because you scheduled functions in the key event handlers and they accumulates.

So, do like this.

...
class Snake():
def __init__(self,canvas,topx,topy):
...
self.next_action = None
def update():
if self.next_action:
self.next_action(None)
window.after(100, update)
update()
def movedown(self,event):
...
self.squares.append(self.newsnake)
window.update()
self.next_action = self.movedown
def moveup(self,event):
...
self.squares.append(self.newsnake)
window.update()
self.next_action = self.moveup
...

huangapple
  • 本文由 发表于 2023年2月10日 15:51:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定