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