简体   繁体   中英

Ball bounce in pong

I have written some code but can't get the ball to bounce on the floor or the ceiling naturally. Please help!

I've already tried getting the ball heading with ball_heading = ball.heading, but that didn't work

#Python 3.6.3
from turtle import *
import math
import random

#Screen Setup
bgcolor("black")
wn = Screen()
wn.title("Pong")

#Drawing Border
bd = Turtle()
bd.pencolor("white")
bd.pensize(3)
bd.hideturtle()
bd.penup()
bd.setposition(-400, -300)
bd.pendown()
bd.speed(0)
bd.pendown()
for line in range(2):
    bd.forward(800)
    bd.left(90)
    bd.forward(600)
    bd.left(90)
bd.penup()
bd.setposition(0, 300)
bd.setheading(270)
bd.pendown()
for dash in range(30):
    bd.forward(10)
    bd.penup()
    bd.forward(10)
    bd.pendown()

#Creating Paddles

#Paddle 1
player1 = Turtle()
player1.color("white")
player1.shape("square")
player1.shapesize(stretch_wid=5, stretch_len=1)
player1.penup()
player1.setposition(-370, 0)

#Paddle 2
player2 = Turtle()
player2.color("white")
player2.shape("square")
player2.shapesize(stretch_wid=5, stretch_len=1)
player2.penup()
player2.setposition(370, 0)

#Creating the ball
ball = Turtle()
ball.color("white")
ball.shape("square")
ball.speed(0)
ball.penup()
ball.setposition(0, 0)
ball.setheading(random.randint(0, 360))

#Moving the  player

playerspeed = 15
#p1
def move_up():
    y = player1.ycor()
    y += playerspeed
    #Setting the boundries
    if y > 245:
        y = 245
    player1.sety(y)

def move_down():
    y = player1.ycor()
    y -= playerspeed
    #Setting the boundries

    if y < -245:
        y = -245
    player1.sety(y)
#p2
def move_up2():
    y = player2.ycor()
    y += playerspeed
    #Setting the boundries
    if y > 245:
        y = 245
    player2.sety(y)

def move_down2():
    y = player2.ycor()
    y -= playerspeed
    #Setting the boundries
    if y < -245:
        y = -245
    player2.sety(y)

#Ball movement
def ball_fd():
    ball.forward(3)

#Ball ceiling / floor bounce
def ball_bounce():
    by = ball.ycor()
    if by > 279:
        by = 279
    ball.sety(by)

    bx = ball.ycor()
    if bx < -279:
        bx = -279
    ball.setx(bx)

#binding
listen()
onkey(move_up, "Up")
onkey(move_down, "Down")
onkey(move_up2, "w")
onkey(move_down2, "s")

#Making the ball move / main game loop
while True:
    ball_fd()
    ball_bounce()

Sorry the code is kind of long, but feel free to copy + paste it into IDLE or whatever. Thank you

Your bounce function seems weird - you are resetting the y value, not really dealing with x (probably an honest mistake in that second ycor ), and most importantly - you are not changing the direction of motion - the heading. Here is a version that works a bit better to start from:

bheading = random.randint(0, 360)
ball.setheading(bheading)

def ball_bounce():
    global bheading
    if abs(ball.ycor()) >= 279 :
        bheading = -bheading
        ball.setheading(bheading)

    if abs(ball.xcor()) >= 379:
        #Or change this to a player fail/scoring event
        bheading-=45
        ball.setheading(bheading)

Now the heading is saved in the global bheading so we can always adjust direction relative to our previous heading. Also note my use of abs for checking both borders at once, both negative and positive side. You may want to replace the x bounce with a fail/scoring event later on.

There still maybe edge problems, for example the ball could get stuck in the corner - border checks will save from that, though this will not happen often so it's hard to debug.

You didn't turn the ball when it hit the floor or ceiling.

while True:
    ball.fd(3)
    by = ball.ycor()
    if abs(by) > 279:
        ball.setheading(-ball.heading())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM