Python 乒乓球弹跳

Python 乒乓球弹跳,python,python-3.x,Python,Python 3.x,我已经写了一些代码,但不能让球在地板或天花板上自然反弹。请帮忙 我已经试过用ball_heading=ball.heading来头球,但是没有成功 #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")

我已经写了一些代码,但不能让球在地板或天花板上自然反弹。请帮忙

我已经试过用ball_heading=ball.heading来头球,但是没有成功

#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()
#Python 3.6.3
从海龟进口*
输入数学
随机输入
#屏幕设置
bgcolor(“黑色”)
wn=屏幕()
wn.名称(“乒乓球”)
#画框
bd=海龟()
bd.pencolor(“白色”)
bd.pensize(3)
bd.hideturtle()
bd.penup()
bd.setposition(-400,-300)
bd.pendown()
速度(0)
bd.pendown()
对于范围(2)中的行:
bd.forward(800)
bd.left(90)
bd.forward(600)
bd.left(90)
bd.penup()
bd.setposition(0,300)
bd.setheading(270)
bd.pendown()
对于范围内的破折号(30):
bd.forward(10)
bd.penup()
bd.forward(10)
bd.pendown()
#创建桨
#桨1
player1=海龟()
播放者1.颜色(“白色”)
播放器1.形状(“方形”)
player1.形状大小(拉伸宽度=5,拉伸长度=1)
player1.penup()
播放器1.设置位置(-370,0)
#桨2
player2=海龟()
播放者2.颜色(“白色”)
播放器2.形状(“方形”)
player2.形状大小(拉伸宽度=5,拉伸长度=1)
player2.penup()
播放器2.设置位置(370,0)
#创造球
球=乌龟()
球。颜色(“白色”)
球形(“方形”)
球速(0)
ball.penup()
球的设定位置(0,0)
球的设置头球(随机随机随机(0360))
#移动玩家
球员速度=15
#p1
def上移()
y=player1.ycor()
y+=玩家速度
#设置边界
如果y>245:
y=245
玩家1.赛蒂(y)
def move_down():
y=player1.ycor()
y-=球员速度
#设置边界
如果y<-245:
y=-245
玩家1.赛蒂(y)
#p2
def move_up2():
y=player2.ycor()
y+=玩家速度
#设置边界
如果y>245:
y=245
玩家2.赛蒂(y)
def向下移动2()
y=player2.ycor()
y-=球员速度
#设置边界
如果y<-245:
y=-245
玩家2.赛蒂(y)
#球的运动
def ball_fd():
球向前(3)
#球天花板/地板反弹
def ball_bounce():
by=ball.ycor()
如果by>279:
by=279
鲍尔·塞蒂(by)
bx=ball.ycor()
如果bx<-279:
bx=-279
球。setx(bx)
#装订
听
ON键(向上移动,“向上”)
ON键(向下移动“向下”)
ON键(向上移动2,“w”)
ON键(向下移动2,“s”)
#使球移动/主游戏循环
尽管如此:
ball_fd()
球反弹()
很抱歉,代码有点长,但是可以随意复制并粘贴到IDLE或其他任何地方。
谢谢你

当球碰到地板或天花板时,你没有转动球

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