import turtle
import time

#datos iniciales
A=""
B=""
datos=True

def nombre(nom):
	
	if len(nom)==0 or len(nom)>4: 
		return nombre(str(input("dime nombre  de 1 a 4 caracteres): ")))
	return nom
A= nombre(str(input("dime nombre A (max 4 caracteres): ")))
B= nombre(str(input("dime nombre B (max 4 caracteres): ")))
while True:
	try:
		vel=int(input("dime velocidad (numero entero mayor de 0 hasta 10: "))
		while vel<=0 or vel>10:
			vel=int(input("dime velocidad( mayor 0 y menor 10: "))
		break
	except ValueError:
		print ("debe ser un número entero")
	
#ventana
wn = turtle.Screen()
wn.title("juego Comuneros")
wn.bgcolor("black")
wn.setup(width =800, height=600)
wn.tracer(0)
#marcador
marcadorA=0
marcadorB=0
#jugadorA
jugadorA= turtle.Turtle()
jugadorA.speed(0)
jugadorA.shape("square")
jugadorA.color("blue")
jugadorA.penup()
jugadorA.goto(-350,0)
jugadorA.shapesize(stretch_wid=5,stretch_len=1)

#jugadorB
jugadorB= turtle.Turtle()
jugadorB.speed(0)
jugadorB.shape("square")
jugadorB.color("red")
jugadorB.penup()
jugadorB.goto(350,0)
jugadorB.shapesize(stretch_wid=5,stretch_len=1)

#Pelota
Pelota= turtle.Turtle()
Pelota.speed(0)
Pelota.shape("square")
Pelota.color("white")
Pelota.penup()
Pelota.goto(0,0)
Pelota.shapesize(stretch_wid=1,stretch_len=1)
Pelota.dx =2
Pelota.dy =2
#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("JugadorA: {} 0     JugadorB: {} 0".format(A,B), 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, "Q")	
wn.onkeypress(jugadorA_down, "A")
wn.onkeypress(jugadorB_up, "Up")	
wn.onkeypress(jugadorB_down, "Down")
#velocidad bola
n=0
vel=vel*0.1

while datos:
	
	Pelota.setx(Pelota.xcor() + Pelota.dx*vel)
	Pelota.sety(Pelota.ycor() + Pelota.dy*vel)
	wn.update()
	#Bordes
	if (Pelota.ycor()  > 290 and n==0):
		Pelota.dy *=-1
	if (Pelota.ycor() < -290 and n==0):
		Pelota.dy *=-1
	#Bordes derecha/izquierda
	if (Pelota.xcor() > 390 and n==0):
		Pelota.goto(0,0)
		marcadorA +=1
		pen.clear()
		pen.write("JugadorA: {} {}     JugadorB: {} {}".format(A,marcadorA,B,marcadorB),align = "center", font=("Courier",24,"normal"))
		if marcadorA>4:
			pen.clear()
			pen.write("Jugador {} GANADOR".format(A),align = "center", font=("Courier",30,"normal"))
			n=1
			time.sleep(5)
			datos=False
			
	if (Pelota.xcor() < -390 and n==0):
		Pelota.goto(0,0)
		marcadorB +=1
		pen.clear()
		pen.write("JugadorA: {} {}       JugadorB: {} {}".format(A,marcadorA,B,marcadorB), align = "center", font=("Courier",24,"normal"))
		if marcadorB>4:
			pen.clear()
			pen.write("Jugador {} GANADOR".format(B),align = "center", font=("Courier",30,"normal"))
			n=1
			time.sleep(5)
			datos= False
	if(((Pelota.xcor() > 340 and Pelota.xcor() <350) 
			and (Pelota.ycor()< jugadorB.ycor() +50)
			and (Pelota.ycor()> jugadorB.ycor() -50))and n==0):
		Pelota.dx *=-1
	if(((Pelota.xcor() < -340 and Pelota.xcor() >-350)
			and (Pelota.ycor()< jugadorA.ycor() +50)
			and (Pelota.ycor()> jugadorA.ycor() -50))and n==0):
		Pelota.dx *=-1
#final
print("adios")
