乒乓球反弹

时间:2019-04-16 07:30:25

标签: 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")
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()

对不起,代码有点长,但是可以随意复制+粘贴到IDLE或其他任何东西中。 谢谢

2 个答案:

答案 0 :(得分:0)

您的退回功能似乎很奇怪-您正在重置y的值,而不是真正地处理x(这可能是第二个ycor的诚实错误),最重要的是-您是不改变运动方向-航向。这是一个更好的起点版本:

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)

现在,标题已保存在全局bheading中,因此我们始终可以相对于之前的标题调整方向。另请注意,我使用abs一次检查了正反两个边界。您稍后可能希望将x退回替换为失败/得分事件。

也许仍然存在边缘问题,例如,球可能卡在角落里-边界检查将为您省钱,尽管这种情况很少发生,所以很难调试。

答案 1 :(得分:0)

当球碰到地板或天花板时,您没有将球转过来。

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