"后"无限循环:永远不会进入主循环 [英] "after" looping indefinitely: never entering mainloop

查看:164
本文介绍了"后"无限循环:永远不会进入主循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章。在两个月前考虑职业互换时,我开始编写代码,正在研究俄罗斯方块克隆。我已经实现了大部分的核心功能,但是无法让游戏持续刷新一个循环。

This is my first post. I started coding when considering a career swap two months ago and am working on a Tetris clone. I've implemented most of the core features, but cannot get the game to refresh continually with an after loop.

我正在使用Tkinter生成我的Gui,并正在尝试面向事件的程序设计。

I'm using Tkinter to produce my Gui and am trying out event oriented programming.

我的理解是 after(Time,Event) Tkinter 应该安排任何事件回调函数在由 Time 指定的延迟之后发生。我认为这段代码应该继续执行后续的项目。

My understanding is that after(Time, Event) from Tkinter should schedule whatever the Event callback function is to occur after a delay specified by Time. I think that the code is supposed to continue executing subsequent items after this.

我的框架刷新功能( game.updateBoard())为tetris工作提供了大部分必要的事件,然后使用后调用自身。初始化游戏实例时,我会打电话一次。

My frame refresh function (game.updateBoard()) does most of the necessary events for tetris to work, then calls itself using after. I call it once when initializing an instance of the game.

而不是继续到 mainloop() game.updateboard() code>函数通过无限期后调用。

Instead of proceeding to mainloop(), the game.updateboard() function calls itself via after indefinitely.

我怀疑在之后 c $ c>之前,我认为这是不行的,这将是继续执行脚本,直到指定的延迟发生。我认为正在等待回调终止才能继续。

I suspect that it is not behaving how I thought after worked which would be to continue to execute the script until the specified delay occurs. I think it is waiting for the callback to terminate to continue.

我试图找到一个资源,但不能。

I tried to find a resource on this but could not.

如果您有解决这个问题的建议,附加的代码,或编码,一般来说,我很高兴听到他们!这是一个学习过程,我乐意尝试几乎任何您建议的内容。

If you have suggestions for fixing this question, the attached code, or for coding in general, I am very happy to hear them! This is a learning process and I'll gladly try pretty much anything you suggest.

以下是代码的相关部分:

Here is the relevant portion of the code:

class game():
    def __init__(self): #Set up board and image board
        self.pieces = ["L","J","S","Z","T","O","I"]
        self.board = boardFrame()
        self.root = Tk()
        self.root.title("Tetris")
        self.root.geometry("250x525")

        self.frame = Frame(self.root)

        #set up black and green squares for display
        self.bSquare = "bsquare.gif"
        self.gSquare = "square.gif"
        self.rSquare = "rsquare.gif"
        self.image0 = PhotoImage(file = self.bSquare)
        self.image1 = PhotoImage(file = self.gSquare)
        self.image2 = PhotoImage(file = self.rSquare)

        #get an initial piece to work with
        self.activeBlock = piece(self.pieces[random.randint(0,6)])

        #Tells program to lower block every half second
        self.blockTimer = 0
        self.updateBoard()

        self.root.bind('<KeyPress-Up>', self.turn)
        self.root.bind('<KeyPress-Right>', self.moveR)
        self.root.bind('<KeyPress-Left>', self.moveL)
        self.root.bind('<KeyPress-Down>',self.moveD)
        print("Entering mainloop")
        self.root.mainloop()

    def turn(self, event):
        self.activeBlock.deOccupy(self.board)
        self.activeBlock.turn()
        self.activeBlock.occupy(self.board)
        self.drawGrid(self.board.grid)

    def moveR(self, event):
        self.activeBlock.deOccupy(self.board)
        self.activeBlock.updatePos([1,0], self.board)
        self.activeBlock.occupy(self.board)
        self.drawGrid(self.board.grid)

    def moveL(self, event):
      if self.activeBlock.checkLeft(self.board) == False:
        self.activeBlock.deOccupy(self.board)
        self.activeBlock.updatePos([-1,0], self.board)
        self.activeBlock.occupy(self.board)
        self.drawGrid(self.board.grid)

    def moveD(self, event): #find
        self.activeBlock.deOccupy(self.board)
        self.activeBlock.updatePos([0,-1],self.board)
        if self.activeBlock.checkBottom(self.board) == True:
            self.activeBlock.occupy(self.board)
            self.activeBlock = piece(self.pieces[random.randint(0,6)])
##            self.activeBlock = piece(self.pieces[1])
            print("bottomed")
            self.activeBlock.occupy(self.board)

        self.activeBlock.occupy(self.board)
        self.drawGrid(self.board.grid)

    def drawGrid(self, dGrid):

        #Generate squares to match tetris board
        for widget in self.frame.children.values():
            widget.destroy()

        self.activeBlock.occupy(self.board)

        for x in range(9,-1,-1):
            for y in range(20,-1,-1):
                if self.board.grid[x][y] == 1:
                    self.frame.displayA = Label(self.frame, image=self.image1)
##                    self.frame.displayA.image = self.image1
                    self.frame.displayA.grid(row=21-y, column=x)


                else:
                    self.frame.displayA = Label(self.frame, image = self.image0)
##                    self.frame.displayA.image = self.image0
                    self.frame.displayA.grid(row=21-y, column=x)

        self.frame.displayA = Label(self.frame, image = self.image2)
        self.frame.displayA.grid(row = 21 - self.activeBlock.center[1], column = self.activeBlock.center[0])

        self.frame.grid()

    def updateBoard(self):
        self.blockTimer += 1
        "print updateBoard Loop"

        ## 1)check for keyboard commands
        #1.1 move block by keyboard commands
        #2) see if block has bottomed out, if it has, have it enter itself into the grid and generate a new block.
        if self.activeBlock.checkBottom(self.board) == True:
            self.activeBlock.occupy(self.board)
            self.activeBlock = piece(self.pieces[random.randint(0,6)])
            print("bottomed")
            self.activeBlock.occupy(self.board)

        #2.2 - if block has not bottomed and 50 frames (~.5 seconds) have passed, move the active block down a square after clearing its old space. 
        elif self.blockTimer%12 == 0:
            self.activeBlock.deOccupy(self.board)
            self.activeBlock.updatePos([0,-1], self.board)
            self.activeBlock.occupy(self.board)


    ## 4) check for filled rows
        for y in range(1,21):
            for x in range(10):
                rowFull = True
                if self.board.grid[x][y] == 0:
                    rowFull == False  
            #4.1 if any row is filled, delete it and then move all rows above the deleted row down by one
            if rowFull == True:
                for x2 in range(10):
                    self.board.grid[x2][y] = 0
                    for y2 in range(y+1,21):
                        if self.board.grid[x2][y2] == 1:
                            self.board.grid[x2][y2] = 0
                            self.board.grid[x2][y2-1] = 1

            #4.11 if the row is full and the row above it was full, delete the row again as well as the row above it, and move all rows down by 2
                for x in range(10):
                    rowFull = True
                    if self.board.grid[x][y] == 0:
                        rowFull == False
                        if rowFull == True:
                            for x2 in range(10):
                                try:
                                    self.board.grid[x2][y] = 0
                                    self.board.grid[x2][y+1] = 0
                                except:
                                    pass
                                for y2 in range(y+2,21):
                                    try:
                                        if self.board.grid[x2][y2] == 1:
                                            self.board.grid[x2][y2] = 0
                                            self.board.grid[x2][y2-2] = 1
                                    except:
                                        pass


        #5) if there is a block in the top row, end the game loop
        for x in range(10):
            if self.board.grid[x][20] == 1:
                game = "over"


        #6) update image
        self.activeBlock.occupy(self.board)
        self.drawGrid(self.board.grid)
        self.frame.after(500, self.updateBoard())


Game = game()


推荐答案

你想做 self.frame.after(500,self.updateBoard)

这里的区别是( self.updateBoard 而不是 self.updateBoard())。在您的版本中,您将的结果传递到之后的而不是传递函数。这导致您描述的无限递归。

The difference here is subtle, (self.updateBoard instead of self.updateBoard()). In your version, you're passing the result of your function to the after method instead of passing the function. This results in the infinite recursion that you described.

这篇关于&QUOT;后&QUOT;无限循环:永远不会进入主循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆