Python Pong
Pong game created through python.
import turtle
wn=turtle.Screen()
wn.title("pong game")
wn.bgcolor("red")
wn.setup (width = 800,height=600)
wn.tracer(0)
Paddle
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.color("white")
paddle_a.penup()
#goto is initial position
paddle_a.goto(-350, 0)
#paddle b
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.color("white")
paddle_b.penup()
paddle_b.goto(350,0)
Ball
#ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("black")
ball.penup()
#goto is initial position
ball.goto(0, 0)
Padle Movement
def paddle_a_up():
#ycor is the y cordinate of object
y = paddle_a.ycor()
y +=20
paddle_a.sety(y)
#wn.listen tells it to listen ad recognize keyboard input
wn.listen()
wn.onkeypress(paddle_a_up,"w") def paddle_a_down(): #ycor is the y cordinate of object y = paddle_a.ycor()
y +=-20
paddle_a.sety(y)
wn.listen()
wn.onkeypress(paddle_a_down,"s") def paddle_b_up(): #ycor is the y cordinate of object y = paddle_b.ycor()
y +=20
paddle_b.sety(y)
wn.listen()
wn.onkeypress(paddle_b_up,"Up") def paddle_b_down(): #ycor is the y cordinate of object y = paddle_b.ycor()
y +=-20
paddle_b.sety(y)
wn.listen()
wn.onkeypress(paddle_b_down,"Down")
Ball Movement
while True:
wn.update() ball.setx(ball.xcor()+ball.dx) ball.sety(ball.ycor()+ball.dy) if ball.ycor()>290: ball.sety(290) ball.dy *=-1 if ball.ycor()<-290: ball.sety(-290) ball.dy *=-1 if ball.xcor()>390: ball.sety(390) ball.goto(0,0) ball.dx*=-1 score_a+=1 if ball.xcor()<-390: ball.goto(0,0) ball.dx *=-1 score_b+=1 if (ball.xcor()>340 and ball.xcor()<350) and (ball.ycor() < paddle_b.ycor()+40 and ball.ycor() > paddle_b.ycor()-40 ): ball.setx(340) ball.dx*=-1 if(ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() - 40): ball.setx(-340) ball.dx*=-1
Scoring System
pen = turtle.Turtle()
pen.speed(0)
pen.penup()
pen.color("white")
pen.hideturtle()
pen.goto(0,260)
pen.write("player a:0 player b:0 ", align = "center",font=("Courier",24,"normal")) score_a = 0 score_b = 0
credit to https://www.youtube.com/channel/UC2vm-0XX5RkWCXWw...
youtube channel: TokyoEdtech for the tutorial