英文:
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="up"
def declaredown():
global direction
if count==1:
direction="down"
def declareright():
global direction
if count==1:
direction="right"
def declareleft():
global direction
if count==1:
direction="left"
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='green')
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='green')
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='green')
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='green')
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='green')
self.squares.append(self.newsnake)
window.after(100, self.moveleft, event)
window.update()
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')
gamesnake=Snake(canvas,0,0)
window.bind('<Down>',gamesnake.movedown)
window.bind('<Up>',gamesnake.moveup)
window.bind('<Right>',gamesnake.moveright)
window.bind('<Left>',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
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论