【问题标题】:Turtle not responding to input乌龟对输入没有反应
【发布时间】:2020-07-24 22:50:29
【问题描述】:

我正在尝试使用 python 中的 turtle 模块创建游戏俄罗斯方块,但遇到了一些问题。

以前我遇到过一个问题,即构成我的方形块的一些块会从左向右移动,但不是全部。我意识到问题在于脚本在 for 循环的中间接收玩家的输入,在一些块已经移动后更新了 update_pos() 函数中的位置。

我的解决方案是在更新块的 for 循环运行时禁用玩家输入

scn.onkeypress(None, "a")
scn.onkeypress(None, "d")

但是一旦我这样做了,这些块将不再向左或向右移动。我能猜到的只是玩家输入从未正确重新启用,但我不知道为什么。

import turtle
import time
import random

last_check = time.time()
spawn_pos = (-12.5, 345)
interval = 2.5
x_input, y_input = 0, 0
colors = ["red", "yellow", "green", "blue"]

scn = turtle.Screen()
scn.setup(1280, 720)

brick = turtle.Turtle()
brick.penup()
brick.pencolor("black")
brick.speed(0)
brick.shape("square")
brick.shapesize(1.05, 1.05, 1.5)

def update_pos(current_shape):
    global x_input, y_input
    scn.onkeypress(None, "a")
    scn.onkeypress(None, "d")
    for a in range(4):
        current_shape[a][1].setpos((current_shape[a][1].xcor() + x_input), (current_shape[a][1].ycor() + y_input - 25))
    x_input, y_input = 0, 0
    scn.onkeypress(move_left, "a")
    scn.onkeypress(move_right, "d")


def spawn_square():
    shape = [[(0, 0)], [(25, 0)], [(25, -25)], [(0, -25)]]
    brick.fillcolor("yellow")
    brick.setpos(spawn_pos)
    move_bricks(shape)
    return(shape)

def move_bricks(shape):
    for i in range(4):
        brick.fillcolor(colors[i])
        shape[i].append(brick.clone())
        shape[i][1].setpos((shape[i][1].xcor() + shape[i][0][0]), (shape[i][1].ycor() + shape[i][0][1]))

def move_left():
    global x_input
    x_input -= 25

def move_right():
    global x_input
    x_input += 25

scn.onkeypress(move_left, "a")
scn.onkeypress(move_right, "d")
scn.listen()

current_shape = spawn_square()

while True:
    if time.time() - last_check > interval:
        last_check = time.time()
        if current_shape[3][1].ycor() <= -300:
            y_input = 600
        update_pos(current_shape)

【问题讨论】:

    标签: python tetris python-turtle


    【解决方案1】:

    您的代码存在一些问题:

    • 您不会根据用户输入立即更新绘图,而是等待垂直重绘。所以用户不会得到他们操作结果的反馈。

    • 您正在使用可能锁定事件的计时器循环。 (例如,尝试关闭您的窗口,它会延迟到下一次垂直重绘。)我已将其更改为使用海龟的计时器事件,以便更好地协同工作。

    我已经修改了您的以下代码,以按照您的预期运行:

    from turtle import Screen, Turtle
    
    SPAWN_POS = (-12.5, 370)
    INTERVAL = 1000  # milliseconds
    COLORS = ['red', 'yellow', 'green', 'blue']
    
    x_input, y_input = 0, 0
    
    def update_pos(shape):
        global x_input, y_input
    
        for a in range(4):
            shape[a][1].setpos((shape[a][1].xcor() + x_input), (shape[a][1].ycor() + y_input))
    
        x_input, y_input = 0, 0
    
        screen.update()
    
    def spawn_square():
        shape = [[(0, 0)], [(25, 0)], [(25, -25)], [(0, -25)]]
        move_bricks(shape)
    
        return shape
    
    def move_bricks(shape):
        for i in range(4):
            brick.fillcolor(COLORS[i])
            shape[i].append(brick.clone())
            shape[i][1].setpos((shape[i][1].xcor() + shape[i][0][0]), (shape[i][1].ycor() + shape[i][0][1]))
    
        screen.update()
    
    def move_left():
        global x_input
    
        x_input -= 25
        update_pos(current_shape)
    
    def move_right():
        global x_input
    
        x_input += 25
        update_pos(current_shape)
    
    def drop_bricks():
        global y_input
    
        screen.onkeypress(None, 'a')
        screen.onkeypress(None, 'd')
    
        if current_shape[3][1].ycor() <= -300:
            y_input = 600
        else:
            y_input -= 25
    
        update_pos(current_shape)
    
        screen.onkeypress(move_left, 'a')
        screen.onkeypress(move_right, 'd')
    
        screen.ontimer(drop_bricks, INTERVAL)
    
    screen = Screen()
    screen.setup(1280, 720)
    screen.tracer(False)
    screen.listen()
    
    brick = Turtle()
    brick.penup()
    brick.shape('square')
    brick.shapesize(1.05, 1.05, 1.5)
    brick.setpos(SPAWN_POS)
    
    current_shape = spawn_square()
    
    drop_bricks()
    
    screen.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2020-09-08
      • 1970-01-01
      • 2022-08-02
      • 2019-01-28
      • 1970-01-01
      • 2017-08-09
      • 2020-07-19
      • 2013-07-29
      • 2023-03-20
      相关资源
      最近更新 更多