Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Imports
- """
- MADE BY Naser Al-Shaer
- """
- import turtle
- import os
- # Setup the screen
- wn = turtle.Screen()
- wn.title("Game By @Naser2018")
- wn.bgcolor("black")
- wn.setup(width=800, height=600)
- wn.tracer(0)
- # Score
- score_a = 0
- score_b = 0
- #paddles width
- paddlesWidth = 100
- # Paddle A
- paddle_a = turtle.Turtle()
- paddle_a.speed(0)
- paddle_a.shape("square")
- paddle_a.shapesize(stretch_wid=paddlesWidth/20, stretch_len=1)
- paddle_a.color("white")
- paddle_a.penup()
- paddle_a.goto(-350, 0)
- # Paddle B
- paddle_b = turtle.Turtle()
- paddle_b.speed(0)
- paddle_b.shape("square")
- paddle_b.shapesize(stretch_wid=paddlesWidth/20, stretch_len=1)
- paddle_b.color("white")
- paddle_b.penup()
- paddle_b.goto(350, 0)
- # Ball
- ball = turtle.Turtle()
- ball.speed(0)
- ball.shape("circle")
- ball.color("white")
- ball.penup()
- ball.goto(0, 0)
- ball.dx = .08
- ball.dy = .08
- # Pen
- pen = turtle.Turtle()
- pen.color("white")
- pen.speed(0)
- pen.penup()
- pen.hideturtle()
- pen.goto(0,260)
- pen.write("Player A: {0} | Player B: {1}".format(score_a, score_b), align="center", font=("Courier", 18, "normal"))
- # Moves Functions
- def paddle_a_up():
- if paddle_a.ycor() + 50 < 300:
- paddle_a.sety(paddle_a.ycor() + paddlesWidth / 2)
- def paddle_a_down():
- if paddle_a.ycor() - 50 > -300:
- paddle_a.sety(paddle_a.ycor() - paddlesWidth / 2)
- def paddle_b_up():
- if paddle_b.ycor() + 50 < 300:
- paddle_b.sety(paddle_b.ycor() + paddlesWidth / 2)
- def paddle_b_down():
- if paddle_b.ycor() - 50 > -300:
- paddle_b.sety(paddle_b.ycor() - paddlesWidth / 2)
- # Keyboards Binding
- wn.listen()
- wn.onkey(paddle_a_up, "w")
- wn.onkey(paddle_a_down, "s")
- wn.onkey(paddle_b_up, "Up")
- wn.onkey(paddle_b_down, "Down")
- # Main game loop
- while True:
- wn.update()
- # Ball moves
- ball.setx(ball.xcor() + ball.dx)
- ball.sety(ball.ycor() + ball.dy)
- # Border Check
- if ball.ycor() > 290:
- ball.sety(290)
- ball.dy *= -1
- os.system("aplay bounce.wav&")
- if ball.ycor() < -290:
- ball.sety(-290)
- ball.dy *= -1
- os.system("aplay bounce.wav&")
- if ball.xcor() > 390:
- ball.goto(0,0)
- ball.dx *= -1
- score_a += 1
- pen.clear()
- pen.write("Player A: {0} | Player B: {1}".format(score_a, score_b), align="center", font=("Courier", 18, "normal"))
- os.system("aplay score.wav")
- if ball.xcor() < -390:
- ball.goto(0,0)
- ball.dx *= -1
- score_b += 1
- pen.clear()
- pen.write("Player A: {0} | Player B: {1}".format(score_a, score_b), align="center", font=("Courier", 18, "normal"))
- os.system("aplay score.wav")
- # Paddles and ball collisions
- if (ball.xcor() > 340 and ball.xcor() < 350 ) and (ball.ycor() > paddle_b.ycor() - 50 and ball.ycor() < paddle_b.ycor() + 50): # 50 is 100/2 = paddleWidth / 2
- ball.setx(340)
- ball.dx *= -1
- os.system("aplay bounce.wav&")
- if (ball.xcor() < -340 and ball.xcor() > -350 ) and (ball.ycor() > paddle_a.ycor() - 50 and ball.ycor() < paddle_a.ycor() + 50): # 50 is 100/2 = paddleWidth / 2
- ball.setx(-340)
- ball.dx *= -1
- os.system("aplay bounce.wav&")
- # Game Finished V0.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement