乒乓球以一种奇怪的方式移动,突然加速

问题描述 投票:0回答:1

球正在移动,但速度突然增加,请观看 0:01。
视频链接:

https://streamable.com/qmtqaq

另外,在球拍边缘,球会越过内侧,不会碰撞和反弹。另外,当球在桨顶部碰撞时,球会水平快速移动

https://streamable.com/9geekj

https://streamable.com/qs5fkq

#main.py
from turtle import Screen
from pong_setup import Paddle
from ball import Ball
import time
screen = Screen()
ball = Ball()

screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.title("Pong")
screen.tracer(0)
screen.listen()

computer = Paddle(x=350, y=0)
user = Paddle(x=-350, y=0)
screen.onkey(fun=computer.mv_fd,key="w")
screen.onkey(fun=computer.mv_bk,key="s")

while True:   
   screen.update()
   ball.move()

   # collisions 
   if ball.ycor() >= 290 or ball.ycor() < -290:
      ball.bounce_y()
   if (ball.distance(computer) < 20 and ball.xcor() > 340):
      ball.bounce_x()
screen.exitonclick()
#ball.py
import time
from turtle import Turtle

class Ball(Turtle):
   def __init__(self):
      super().__init__()
      self.shape("circle")
      self.color("white")
      self.penup()
      self.x_move = 0.05
      self.y_move = 0.05
   
   def move(self):
      new_x = self.xcor() + self.x_move
      new_y = self.ycor() + self.y_move
      self.goto(new_x, new_y)


   def bounce_y(self):
     self.y_move *= -1
   def bounce_x(self):
     self.x_move *= -1
#paddle.py
from turtle import Turtle

class Paddle(Turtle):
   def __init__(self,x,y):
      super().__init__()
      # self.speed(0)
      self.shape("square")
      self.penup()
      self.color("white")
      self.turtlesize(5,1)
      self.goto(x,y)

   def mv_fd(self):
      self.goto(350,self.ycor()+30)
   def mv_bk(self):
      self.goto(350,self.ycor()-30)

如果球在桨的中间碰撞,那么它会弹开(效果很好)。我试图改变球的速度。对于上述情况,结果仍然有问题

python python-turtle pong
1个回答
0
投票

正如每个人都提到的,while 循环以可变速度运行,这是因为它尽可能快地运行,但根据计算机上随时发生的情况,这会有所不同。

为了解决这个问题,我们需要测量已经发生的实时时间量,如果自上次更新以来已经过去了很多时间,我们需要将球移动很多,如果没有,那么我们就不会移动它很多。这就是测量更新之间的增量时间然后乘以它的作用。

在进行桨碰撞之前,请确保您可以使用时间增量让球在屏幕上弹跳,这里有一些 Python 代码,它使用库

pygame
来执行此操作,您必须安装该库才能运行此演示。请随意使用turtle和Python中的内置时间库重新实现

import pygame
import sys

pygame.init()

screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Moving Ball")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

ball_radius = 20
ball_x = screen_width // 2
ball_y = screen_height // 2
ball_speed_x = 300  # Pixels per second
ball_speed_y = 300  # Pixels per second

prev_time = pygame.time.get_ticks()
while True:
    # Calculate delta time
    current_time = pygame.time.get_ticks()
    dt = (current_time - prev_time) / 1000.0  # Convert to seconds
    prev_time = current_time

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    ball_x += ball_speed_x * dt
    ball_y += ball_speed_y * dt

    if ball_x + ball_radius >= screen_width or ball_x - ball_radius <= 0:
        ball_speed_x *= -1
    if ball_y + ball_radius >= screen_height or ball_y - ball_radius <= 0:
        ball_speed_y *= -1

    screen.fill(BLACK)
    pygame.draw.circle(screen, WHITE, (int(ball_x), int(ball_y)), ball_radius)

    pygame.display.flip()
    pygame.time.Clock().tick(60)
© www.soinside.com 2019 - 2024. All rights reserved.