巨蟒乒乓球比赛,划桨过程中球速随机变化 [英] Python Ping-Pong game, the ball speed randomly changes during paddle movement

查看:0
本文介绍了巨蟒乒乓球比赛,划桨过程中球速随机变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从学习python开始,并尝试按照教程学习如何用python制作乒乓球游戏。在我的游戏中,球的速度会有某种程度的波动和不一致,特别是当我使用W或S和箭头键移动两个球拍时,速度会发生变化,造成不便。我已经多次检查代码中的错误,但我无法找出它。以下是我的代码。

import turtle

win = turtle.Screen()
win.title("Pong by killkennyale")
win.bgcolor("black")
win.setup(width=800, height=600)
win.tracer(0)

# Score
score_a = 0
score_b = 0


# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0) #speed of animation of paddle
paddle_a.shape("square")
paddle_a.shapesize(stretch_wid=5, stretch_len=1) 
paddle_a.color("white")
paddle_a.penup() #turtle draws lines, we don't want that duh
paddle_a.goto(-350,0) #coordinates for turtle-paddle


# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0) #speed of animation of paddle
paddle_b.shape("square")
paddle_b.shapesize(stretch_wid=5, stretch_len=1) 
paddle_b.color("white")
paddle_b.penup() #turtle draws lines, we don't want that duh
paddle_b.goto(350,0) #coordinates for turtle-paddle


# Ball
ball = turtle.Turtle()
ball.speed(0) #speed of animation of ball
ball.shape("circle")
ball.shapesize(stretch_wid=1, stretch_len=1) 
ball.color("white")
ball.penup() #turtle draws lines, we don't want that duh
ball.goto(0,0) #coordinates for turtle-ball
ball.dx = 0.5 #speed of x
ball.dy = 0.5 #speed of y

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle() # we don't need to see it
pen.goto(0, 260)
pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 16, "normal"))

# now we gotta make the paddle move using a keyboard

# Function
def paddle_a_up():
    y = paddle_a.ycor() #assign y-coordinate to variable y
    y = y + 20 #add 20 pixels to y-coordinate
    paddle_a.sety(y)

def paddle_a_down():
    y = paddle_a.ycor() #assign y-coordinate to variable y
    y = y - 20 #add 20 pixels to y-coordinate
    paddle_a.sety(y)

def paddle_b_up():
    y = paddle_b.ycor() #assign y-coordinate to variable y
    y = y + 20 #add 20 pixels to y-coordinate
    paddle_b.sety(y)

def paddle_b_down():
    y = paddle_b.ycor() #assign y-coordinate to variable y
    y = y - 20 #add 20 pixels to y-coordinate
    paddle_b.sety(y)

# Keyboard Binding
win.listen() # tells the turtle to listen to the keyboard
win.onkeypress(paddle_a_up, "w")
win.onkeypress(paddle_a_down, "s")
# when we press 'w'or's' then it calls paddle_a_up or down and adds or subtracts 20 to the y coordinate
win.onkeypress(paddle_b_up, "Up")
win.onkeypress(paddle_b_down, "Down")
# when we press 'up'or'down' arrow then it calls paddle_b_up or down and adds or subtracts 20 to the y coordinate


#Main Game Loop
while True:
    win.update()

    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # Border Checking
    if ball.ycor() > 290:
        ball.sety(290) # it reaches roof at 300
        ball.dy = ball.dy * -1
   
    if ball.ycor() < -290:
        ball.sety(-290) # it reaches roof at 300
        ball.dy = ball.dy * -1
   
    if ball.xcor() > 390:
        ball.goto(0,0)
        ball.dx = ball.dx * -1
        score_a += 1
        pen.clear()
        pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 16, "normal"))

    if ball.xcor() < -390:
        ball.goto(0,0)
        ball.dx = ball.dx * -1
        score_b += 1
        pen.clear()
        pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 16, "normal"))

    # 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

推荐答案

您需要一个主/游戏/动画循环,其设置方式是以恒定的速度向前推进,而不是像当前那样以可变速度前进。

使用您的示例中设置的while循环,循环的下一次迭代将在前一次迭代之后立即调用,并且每次迭代可能需要变量的时间量才能完成。但是您的球和球拍的移动量是固定的,这就是您有问题的原因。例如,循环的第一帧/迭代可能需要1毫秒,但第二帧/迭代可能需要7毫秒,因为按下键,因此由计算机进行更多计算。因此,在第1帧中,球将在1ms内移动dxdy,但在下一帧中,球仅移动dxdy需要7ms-换句话说,球/桨的移动是可变的,因为它以每秒可变的帧数发生,而不是以固定的fps发生。

这样你就可以告诉计算机每隔多长时间不断推进一次游戏/动画,而不是让它以可变的速度前进。(否则,如果你愿意,可以把东西调整到可变的速度)。您可以通过以恒定帧速率运行的回调函数来实现这一点--例如Pong with Turtle Graphics: Part 2中的frame()函数:

framerate_ms = 40  # Every how many milliseconds must frame function be called?

def frame () :
    screen.ontimer(frame, framerate_ms)  # schedule this function to be called again
    check_if_someone_scores()
    update_paddle_positions()
    update_ball_position()
    screen.update()                      # display the new frame

frame()                                  # call it manually the first time

这篇关于巨蟒乒乓球比赛,划桨过程中球速随机变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆