问题描述
我如何理解:
在while循环中,我每次迭代都升级窗口,然后调用_snake_move(),它检查是否有按下的按钮(在开始方向是“停止”,所以什么也没有发生),然后如果按下“ w”,则调用go_up(),它将snake.direction更改为“ up”。在循环的下一次迭代中,我们调用_snake_move(),它现在激活条件语句之一,并引起sety(y + 20)来移动蛇。为什么不起作用?
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()
解决方法
您应该将on函数传递给onkey而不是调用它,因此请删除()括号,如下所示:
{
path: ':city',// This is your parent route and parent component which you expect those child components below will be rendered in here. Thus,you need to specify a router-outlet in here.
component: HomeComponent,...
children: [ // This is your child components that will render/reflect it's templates to their parent above or in this case,inside the HomeComponent
{
path: ':category',component: HomeComponent // This will render the category -> home template/component inside the parent's HomeComponent
},{
path: 'search',component: SearchComponent,// this will render the search template/component inside the parent's HomeComponent
}
]
},
,
我不认为您应该在函数中放置onkey
或onclick
。当我在游戏中尝试此操作时,总是会弹出错误消息。