Learn with Hafiza Palwasha
How to design a game 'Basic Ping Pong' Using Visual Studio and Python (Free) ?
Lecture No. 5
1. Download visual studio 'Visual Studio', from the link as shown below and install it.
2. Now download 'Python', from the link as shown below and install it.
3. Go to the 'Downloads' and download 'Python install manager' as shown below:
4. Now run the python, a block window will appear, keep typing 'y' to install and connection with the visual studio.
5. Next, open the visual studio and install updates.
6. Now paste the below code and run it with the play button at the top right side.
import turtle
# Create the screenwn = turtle.Screen()wn.title("Ping Pong Game")wn.bgcolor("black")wn.setup(width=800, height=600)wn.tracer(0)
# Paddle A (Left)paddle_a = turtle.Turtle()paddle_a.speed(0)paddle_a.shape("square")paddle_a.color("white")paddle_a.shapesize(stretch_wid=5, stretch_len=1)paddle_a.penup()paddle_a.goto(-350, 0)
# Paddle B (Right)paddle_b = turtle.Turtle()paddle_b.speed(0)paddle_b.shape("square")paddle_b.color("white")paddle_b.shapesize(stretch_wid=5, stretch_len=1)paddle_b.penup()paddle_b.goto(350, 0)
# Ballball = turtle.Turtle()ball.speed(0)ball.shape("circle")ball.color("white")ball.penup()ball.goto(0, 0)ball.dx = 0.2 # Speed in x directionball.dy = 0.2 # Speed in y direction
# Functions to move paddlesdef paddle_a_up(): y = paddle_a.ycor() y += 20 paddle_a.sety(y)
def paddle_a_down(): y = paddle_a.ycor() y -= 20 paddle_a.sety(y)
def paddle_b_up(): y = paddle_b.ycor() y += 20 paddle_b.sety(y)
def paddle_b_down(): y = paddle_b.ycor() y -= 20 paddle_b.sety(y)
# Keyboard bindingswn.listen()wn.onkeypress(paddle_a_up, "w")wn.onkeypress(paddle_a_down, "s")wn.onkeypress(paddle_b_up, "Up")wn.onkeypress(paddle_b_down, "Down")
# Main game loopwhile True: wn.update()
# Move the ball ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy)
# Border checking (Top/Bottom) if ball.ycor() > 290: ball.sety(290) ball.dy *= -1 if ball.ycor() < -290: ball.sety(-290) ball.dy *= -1
# Border checking (Left/Right) - Reset ball if it passes paddle if ball.xcor() > 390: ball.goto(0, 0) ball.dx *= -1 if ball.xcor() < -390: ball.goto(0, 0) ball.dx *= -1
# Paddle and ball collisions if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() - 40): ball.setx(340) ball.dx *= -1
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() - 40): ball.setx(-340) ball.dx *= -1
7. If you are using Chromebook, use this Replit software.
1. Download visual studio 'Visual Studio', from the link as shown below and install it.
2. Now download 'Python', from the link as shown below and install it.
3. Go to the 'Downloads' and download 'Python install manager' as shown below:
4. Now run the python, a block window will appear, keep typing 'y' to install and connection with the visual studio.
5. Next, open the visual studio and install updates.
6. Now paste the below code and run it with the play button at the top right side.
import turtle
# Create the screen
wn = turtle.Screen()
wn.title("Ping Pong Game")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)
# Paddle A (Left)
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)
# Paddle B (Right)
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
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 = 0.2 # Speed in x direction
ball.dy = 0.2 # Speed in y direction
# Functions to move paddles
def paddle_a_up():
y = paddle_a.ycor()
y += 20
paddle_a.sety(y)
def paddle_a_down():
y = paddle_a.ycor()
y -= 20
paddle_a.sety(y)
def paddle_b_up():
y = paddle_b.ycor()
y += 20
paddle_b.sety(y)
def paddle_b_down():
y = paddle_b.ycor()
y -= 20
paddle_b.sety(y)
# Keyboard bindings
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up")
wn.onkeypress(paddle_b_down, "Down")
# Main game loop
while True:
wn.update()
# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# Border checking (Top/Bottom)
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
# Border checking (Left/Right) - Reset ball if it passes paddle
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
if ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
# Paddle and ball collisions
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() - 40):
ball.setx(340)
ball.dx *= -1
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() - 40):
ball.setx(-340)
ball.dx *= -1
7. If you are using Chromebook, use this Replit software.
Homework Task
Now you have to do the following tasks to show your skills:1. Change the shape of ball to square.2. Move the turtle top and bottom instead of left and right, and the ball movement should be accordingly.3. Make the background 'white' and turtle and ball color 'black'.
Enter your comments if you are facing some difficulty.
Now you have to do the following tasks to show your skills:
1. Change the shape of ball to square.
2. Move the turtle top and bottom instead of left and right, and the ball movement should be accordingly.
3. Make the background 'white' and turtle and ball color 'black'.
Enter your comments if you are facing some difficulty.
.png)
Comments
Post a Comment