From 5722c619108ead7c7ed6419f89a4ce5ea3d595ef Mon Sep 17 00:00:00 2001 From: geekboi <85869106+geekboi777@users.noreply.github.com> Date: Wed, 14 Jul 2021 16:57:22 +0530 Subject: [PATCH 1/2] Easier method to reverse a number .py --- Program of Reverse of any number.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Program of Reverse of any number.py b/Program of Reverse of any number.py index 3b8d5a8ddb5..cffc44dcba6 100644 --- a/Program of Reverse of any number.py +++ b/Program of Reverse of any number.py @@ -5,3 +5,8 @@ num = num//10 rev=rev*10+Rem print("The Reverse of the number",rev) +################## +# could also simply do this another way + +num = input() +print(int(num[::-1])) From f7755a50d31b9582f8b3131e0eeb308b840230d3 Mon Sep 17 00:00:00 2001 From: geekboi <85869106+geekboi777@users.noreply.github.com> Date: Fri, 16 Jul 2021 22:27:36 +0530 Subject: [PATCH 2/2] pong_game.py Pong game using turtle --- Pong_game.py | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 Pong_game.py diff --git a/Pong_game.py b/Pong_game.py new file mode 100644 index 00000000000..636f2296b01 --- /dev/null +++ b/Pong_game.py @@ -0,0 +1,166 @@ +import turtle + +# Create screen +from turtle import Turtle + +sc = turtle.Screen() +sc.title("Pong game") +sc.bgcolor("white") +sc.setup(width=1000, height=600) + +# Left paddle +left_pad = turtle.Turtle() +left_pad.speed(0) +left_pad.shape("square") +left_pad.color("black") +left_pad.shapesize(stretch_wid=6, stretch_len=2) +left_pad.penup() +left_pad.goto(-400, 0) + +# Right paddle +right_pad = turtle.Turtle() +right_pad.speed(0) +right_pad.shape("square") +right_pad.color("black") +right_pad.shapesize(stretch_wid=6, stretch_len=2) +right_pad.penup() +right_pad.goto(400, 0) + +# Ball of circle shape +hit_ball = turtle.Turtle() +hit_ball.speed(40) +hit_ball.shape("circle") +hit_ball.color("blue") +hit_ball.penup() +hit_ball.goto(0, 0) +hit_ball.dx = 5 +hit_ball.dy = -5 + +# Create screen +sc = turtle.Screen() +sc.title("Pong game") +sc.bgcolor("white") +sc.setup(width=1000, height=600) + +# Left paddle +left_pad = turtle.Turtle() +left_pad.speed(0) +left_pad.shape("square") +left_pad.color("black") +left_pad.shapesize(stretch_wid=6, stretch_len=2) +left_pad.penup() +left_pad.goto(-400, 0) + +# Right paddle +right_pad = turtle.Turtle() +right_pad.speed(0) +right_pad.shape("square") +right_pad.color("black") +right_pad.shapesize(stretch_wid=6, stretch_len=2) +right_pad.penup() +right_pad.goto(400, 0) + +# Ball of circle shape +hit_ball: Turtle = turtle.Turtle() +hit_ball.speed(40) +hit_ball.shape("circle") +hit_ball.color("blue") +hit_ball.penup() +hit_ball.goto(0, 0) +hit_ball.dx = 5 +hit_ball.dy = -5 + +# Initialize the score +left_player = 0 +right_player = 0 + +# Displays the score +sketch = turtle.Turtle() +sketch.speed(0) +sketch.color("blue") +sketch.penup() +sketch.hideturtle() +sketch.goto(0, 260) +sketch.write("Left_player : 0 Right_player: 0", + align="center", font=("Courier", 24, "normal")) + + +# Functions to move paddle vertically +def paddleaup(): + y = left_pad.ycor() + y += 20 + left_pad.sety(y) + + +def paddleadown(): + y = left_pad.ycor() + y -= 20 + left_pad.sety(y) + + +def paddlebup(): + y = right_pad.ycor() + y += 20 + right_pad.sety(y) + + +def paddlebdown(): + y = right_pad.ycor() + y -= 20 + right_pad.sety(y) + + +# Keyboard bindings +sc.listen() +sc.onkeypress(paddleaup, "e") +sc.onkeypress(paddleadown, "x") +sc.onkeypress(paddlebup, "Up") +sc.onkeypress(paddlebdown, "Down") + +while True: + sc.update() + + hit_ball.setx(hit_ball.xcor() + hit_ball.dx) + hit_ball.sety(hit_ball.ycor() + hit_ball.dy) + + # Checking borders + if hit_ball.ycor() > 280: + hit_ball.sety(280) + hit_ball.dy *= -1 + + if hit_ball.ycor() < -280: + hit_ball.sety(-280) + hit_ball.dy *= -1 + + if hit_ball.xcor() > 500: + hit_ball.goto(0, 0) + hit_ball.dy *= -1 + left_player += 1 + sketch.clear() + sketch.write("Left_player : {} Right_player: {}".format( + left_player, right_player), align="center", + font=("Courier", 24, "normal")) + + if hit_ball.xcor() < -500: + hit_ball.goto(0, 0) + hit_ball.dy *= -1 + right_player += 1 + sketch.clear() + sketch.write("Left_player : {} Right_player: {}".format( + left_player, right_player), align="center", + font=("Courier", 24, "normal")) + + # Paddle ball collision + if ((hit_ball.xcor() > 360 and + hit_ball.xcor() < 370) and + (hit_ball.ycor() < right_pad.ycor() + 40 and + hit_ball.ycor() > right_pad.ycor() - 40)): + hit_ball.setx(360) + hit_ball.dx *= -1 + + if ((hit_ball.xcor() < -360 and + hit_ball.xcor() > -370) and + (hit_ball.ycor() < left_pad.ycor() + 40 and + hit_ball.ycor() > left_pad.ycor() - 40)): + hit_ball.setx(-360) + hit_ball.dx *= -1