【问题标题】:Trying to move paddle up and down试图上下移动桨
【发布时间】:2022-06-17 18:37:13
【问题描述】:

我正在制作一个单人乒乓球游戏,除了上下移动球拍外,我什么都有,我真的不明白怎么做,谁能帮忙。是与“pos”有关还是与行的语法有关。它控制桨的运动的部分是def move paddle

import tkinter

# steps
# ball diagonal
# paddle draw
# paddle animation with keyboard (right/left) -> challenge up/down
# collisions (if time)

# graphic parameters
canvas_width = 400
canvas_height = 500
ball_size = 30
timer_refresh = 20
paddle_width = 100
paddle_height = 20

# ball movement
y_move = 2
x_move = 2
# paddle movement
paddle_speed = 6

# game_state
game_running = True

def end_game():
    global game_running

    game_running = False
    canvas.create_text(canvas_width/2, canvas_height/2, text="you lost!")

# move paddle when key is pressed
def move_paddle(event):
    key_symbol = event.keysym
    print(key_symbol)
    pos = canvas.coords(paddle)
    left = pos[0]
    right = pos[2]
    up = pos[1]
    down = pos[3]
    if key_symbol == "Left" and left > 0:
        canvas.move(paddle, -paddle_speed, 0)
    elif key_symbol == "Right" and right <= canvas_width:
        canvas.move(paddle, paddle_speed, 0)
    # move paddle up
    elif key_symbol == "Up" and up >= 0:
        canvas.move(paddle, paddle_speed, 0)
    # move paddle down
    elif key_symbol == "Down" and down <= canvas_width:
        canvas.move(paddle, paddle_speed, 0)*

def collision(ball_pos):
    overlap_result = canvas.find_overlapping(ball_pos[0],ball_pos[1],ball_pos[2],ball_pos[3])
    if paddle in overlap_result:
        return True;
    else:
        return False;


# draw/move ball
def draw():
    global y_move, x_move
    canvas.move(ball1, x_move, y_move)
    pos = canvas.coords(ball1)
    top_y = pos[1]
    bottom_y = pos[3]
    left = pos[0]
    right = pos[2]
    if top_y <= 0:
        y_move = -y_move
    elif bottom_y >= canvas_height-5:
        y_move = -y_move
        end_game()
    # did I hit left or right wall?
    elif left <= 0 or right >= canvas_width-5:
        x_move = -x_move

    # did I collide with the paddle? if so bounce vertically
    if collision(pos):
        y_move = -y_move

# animation timer
def master_timer():
    # draw/move ball
    draw()
    # tkinter processing
    tk.update_idletasks()
    tk.update()
    if game_running:
        tk.after(timer_refresh, master_timer)

tk = tkinter.Tk()
tk.title("Simplified Pong")
# block resizing window
tk.resizable(0,0)

# drawing the canvasd
canvas = tkinter.Canvas(tk, width=canvas_width, height=canvas_height, bd=0, highlightthickness=0)
canvas.pack()
ball1 = canvas.create_oval(0, 0, ball_size, ball_size, fill="red")
canvas.move(ball1, canvas_width/2, canvas_height/2)

paddle = canvas.create_rectangle(0,0,paddle_width, paddle_height, fill="black")
canvas.move(paddle, canvas_width/2, canvas_height/1.2)


canvas.bind_all("<KeyPress-Right>", move_paddle)
canvas.bind_all("<KeyPress-Left>", move_paddle)
canvas.bind_all("<KeyPress-Up>", move_paddle)
canvas.bind_all("<KeyPress-Down>", move_paddle)
master_timer()
tk.mainloop()

【问题讨论】:

  • 不胜感激看看下面的答案:D

标签: python user-interface pong


【解决方案1】:

如果您参考 tkinter 的 move 方法的文档,或者了解如何使用它,问题就很简单了。来自文档:

.move(tagOrId, xAmount, yAmount) 移动由tagOrId 指定的项目,方法是将xAmount 添加到它们的x 坐标并将yAmount 添加到它们的y 坐标。

因此,如果您注意到,它会先获取 x 坐标,然后再获取 y 坐标。因此,当您想在 y 轴(向上和向下)上平移(移动)时,您想要更改 y 轴参数,但您正在为 x 轴执行此操作。因此,您只需将变量传递给正确的参数即可。

elif key_symbol == "Up" and up >= 0:
    canvas.move(paddle, 0, -paddle_speed)
# move paddle down
elif key_symbol == "Down" and down <= canvas_width:
    canvas.move(paddle, 0, paddle_speed)

还请注意,您可以摆脱所有键绑定并只保留一个通用绑定,因为在您的函数内部,您已经获得了动态按下的键,因此绑定没有多大意义所有可能的键:

canvas.bind_all("<KeyPress>", move_paddle) # Instead of Up, Down, Left and Right

另一个提示是,您可以利用* 将可迭代的内容作为参数传递给函数:

canvas.find_overlapping(*ball_pos) # Instead of ball_pos[0] and so on

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多