Commit 979c6597 authored by 12903783's avatar 12903783
Browse files

Ball now bounces off paddles.

parent b7ab1769
......@@ -3,30 +3,41 @@ import turtle
wn = turtle.Screen()
wn.setup(width=800, height=600)
wn.bgcolor ("#0fb300")
wn.title ("Turtle Tennis")
#Paddle 1
paddle1 = turtle.Turtle()
paddle1.hideturtle()
paddle1.shape("square")
paddle1.color("red")
paddle1.shapesize(10,1,5)
paddle1.penup()
paddle1.setx(-300)
paddle1.showturtle()
#Court
court = turtle.Turtle()
court.shape("turtle")
court.color("white")
court.penup()
court.sety (300)
court.right (90)
court.pendown()
court.forward (600)
#Paddle 2
paddle2 = turtle.Turtle()
paddle2.hideturtle()
paddle2.shape("square")
paddle2.color("blue")
paddle2.shapesize(10,1,5)
paddle2.penup()
paddle2.setx(300)
paddle2.showturtle()
#Racquet 1
racquet1 = turtle.Turtle()
racquet1.hideturtle()
racquet1.shape("square")
racquet1.color("red")
racquet1.shapesize(10,1,5)
racquet1.penup()
racquet1.setx(-300)
racquet1.showturtle()
#Racquet 2
racquet2 = turtle.Turtle()
racquet2.hideturtle()
racquet2.shape("square")
racquet2.color("blue")
racquet2.shapesize(10,1,5)
racquet2.penup()
racquet2.setx(300)
racquet2.showturtle()
#Ball
ball = turtle.Turtle()
ball.shape("circle")
ball.shape("turtle")
ball.color("#d2fc03")
ball.penup()
ball.dx = 4
......@@ -45,45 +56,56 @@ scoreboard.write("0 0", font=("Arial Black", "60"))
#Movement
def paddle1up():
y = paddle1.ycor()
def racquet1up():
y = racquet1.ycor()
y += 20
paddle1.sety(y)
racquet1.sety(y)
def paddle1down():
y = paddle1.ycor()
def racquet1down():
y = racquet1.ycor()
y -= 20
paddle1.sety(y)
racquet1.sety(y)
def paddle2up():
y = paddle2.ycor()
def racquet2up():
y = racquet2.ycor()
y += 20
paddle2.sety(y)
racquet2.sety(y)
def paddle2down():
y = paddle2.ycor()
def racquet2down():
y = racquet2.ycor()
y -= 20
paddle2.sety(y)
racquet2.sety(y)
#Controls
wn.listen()
wn.onkeypress(paddle1up, "w")
wn.onkeypress(paddle1down, "s")
wn.onkeypress(paddle2up, "k")
wn.onkeypress(paddle2down, "m")
wn.onkeypress(racquet1up, "w")
wn.onkeypress(racquet1down, "s")
wn.onkeypress(racquet2up, "k")
wn.onkeypress(racquet2down, "m")
#Screen Boundaries
while True:
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
if ball.xcor() > 390:
ball.setx(390)
if ball.xcor() > 330:
ball.hideturtle()
ball.setx(0)
ball.sety(0)
ball.showturtle()
ball.dx *= -1
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() < -390:
ball.setx(-390)
if ball.xcor() < -330:
ball.hideturtle()
ball.setx(0)
ball.sety(0)
ball.showturtle()
ball.dx *= -1
if (ball.xcor() > 275 and ball.xcor() < 285) and (ball.ycor() < racquet2.ycor() +100 and ball.ycor() > racquet2.ycor() -100):
ball.dx *= -1
if (ball.xcor() < -275 and ball.xcor() > -285) and (ball.ycor() < racquet1.ycor() +100 and ball.ycor() > racquet1.ycor() -100):
ball.dx *= -1
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment