Skip to content

Commit

Permalink
Update ping_pong.py
Browse files Browse the repository at this point in the history
The previous code had some errors, such as the "os" module not being imported and incorrect variable names, which could lead to program crashes in the middle.
  • Loading branch information
adixoo authored Sep 24, 2023
1 parent ecc5654 commit ee5682f
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 deletions projects/ping_pong/ping_pong.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# adixoo https://github.com/adixoo
import turtle as t
import os

playerAscore = 0
playerBscore = 0
# Initialize player scores
player_a_score = 0
player_b_score = 0

# create a window and declare a variable called window and call the screen()
# Create a window
window = t.Screen()
window.title("The Pong Game")
window.bgcolor("green")
window.setup(width=800, height=600)
window.tracer(0)
window.tracer(0) # Turn off automatic screen updates

# Creating the left paddle
leftpaddle = t.Turtle()
Expand All @@ -26,51 +29,55 @@
rightpaddle.color("white")
rightpaddle.shapesize(stretch_wid=5, stretch_len=1)
rightpaddle.penup()
rightpaddle.goto(-350, 0)
rightpaddle.goto(350, 0) # Corrected the initial position

# Code for creating the ball
ball = t.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("red")
ball.penup()
ball.goto(5, 5)
ballxdirection = 0.2
ballydirection = 0.2
ball.goto(0, 0)
ball_dx = 0.2 # Changed variable name to match usage
ball_dy = 0.2 # Changed variable name to match usage

# Code for creating pen for scorecard update
# Code for creating a pen to display the score
pen = t.Turtle()
pen.speed(0)
pen.color("Blue")
pen.color("blue")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("score", align="center", font=("Arial", 24, "normal"))
pen.write(
"Player A: 0 Player B: 0",
align="center",
font=("Arial", 24, "normal"),
)


# code for moving the leftpaddle
# Functions to move the left paddle
def leftpaddleup():
y = leftpaddle.ycor()
y = y + 90
y += 20 # Adjusted the paddle movement
leftpaddle.sety(y)


def leftpaddledown():
y = leftpaddle.ycor()
y = y + 90
y -= 20 # Adjusted the paddle movement
leftpaddle.sety(y)


# code for moving the rightpaddle
# Functions to move the right paddle
def rightpaddleup():
y = rightpaddle.ycor()
y = y + 90
y += 20 # Adjusted the paddle movement
rightpaddle.sety(y)


def rightpaddledown():
y = rightpaddle.ycor()
y = y + 90
y -= 20 # Adjusted the paddle movement
rightpaddle.sety(y)


Expand All @@ -81,71 +88,72 @@ def rightpaddledown():
window.onkeypress(rightpaddleup, "Up")
window.onkeypress(rightpaddledown, "Down")

# Main game loop
while True:
window.update()

# moving the ball
ball.setx(ball.xcor() + ballxdirection)
ball.sety(ball.ycor() + ballxdirection)
# Move the ball
ball.setx(ball.xcor() + ball_dx)
ball.sety(ball.ycor() + ball_dy)

# border set up
# Border setup for ball
if ball.ycor() > 290:
ball.sety(290)
ballydirection = ballydirection * -1
ball_dy *= -1

if ball.ycor() < -290:
ball.sety(-290)
ballydirection = ballydirection * -1
ball_dy *= -1

if ball.xcor() > 390:
ball.goto(0, 0)
ball_dx = ball_dx * -1
player_a_score = player_a_score + 1
ball_dx *= -1
player_a_score += 1
pen.clear()
pen.write(
"Player A: {} Player B: {} ".format(
player_a_score, player_b_score
),
align="center",
font=("Monaco", 24, "normal"),
font=("Arial", 24, "normal"),
)
os.system("afplay wallhit.wav&")
# os.system("afplay wallhit.wav&")

if (ball.xcor()) < -390: # Left width paddle Border
if ball.xcor() < -390:
ball.goto(0, 0)
ball_dx = ball_dx * -1
player_b_score = player_b_score + 1
ball_dx *= -1
player_b_score += 1
pen.clear()
pen.write(
"Player A: {} Player B: {} ".format(
player_a_score, player_b_score
),
align="center",
font=("Monaco", 24, "normal"),
font=("Arial", 24, "normal"),
)
os.system("afplay wallhit.wav&")

# Handling the collisions with paddles.
# os.system("afplay wallhit.wav&")

# Handling collisions with paddles
if (
(ball.xcor() > 340)
and (ball.xcor() < 350)
and (
ball.ycor() < rightpaddle.ycor() + 40
and ball.ycor() > rightpaddle.ycor() - 40
ball.ycor() < rightpaddle.ycor() + 50
and ball.ycor() > rightpaddle.ycor() - 50
)
):
ball.setx(340)
ball_dx = ball_dx * -1
os.system("afplay paddle.wav&")
ball_dx *= -1
# os.system("afplay paddle.wav&")

if (
(ball.xcor() < -340)
and (ball.xcor() > -350)
and (
ball.ycor() < leftpaddle.ycor() + 40
and ball.ycor() > leftpaddle.ycor() - 40
ball.ycor() < leftpaddle.ycor() + 50
and ball.ycor() > leftpaddle.ycor() - 50
)
):
ball.setx(-340)
ball_dx = ball_dx * -1
os.system("afplay paddle.wav&")
ball_dx *= -1
# os.system("afplay paddle.wav&")

0 comments on commit ee5682f

Please sign in to comment.