ping pong game python turtle code example
Example: how to make a ping pong game in python using turtle
from turtle import *
from random import randint, random
from time import sleep
start = 0
sliderH, sliderW = 60,20
totalX, totalY = 400, 400
ballX, ballY = 0.0, 0.0
collision = 0
dX = random() + 1
dY = random() + 1
screen = Screen()
screen.setup(totalX+50,totalY+50)
screen.bgcolor('black')
t0 = Turtle()
t0.hideturtle()
t0.color('white'); t0.speed(5)
t0.penup(); t0.goto(0,totalY//2+10); t0.write("Press 'S' to start", True, align="center")
t0.penup(); t0.goto(-totalX//2,-totalY//2); t0.pendown(); t0.goto(-totalX//2,totalY//2); t0.goto(totalX//2,totalY//2); t0.goto(totalX//2,-totalY//2); t0.goto(-totalX//2,-totalY//2);
t0.penup();
t1 = Turtle()
t1.hideturtle()
screen.register_shape("line", ((-sliderW//2,-sliderH//2), (-sliderW//2,sliderH//2), (sliderW//2,sliderH//2), (sliderW//2,-sliderH//2)))
t1.shape('line')
t1.speed(0)
t1.color('red')
t1.setheading(90)
t1.penup(); t1.goto(-totalX//2 + 10, 0);
t1.showturtle()
def go_up():
t1.fd(5)
def go_down():
t1.bk(5)
def start_game():
global start
start = 1
screen.onkey(go_up, 'Up')
screen.onkey(go_down, 'Down')
screen.onkey(start_game, 'S')
t2 = Turtle()
t2.shape('circle'); t2.speed(0)
t2.color('blue')
t2.penup(); t2.goto(ballX,ballY);
t1.setheading(90)
def move_t2():
global sliderH, sliderW
global totalX, totalY
global ballX, ballY
global dX, dY
global collision
global start
t2.goto(ballX, ballY)
sliderX, sliderY = t1.pos()
if ballY >= (sliderY - sliderH//2)and ballY <= (sliderY + sliderH//2) and ballX <= -totalX//2 + sliderW + 10:
if collision == 0:
dX = -dX
collision = 1
if ballY <= (sliderY - sliderH//2) or ballY >= (sliderY + sliderH//2):
if ballX <= -totalX//2 + sliderW + 10 -5:
start = 0
if ballX >= -totalX//2 + sliderW + 10:
collision = 0
if ballX > totalX//2 -10:
dX = -dX
if ballY > totalY//2 - 10:
dY = -dY
if ballY < -totalY//2 + 10:
dY = -dY
if start == 1:
ballX += dX
ballY += dY
else:
ballX = 0
ballY = 0
screen.ontimer(move_t2,1)
move_t2()
screen.listen()
screen.mainloop()