You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Turtle Pong游戏问题:无法同时移动两名玩家求助

Pong游戏双玩家无法同时移动问题排查与新手游戏推荐

我是编程新手,为了学习编程逻辑开始做简单游戏。用Python的turtle库写经典Pong游戏时,遇到了两个玩家(jugadorA和jugadorB)无法同时移动的问题,每次只能移动其中一个,麻烦帮忙排查代码问题,同时也欢迎推荐适合Python新手的简单游戏项目。

以下是我的代码:

import turtle

#Ventana
wn = turtle.Screen()
wn.title("Mi Pong")
wn.bgcolor("black")
wn.setup(width = 800, height = 600)
wn.tracer(0)


#Marcador
marcadorA = 0
marcadorB = 0

#Jugador A
jugadorA = turtle.Turtle()
jugadorA.speed(0)
jugadorA.shape("square")
jugadorA.color("white")
jugadorA.penup()
jugadorA.goto(-350,0)
jugadorA.shapesize(stretch_wid = 5, stretch_len = 1)

#Jugador B
jugadorB = turtle.Turtle()
jugadorB.speed(0)
jugadorB.shape("square")
jugadorB.color("white")
jugadorB.penup()
jugadorB.goto(350,0)
jugadorB.shapesize(stretch_wid = 5, stretch_len = 1)

#Pelota
pelota = turtle.Turtle()
pelota.speed(0)
pelota.shape("circle")
pelota.color("blue")
pelota.penup()
pelota.goto(0,0)
pelota.dx = 3
pelota.dy = 3


#Linea Division
division = turtle.Turtle()
division.color("white")
division.goto(0,400)
division.goto(0,-400)


#Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write("Juegador A: 0        Jugador B: 0", align = "center", font= ("Courier", 24, "normal"))


#Funciones
def jugadorA_up():
    y = jugadorA.ycor()
    y += 20
    jugadorA.sety(y)

def jugadorA_down():
    y = jugadorA.ycor()
    y -= 20
    jugadorA.sety(y)

def jugadorB_up():
    y = jugadorB.ycor()
    y += 20
    jugadorB.sety(y)

def jugadorB_down():
    y = jugadorB.ycor()
    y -= 20
    jugadorB.sety(y)

#teclado
wn.listen()
wn.onkeypress(jugadorA_up,"w")
wn.onkeypress(jugadorA_down,"s")
wn.onkeypress(jugadorB_up,"Up")
wn.onkeypress(jugadorB_down,"Down")



while True:
    wn.update()


    # Mover los jugadores al mismo tiempo
    if "w" in wn._keys:
        jugadorA_up()
    if "s" in wn._keys:
        jugadorA_down()
    if "Up" in wn._keys:
        jugadorB_up()
    if "Down" in wn._keys:
        jugadorB_down()

    pelota.setx(pelota.xcor() + pelota.dx)
    pelota.sety(pelota.ycor() + pelota.dy)


    #Bordes
    if pelota.ycor() > 290:
        pelota.dy *= -1
    if pelota.ycor() < -290:
        pelota.dy *= -1

    #Bordes derecha e izquierda
    if pelota.xcor() > 390:
        pelota.goto(0,0)
        pelota.dx *= -1
        marcadorA += 1
        pen.clear()
        pen.write("Juegador A: {}        Jugador B: {}".format(marcadorA,marcadorB), align = "center", font= ("Courier", 24, "normal"))

    if pelota.xcor() < -390:
        pelota.goto(0,0)
        pelota.dx *= -1
        marcadorB += 1
        pen.clear()
        pen.write("Juegador A: {}        Jugador B: {}".format(marcadorA,marcadorB), align = "center", font= ("Courier", 24, "normal"))



    if ((pelota.xcor() > 340 and pelota.xcor() < 350)
        and (pelota.ycor() < jugadorB.ycor() + 50
             and pelota.ycor() > jugadorB.ycor() -50)):
        pelota.dx *= -1

    if ((pelota.xcor() < -340 and pelota.xcor() > -350)
        and (pelota.ycor() < jugadorA.ycor() + 50
             and pelota.ycor() > jugadorA.ycor() -50)):
        pelota.dx *= -1

问题排查与解决

你的代码里同时用了两种键盘事件处理方式,导致冲突:

  1. 已经通过wn.onkeypress()绑定了按键触发的移动函数
  2. 又在while True循环里直接访问turtle内部私有属性wn._keys来检测按键

turtle的_keys是内部私有属性,不应该直接调用,而且两种方式叠加会导致按键事件处理混乱,无法响应多键同时按下。

解决方法:删除while True循环里的这段代码:

# Mover los jugadores al mismo tiempo
if "w" in wn._keys:
    jugadorA_up()
if "s" in wn._keys:
    jugadorA_down()
if "Up" in wn._keys:
    jugadorB_up()
if "Down" in wn._keys:
    jugadorB_down()

保留之前的wn.onkeypress()绑定即可,turtle本身支持多键同时按下的事件响应,删除重复代码后就能实现两个玩家同时移动了。

另外,代码里的计分板文字有个拼写错误:Juegador应该是Jugador,可以顺手修正。

适合Python新手的简单游戏项目推荐

  • 猜数字游戏:随机生成一个1-100的数字,玩家输入猜测值,程序提示“大了”/“小了”,直到猜对为止,能练习条件判断和循环逻辑
  • 贪吃蛇游戏:用turtle或pygame实现,控制蛇吃食物变长,碰到边界或自身就结束,练习运动逻辑和碰撞检测
  • 打砖块游戏:类似Pong,控制挡板反弹小球打掉上方的砖块,练习碰撞检测和得分系统
  • 井字棋:双人对战的3x3棋盘,判断横竖斜方向的三连获胜,练习数组操作和胜负判断逻辑
  • 简单计算器:实现加减乘除基本运算,练习输入输出和运算符逻辑

内容的提问来源于stack exchange,提问作者Mr Rick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 01:22:05