top of page

Pong Game in Python (BuildLog)

This was my first foray into making a piece of usable software in any programming language. I've always wanted to learn to program, but was so tired of the "hello, world" exercises and books that outlined concepts but never tied them to real-world scenarios and use cases. So I thought "let's set a goal to create a simple game, and learn from there" and it was the most productive way of learning I've ever found. This project was written in Python 2.8 on a Macbook Pro. When I exported it to a Windows machine, the object speeds had to be drastically reduced because everything moved as lightspeed.





Code if anyone is interested is here- it's a python-based script so you could copy+paste this into an empty file, give it a .py extension and execute/play it yourself:

# This is me learning how to write something real in Python
# Comments have to have 2 spaces between the command, and 1 space after the hashtag
# By Cameron Skokowski

import turtle  # import means you import a module, like in powershell or anything else
import os


wn = turtle.Screen()  # wn is defined as a window using turtle.screen that defines it as youre using turtle
wn.title("Pong by onesixski")  # title, bgcolor, everything are all variable properties you can choose and define
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)  # this is to speed up the games as tracer being ON stops the screen from updating during every loop

# Score
score_a = 0
score_b = 0

# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350,0)

# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")  # 20 pixels wide and tall by default
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)  # 100 pixels up and down, 20 pixels left and right
paddle_b.penup()  #without this you'll see a pen moving between the points
paddle_b.goto(350,0)  # this should be 350 pixels left of center of the window

# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)  # objects all start in the center of the screen by default, but this explicitly tells it to
ball.dx = 3
ball.dy = 4

# Pen for words on top of screen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()  # this hides while it's drawing
pen.goto(0, 225)
pen.write("CAMPONG", align="center", font=("courier", 66, "bold"))
pen.goto(0, -260)  # this puts it near the top of the screen as the second value is window height
pen.write("Player A: 0                       Player B: 0", align="center", font=("courier", 24, "normal"))





# Function
def paddle_a_up():  # functions are defined with 'def', they're like actions for variables
    y = paddle_a.ycor()  # this sends the Paddle A variable to move up and down using the ycor method in turtle
    y += 20 # this sets the coordinate to 20 pixels higher where it currently started
    paddle_a.sety(y)  # this just defined the sety property to be defined by the y function you just made

def paddle_a_down():  
    y = paddle_a.ycor() 
    y -= 20 # this sets the coordinate to 20 pixels lower where its last position was
    paddle_a.sety(y)

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

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

# Keyboard Binding
wn.listen()
wn.onkeypress(paddle_a_up, "w")  # button 4 and button 5 are mousewheel scroll up and down
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up")  # you can use w, s, anything, but arrows are defined as up, down, left, right
wn.onkeypress(paddle_b_down, "Down")

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

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

 # Border checking
 if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1

 if ball.ycor() < -280:
        ball.sety(-280)
        ball.dy *= -1
 
 if ball.xcor() > 380:  # this just resets the ball every time it hits the side, because it's only supposed to bounce off a paddle
        ball.goto(0, 0)
        ball.dx *= -1 # this changes its left/right direction when it goes back to center, which is why it starts differently
        score_a += 1
        pen.clear()
        pen.goto(0, 225)
        pen.write("CAMPONG", align="center", font=("courier", 66, "bold"))
        pen.goto(0, -260)
        pen.write("Player A: {}                       Player B: {}".format(score_a, score_b), align="center", font=("courier", 24, "normal"))


 if ball.xcor() < -390:
        ball.goto(0, 0)
        ball.dx *= -1
        score_b += 1
        pen.clear()
        pen.goto(0, 225)
        pen.write("CAMPONG", align="center", font=("courier", 66, "bold"))
        pen.goto(0, -260)
        pen.write("Player A: {}                       Player B: {}".format(score_a, score_b), align="center", font=("courier", 24, "normal"))

 # Paddle and ball collisions
 if (ball.xcor() > 330 and ball.xcor() < 340) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() -50):
        ball.setx(330)    
        ball.dx *= -1
        os.system("afplay /Users/cameronski/VSCpython/Pong/bounce.wav&")

 if (ball.xcor() < -330 and ball.xcor() > -340) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() -50):
        ball.setx(-330)    
        ball.dx *= -1
        os.system("afplay /Users/cameronski/VSCpython/Pong/bounce.wav&")



55 views0 comments

Comentarios


bottom of page