Python Pong Game
Python Pong Game
import turtle
# Create screen
sc = turtle.Screen()
sc.title("pong game by saad")
sc.bgcolor("black")
sc.setup(width=1000, height=600)
#paddlea
left_pad = turtle.Turtle()
left_pad.speed(0)
left_pad.shape("square")
left_pad.color("white")
left_pad.shapesize(stretch_wid=6, stretch_len=2)
left_pad.penup()
left_pad.goto(-400, 0)
#paddleb
right_pad = turtle.Turtle()
right_pad.speed(0)
right_pad.shape("square")
right_pad.color("white")
right_pad.shapesize(stretch_wid=6, stretch_len=2)
right_pad.penup()
right_pad.goto(400, 0)
def paddleadown():
y = left_pad.ycor()
y -= 20
left_pad.sety(y)
def paddlebup():
y = right_pad.ycor()
y += 20
right_pad.sety(y)
def paddlebdown():
y = right_pad.ycor()
y -= 20
right_pad.sety(y)
# Keyboard bindings
sc.listen()
sc.onkeypress(paddleaup, "w")
sc.onkeypress(paddleadown, "s")
sc.onkeypress(paddlebup, "8")
sc.onkeypress(paddlebdown, "2")
while True:
sc.update()
hit_ball.setx(hit_ball.xcor()+hit_ball.dx)
hit_ball.sety(hit_ball.ycor()+hit_ball.dy)
# Checking borders
if hit_ball.ycor() > 280:
hit_ball.sety(280)
hit_ball.dy *= -1
if (hit_ball.xcor()<-360 and
hit_ball.xcor()>-370) and
(hit_ball.ycor()<left_pad.ycor()+40 and
hit_ball.ycor()>left_pad.ycor()-40):
hit_ball.setx(-360)
hit_ball.dx*=-1