Share your repls and programming experiences

← Back to all posts
2
PONG

Not Exactly on Repl.it...

Since repl.it is a bit weird with Python Turtle, I'll just give you the code to copy and paste in to another program that's not online, something like PyCharm.


PONG - Multiplayer

import turtle
import time
import random
#import winsound

wn = turtle.Screen()
wn.title("PONG")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)

# score
score_a = 0
score_b = 0

# Paddle 1
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("red")
paddle_a.shapesize(5, 1)
paddle_a.penup()
paddle_a.goto(-350, -50)

# Paddle 2
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("blue")
paddle_b.shapesize(5, 1)
paddle_b.penup()
paddle_b.goto(350, 0)


# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("green")
ball.shapesize(1, 1)
ball.penup()
ball.goto(345, 0)
ball.dx = 1
ball.dy = 1

# Function
wait = time.sleep

# pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Controls, w and s for left, and Up arrow and Down arrow for right.", align="center", font=("Courier", 14, "normal"))
wait(3)
pen.clear()
pen.write("When the ball is going to your opponent, do NOT press any keys.", align="center", font=("Courier", 14, "normal"))
wait(3)
pen.clear()
pen.write("Player: 0  Player B: 0", align="center", font=("Courier", 24, "normal"))


def paddle_a_up():
    y = paddle_a.ycor()
    y += 25
    paddle_a.sety(y)

def paddle_a_down():
    y = paddle_a.ycor()
    y += -25
    paddle_a.sety(y)

def paddle_b_up():
    y = paddle_b.ycor()
    y += 25
    paddle_b.sety(y)

def paddle_b_down():
    y = paddle_b.ycor()
    y += -25
    paddle_b.sety(y)

# Keyboard Binding
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.listen()
wn.onkeypress(paddle_a_down, "s")
wn.listen()
wn.onkeypress(paddle_b_up, "Up")
wn.listen()
wn.onkeypress(paddle_b_down, "Down")

# Main Game Loop
while True:
    wn.update()

    # move ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # border check
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1
        #winsound.PlaySound("C:/Users/kerry/Desktop/sound.wav", winsound.SND_ASYNC)
    if paddle_a.ycor() > 245:
        paddle_a.sety(245)

    if paddle_b.ycor() > 245:
        paddle_b.sety(245)

    if paddle_a.ycor() < -245:
        paddle_a.sety(-245)

    if paddle_b.ycor() < -245:
        paddle_b.sety(-245)

    if ball.ycor() < -284:
        ball.sety(-284)
        ball.dy *= -1
        #winsound.PlaySound("C:/Users/kerry/Desktop/sound.wav", winsound.SND_ASYNC)

    if ball.xcor() > 390:
        ball.setx(-345)
        ball.sety(0)
        ball.dx = 1
        ran1 = random.randint(int(1), int(11))
        if ran1 == 1:
            ball.dy = 1.2
        elif ran1 == 2:
            ball.dy = 1.15
        elif ran1 == 3:
            ball.dy = 1.1
        elif ran1 == 4:
            ball.dy = 1.05
        elif ran1 == 5:
            ball.dy = 1
        elif ran1 == 6:
            ball.dy = 0.95
        elif ran1 == 7:
            ball.dy = 1.175
        elif ran1 == 8:
            ball.dy = 1.125
        elif ran1 == 9:
            ball.dy = 1.075
        elif ran1 == 10:
            ball.dy = 1.025
        elif ran1 == 11:
            ball.dy = 0.975
        paddle_a.goto(-350, 0)
        paddle_b.goto(350, -50)
        ball.dx *= -1
        score_a += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))

    if ball.xcor() < -390:
        ball.setx(345)
        ball.sety(0)
        ball.dx = 1
        ran1 = random.randint(int(1), int(11))
        if ran1 == 1:
            ball.dy = 1.2
        elif ran1 == 2:
            ball.dy = 1.15
        elif ran1 == 3:
            ball.dy = 1.1
        elif ran1 == 4:
            ball.dy = 1.05
        elif ran1 == 5:
            ball.dy = 1
        elif ran1 == 6:
            ball.dy = 0.95
        elif ran1 == 7:
            ball.dy = 1.175
        elif ran1 == 8:
            ball.dy = 1.125
        elif ran1 == 9:
            ball.dy = 1.075
        elif ran1 == 10:
            ball.dy = 1.025
        elif ran1 == 11:
            ball.dy = 0.975
        paddle_a.goto(-350, -50)
        paddle_b.goto(350, 0)
        ball.dx *= -1
        score_b += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))

    # paddle ball collision
    if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 60 and ball.ycor() > paddle_b.ycor() - 60):
        ball.setx(340)
        ball.dx *= -1
        ball.dx += 0.1
        ball.dy += (random.randint(-2, 2) / 10)
        #winsound.PlaySound("C:/Users/kerry/Desktop/sound.wav", winsound.SND_ASYNC)

    if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 60 and ball.ycor() > paddle_a.ycor() - 60):
        ball.setx(-340)
        ball.dx *= -1
        ball.dx += 0.1
        ball.dy += (random.randint(-2, 2) / 10)
        #winsound.PlaySound("C:/Users/kerry/Desktop/sound.wav", winsound.SND_ASYNC)

PONG - Single Player

import turtle
import time
import random
#import winsound

wn = turtle.Screen()
wn.title("PONG - Single Player")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)

# score
score_a = 0
score_b = 0

# Paddle 1
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("red")
paddle_a.shapesize(5, 1)
paddle_a.penup()
paddle_a.goto(-350, -50)

# Paddle 2
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("blue")
paddle_b.shapesize(5, 1)
paddle_b.penup()
paddle_b.goto(350, 0)


# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("green")
ball.shapesize(1, 1)
ball.penup()
ball.goto(345, 0)
ball.dx = 1
ball.dy = 1

# Function
wait = time.sleep

# pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Controls, w and s, and you are on the left side.", align="center", font=("Courier", 14, "normal"))
wait(3)
pen.clear()
pen.write("Player: 0  Player B: 0", align="center", font=("Courier", 24, "normal"))


def paddle_a_up():
    y = paddle_a.ycor()
    y += 25
    paddle_a.sety(y)

def paddle_a_down():
    y = paddle_a.ycor()
    y += -25
    paddle_a.sety(y)

def paddle_b_up():
    y = paddle_b.ycor()
    y += 25
    paddle_b.sety(y)

def paddle_b_down():
    y = paddle_b.ycor()
    y += -25
    paddle_b.sety(y)

# Keyboard Binding
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.listen()
wn.onkeypress(paddle_a_down, "s")

# Main Game Loop
while True:
    wn.update()

    #computer
    by = ball.ycor()
    py = paddle_b.ycor()
    if by < py:
        paddle_b.sety(paddle_b.ycor() - 1)
    if by > py:
        paddle_b.sety(paddle_b.ycor() + 1)

    # move ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # border check
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1
        #winsound.PlaySound("C:/Users/kerry/Desktop/sound.wav", winsound.SND_ASYNC)
    if paddle_a.ycor() > 245:
        paddle_a.sety(245)

    if paddle_b.ycor() > 245:
        paddle_b.sety(245)

    if paddle_a.ycor() < -245:
        paddle_a.sety(-245)

    if paddle_b.ycor() < -245:
        paddle_b.sety(-245)

    if ball.ycor() < -284:
        ball.sety(-284)
        ball.dy *= -1
        #winsound.PlaySound("C:/Users/kerry/Desktop/sound.wav", winsound.SND_ASYNC)

    if ball.xcor() > 390:
        ball.setx(-345)
        ball.sety(0)
        ball.dx = 1
        ran1 = random.randint(int(1), int(11))
        if ran1 == 1:
            ball.dy = 1.2
        elif ran1 == 2:
            ball.dy = 1.15
        elif ran1 == 3:
            ball.dy = 1.1
        elif ran1 == 4:
            ball.dy = 1.05
        elif ran1 == 5:
            ball.dy = 1
        elif ran1 == 6:
            ball.dy = 0.95
        elif ran1 == 7:
            ball.dy = 1.175
        elif ran1 == 8:
            ball.dy = 1.125
        elif ran1 == 9:
            ball.dy = 1.075
        elif ran1 == 10:
            ball.dy = 1.025
        elif ran1 == 11:
            ball.dy = 0.975
        paddle_a.goto(-350, 0)
        paddle_b.goto(350, -50)
        ball.dx *= -1
        score_a += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))

    if ball.xcor() < -390:
        ball.setx(345)
        ball.sety(0)
        ball.dx = 1
        ran1 = random.randint(int(1), int(11))
        if ran1 == 1:
            ball.dy = 1.2
        elif ran1 == 2:
            ball.dy = 1.15
        elif ran1 == 3:
            ball.dy = 1.1
        elif ran1 == 4:
            ball.dy = 1.05
        elif ran1 == 5:
            ball.dy = 1
        elif ran1 == 6:
            ball.dy = 0.95
        elif ran1 == 7:
            ball.dy = 1.175
        elif ran1 == 8:
            ball.dy = 1.125
        elif ran1 == 9:
            ball.dy = 1.075
        elif ran1 == 10:
            ball.dy = 1.025
        elif ran1 == 11:
            ball.dy = 0.975
        paddle_a.goto(-350, -50)
        paddle_b.goto(350, 0)
        ball.dx *= -1
        score_b += 1
        pen.clear()
        pen.write("Player A: {}  Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))

    # paddle ball collision
    if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 60 and ball.ycor() > paddle_b.ycor() - 60):
        ball.setx(340)
        ball.dx *= -1
        ball.dx += 0.1
        ball.dy += (random.randint(-2, 2) / 10)
        #winsound.PlaySound("C:/Users/kerry/Desktop/sound.wav", winsound.SND_ASYNC)

    if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 60 and ball.ycor() > paddle_a.ycor() - 60):
        ball.setx(-340)
        ball.dx *= -1
        ball.dx += 0.1
        ball.dy += (random.randint(-2, 2) / 10)
        #winsound.PlaySound("C:/Users/kerry/Desktop/sound.wav", winsound.SND_ASYNC)
Loading Repl