[英]Python Ping-Pong game, the ball speed randomly changes during paddle movement
我從學習 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 幀中,球將在 1 毫秒內移動dx
和dy
,但在下一幀中,球需要 7 毫秒才能再次移動dx
和dy
— 換句話說,您的球/槳的運動因此是可變的,因為它是以可變數量的每秒幀數而不是固定的 fps 發生。
因此,您可以告訴計算機以多長時間不斷推進游戲/動畫,而不是讓它以可變速度推進。 (否則,如果您願意,可以將事物調整為可變速度)。 您可以通過以恆定幀速率運行的回調函數來實現這一點——例如, Pong with Turtle Graphics 中的frame()
函數:第 2 部分:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.