0

Hey I'm new to the world of programing so I decide to start making simple games so that I can learn the logic behind it.

In my code of the classic game of Pong, I don't know why but I can't get to move both players (jugadorA and jugadorB). I can only get to move one at a time.

If someone could look at my could and give me a suggestion I would be very thankful. Also if you guys have any suggestions of simple games to program in python (the language I'm learning) I'm all ears.

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
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Does this answer your question? [How to bind several key presses together in turtle graphics?](https://stackoverflow.com/questions/47879608/how-to-bind-several-key-presses-together-in-turtle-graphics) – ggorlen Apr 09 '23 at 22:47
  • A few problems: (1) changing positions directly inside handlers (2) not using `ontimer` and a proper event loop (3) accessing private `wn` properties to test key presses in addition to using handlers. [This answer](https://stackoverflow.com/a/70979967/6243352) fixes all of these issues. – ggorlen Apr 09 '23 at 22:48
  • This could use a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). It would help read your code faster. – Blue Robin Apr 11 '23 at 19:55

0 Answers0