Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve the Issue #332|Corrected errors in Pon_Game.py #457

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions Pong_Game.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import turtle as t
playerAscore=0
playerBscore=0
player_a_score=0
player_b_score=0
import os
#create a window and declare a variable called window and call the screen()
window=t.Screen()
window.title("The Pong Game")
Expand All @@ -13,7 +13,7 @@
leftpaddle=t.Turtle()
leftpaddle.speed(0)
leftpaddle.shape("square")
leftpaddle.color("white")
leftpaddle.color("yellow")
leftpaddle.shapesize(stretch_wid=5,stretch_len=1)
leftpaddle.penup()
leftpaddle.goto(-350,0)
Expand All @@ -25,7 +25,7 @@
rightpaddle.color("white")
rightpaddle.shapesize(stretch_wid=5,stretch_len=1)
rightpaddle.penup()
rightpaddle.goto(-350,0)
rightpaddle.goto(350,0)

#Code for creating the ball
ball=t.Turtle()
Expand All @@ -34,8 +34,8 @@
ball.color("red")
ball.penup()
ball.goto(5,5)
ballxdirection=0.2
ballydirection=0.2
ballxdirection=2
ballydirection=2

#Code for creating pen for scorecard update
pen=t.Turtle()
Expand All @@ -50,22 +50,30 @@
def leftpaddleup():
y=leftpaddle.ycor()
y=y+90
if y > 260:
y = 260
leftpaddle.sety(y)

def leftpaddledown():
y=leftpaddle.ycor()
y=y+90
y=y-90
if y < -260:
y = -260
leftpaddle.sety(y)

#code for moving the rightpaddle
def rightpaddleup():
y=rightpaddle.ycor()
y=y+90
if y > 260:
y = 260
rightpaddle.sety(y)

def rightpaddledown():
y=rightpaddle.ycor()
y=y+90
y=y-90
if y < -260:
y = -260
rightpaddle.sety(y)

#Assign keys to play
Expand All @@ -80,7 +88,7 @@ def rightpaddledown():

#moving the ball
ball.setx(ball.xcor()+ballxdirection)
ball.sety(ball.ycor()+ballxdirection)
ball.sety(ball.ycor()+ballydirection)

#border set up
if ball.ycor()>290:
Expand All @@ -91,31 +99,31 @@ def rightpaddledown():
ballydirection=ballydirection*-1

if ball.xcor() > 390:
ball.goto(0,0)
ball_dx = ball_dx * -1
ball.goto(100,100)
ballxdirection = ballxdirection * -1
player_a_score = 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"))
os.system("afplay wallhit.wav&")



if(ball.xcor()) < -390: # Left width paddle Border
ball.goto(0,0)
ball_dx = ball_dx * -1
ballxdirection = ballxdirection * -1
player_b_score = 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"))
os.system("afplay wallhit.wav&")

# Handling the collisions with paddles.

if(ball.xcor() > 340) and (ball.xcor() < 350) and (ball.ycor() < rightpaddle.ycor() + 40 and ball.ycor() > rightpaddle.ycor() - 40):
if(ball.xcor() > 350) and (ball.xcor() < 360) and (ball.ycor() < rightpaddle.ycor() + 40 and ball.ycor() > rightpaddle.ycor() - 40):
ball.setx(340)
ball_dx = ball_dx * -1
ballxdirection = ballxdirection * -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):
if(ball.xcor() < -350) and (ball.xcor() > -360) and (ball.ycor() < leftpaddle.ycor() + 40 and ball.ycor() > leftpaddle.ycor() - 40):
ball.setx(-340)
ball_dx = ball_dx * -1
ballxdirection = ballxdirection * -1
os.system("afplay paddle.wav&")