Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.onkey()在turtle、Python中_Python_Turtle Graphics - Fatal编程技术网

.onkey()在turtle、Python中

.onkey()在turtle、Python中,python,turtle-graphics,Python,Turtle Graphics,我的理解是: 在while循环中,每次迭代我都会升级窗口,然后调用_snake_move(),检查是否有按钮被按下(开始方向是“停止”,所以什么都不会发生),然后如果按下“w”,则调用go_up(),将snake.direction更改为“向上”。在循环的下一次迭代中,我们调用_snake_move(),它现在激活一个条件语句并导致sety(y+20),sety应该移动蛇。为什么它不起作用 import turtle class Settings(): def __init

我的理解是:

在while循环中,每次迭代我都会升级窗口,然后调用_snake_move(),检查是否有按钮被按下(开始方向是“停止”,所以什么都不会发生),然后如果按下“w”,则调用go_up(),将snake.direction更改为“向上”。在循环的下一次迭代中,我们调用_snake_move(),它现在激活一个条件语句并导致sety(y+20),sety应该移动蛇。为什么它不起作用

import turtle


class Settings():
    
    def __init__(self):
        
        """ Initialize settings of the game. """
        
        self.window_width = 500
        self.window_height = 500
        self.bgcolor = 'blue'
        self.game_title = 'Reinforced Snake'
        
        self.food = False
        
        self.snake_color = 'red'
        
        
class Reinforced_Snake():
    
    def __init__(self):
        
        """ Initialize classes for the main one. """
        
        # initialize classes
        self.settings = Settings()
        
        # initialize the main screen of the game
        self.window = turtle.Screen()
        
        self.window.bgcolor(self.settings.bgcolor)
        self.window.title(self.settings.game_title)
        self.window.setup(width = self.settings.window_width, 
                     height = self.settings.window_height)
        
        # initialize the snake
        self._init_snake()
        
        # turn off screen updates
        self.window.tracer(0)
        
    def _init_snake(self):
        
        
        """ Initialize the snake instead of creating another class. """
        
        self.snake = turtle.Turtle()
        self.snake.speed(0)
        self.snake.color(self.settings.snake_color)
        
        # so that path is not drawn
        self.snake.penup()
        
        # place the snake and freeze it initially
        self.snake.goto(0, 100)
        self.snake.direction = 'stop'
        
        
        
    def main(self):
        
        """ Main loop. """
        
        while True:
            
            self.window.update()
            self._snake_move()


    def _snake_move(self):
        
        """ Move the snake. """
        
        self.window.listen()
        
        
        self.window.onkey(self.go_up(), "w")
        
        self.window.onkey(self.go_down(), "s")
        
        self.window.onkey(self.go_right(), "d")
        
        self.window.onkey(self.go_left(), "a")
        
        
        if self.snake.direction == "up":
            y = self.snake.ycor() #y coordinate of the turtle
            self.snake.sety(20)
     
        if self.snake.direction == "down":
            y = self.snake.ycor() #y coordinate of the turtle
            self.snake.sety(-20)
     
        if self.snake.direction == "right":
            x = self.snake.xcor() #y coordinate of the turtle
            self.snake.setx(20)
     
        if self.snake.direction == "left":
            x = self.snake.xcor() #y coordinate of the turtle
            self.snake.setx(-20)
        
            
    def go_up(self):
        if self.snake.direction != "down":
            self.snake.direction = "up"
     
    def go_down(self):
        if self.snake.direction != "up":
            self.snake.direction = "down"
     
    def go_right(self):
        if self.snake.direction != "left":
            self.snake.direction = "right"
     
    def go_left(self):
        if self.snake.direction != "right":
            self.snake.direction = "left"
                
            
    
            
if __name__ == '__main__':
    
    snake = Reinforced_Snake()
    snake.main()

您应该将函数传递给onkey,而不是调用它,因此请删除()括号,如下所示:

self.window.onkey(self.go_up, "w")

self.window.onkey(self.go_down, "s")

self.window.onkey(self.go_right, "d")

self.window.onkey(self.go_left, "a")

我认为您不应该在函数中设置
onkey
onclick
。当我在游戏中尝试这一点时,总会出现错误