# A program that simulates a two-player game of pong. # Author: Nicholas Coleman # The 'a' and 'z' keys are used to move player 1 (left) # The up and down arrows are used to move player 2 (right) # The space bar is used to serve # The program does not currently keep track of points from turtle import * class TurtlePong: # Create a new TurtlePong object def __init__(game): # Create Turtle object for player 1 game.player1 = makePlayer(-300, 0) # Create Turtle object for player 2 game.player2 = makePlayer(300, 0) # Create Turtle object for the ball game.ball = makeBall(30) # Set player 1 as the first server game.setServer(game.player1) # Use 'a' and 'z' to move player1 onkey(game.p1up, "a") onkey(game.p1down, "z") # Use up and down arrows to move player 2 onkey(game.p2up, "Up") onkey(game.p2down, "Down") # Use space bar to serve onkey(game.serve, " ") listen() # Give the ball to the player who will serve next def setServer(game, server): game.ball.speed("fastest") if server == game.player1: offset = 25 else: # server == player 2 offset = -25 game.ball.goto(server.xcor() + offset, server.ycor()) game.server = server game.holding = True # move player 1 up def p1up(game): moveUp(game.player1) if game.server == game.player1 and game.holding: moveUp(game.ball) # move player 1 down def p1down(game): moveDown(game.player1) if game.server == game.player1 and game.holding: moveDown(game.ball) # move player 2 up def p2up(game): moveUp(game.player2) if game.server == game.player2 and game.holding: moveUp(game.ball) # move player 2 down def p2down(game): moveDown(game.player2) if game.server == game.player2 and game.holding: moveDown(game.ball) # serve the ball if it is ready to be served def serve(game): # only serve if a player is holding the ball if(game.holding): game.holding = False # keep going until the ball is out of bounds while game.ball.xcor() > -300 and game.ball.xcor() < 300: # if the ball hits the top or bottom wall, change direction if game.ball.ycor() > 300 or game.ball.ycor() < -300: game.ball.speed("fastest") game.ball.setheading(360 - game.ball.heading()) # if the ball hits player 1 or 2, change direction if touching(game.player1, game.ball) or \ touching(game.player2, game.ball): game.ball.speed("fastest") game.ball.setheading(180 - game.ball.heading()) # move the ball forward game.ball.speed("slowest") game.ball.forward(10) # if the ball is out on player 1's side, serve goes to player 2 if game.ball.xcor() < -300: game.setServer(game.player2) # if the ball is out on player 2's side, serve goes to player 1 if game.ball.xcor() > 300: game.setServer(game.player1) # make a turtle that looks like a pong player def makePlayer(xPos, yPos): player = Turtle() player.shape("square") player.shapesize(4.0, 1.0) player.speed("fastest") player.penup() player.goto(xPos, yPos) return player # make a turtle that looks like a turtle, but acts like a ball def makeBall(heading): ball = Turtle() ball.shape("turtle") ball.penup() ball.setheading(heading) return ball # move a turtle up by 25 pixels def moveUp(turtle): turtle.goto(turtle.xcor(), turtle.ycor() + 25) # move a turtle down by 25 pixels def moveDown(turtle): turtle.goto(turtle.xcor(), turtle.ycor() - 25) # determine if a player is touching the ball def touching(player, ball): return abs(ball.xcor() - player.xcor()) < 25 and \ abs(ball.ycor() - player.ycor()) < 50 # Create a new game and keep it going until the user clicks on the window def main(): game = TurtlePong() exitonclick() main()