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

added a ping pong ball game #1

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
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
Binary file added pingpong/__pycache__/ball.cpython-310.pyc
Binary file not shown.
Binary file added pingpong/__pycache__/play.cpython-310.pyc
Binary file not shown.
Binary file added pingpong/__pycache__/scoreboard.cpython-310.pyc
Binary file not shown.
24 changes: 24 additions & 0 deletions pingpong/ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.speed("slowest")
self.shape("circle")
self.color("white")
self.xmove= 10
self.ymove= 10
self.move_speed =0.1
def move(self):
newx= self.xcor()+self.xmove
newy= self.ycor()+self.ymove
self.goto(newx,newy)
def reflect(self):
self.ymove *= -1
def bounce(self):
self.xmove *= -1
self.move_speed *= 0.9
def refresh(self):
self.goto(0,0)
self.move_speed = 0.1
self.xmove *= -1
48 changes: 48 additions & 0 deletions pingpong/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from turtle import Turtle ,Screen
from play import Game
from ball import Ball
from scoreboard import Score
import time
screen = Screen()
paddle1 = Game((350,0))
paddle2 = Game((-350,0))
ball = Ball()
score = Score()
screen.tracer(0)

paddle1.goto(350,0)
paddle2.goto(-350,0)

screen.setup(width=800,height=600)
screen.bgcolor("black")
screen.title("PONG")


screen.listen()
if paddle1.xcor()<390 or paddle2.ycor()<-350:
screen.onkey(fun=paddle1.goup,key="Up")
screen.onkey(fun=paddle1.godown,key="Down")
screen.onkey(fun=paddle2.goup,key="w")
screen.onkey(fun=paddle2.godown,key="s")

game_on = True
while game_on:
time.sleep(ball.move_speed)
ball.move()
if ball.ycor()>280 or ball.ycor()<-280:
ball.reflect()
if ball.distance(paddle1)<40 or ball.distance(paddle2)<40:
ball.bounce()

if ball.xcor()>400 :
ball.refresh()
score.goto(-300, 250)
score.currentleftscr()
if ball.xcor() < -400:
ball.refresh()
score.goto(300, 250)
score.currentrightscr()
screen.update()


screen.exitonclick()
18 changes: 18 additions & 0 deletions pingpong/play.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from turtle import Turtle , Screen

class Game(Turtle) :
def __init__(self,position):
super().__init__()
self.penup()
self.color("white")
self.shape("square")
self.shapesize(4, 1)
self.goto(position)
def goup(self):
newy = self.ycor()+20
self.goto(self.xcor(),newy)
def godown (self):
newy = self.ycor()-20
self.goto(self.xcor(),newy)


28 changes: 28 additions & 0 deletions pingpong/scoreboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from turtle import Turtle
class Score(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.hideturtle()
self.color("white")
self.lscr=0
self.rscr=0


self.updatescr()

def updatescr(self):
self.goto(-100, 200)
self.write(f"{self.lscr}", True, align="left", font=("arial", 24, "normal"))
self.goto(100, 200)
self.write(f"{self.rscr}", True, align="right", font=("arial", 24, "normal"))

def currentleftscr(self):
self.lscr+=1
self.clear()
self.updatescr()
def currentrightscr(self):
self.rscr+=1
self.clear()
self.updatescr()