代码之家  ›  专栏  ›  技术社区  ›  Adnan

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

  •  0
  • Adnan  · 技术社区  · 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)
    

    如果球在球拍的中间碰撞,那么它会反弹(效果很好)。我试着改变球的速度。上述案例的结果仍然存在漏洞

    1 回复  |  直到 1 年前
        1
  •  0
  •   cuppajoeman    1 年前

    正如每个人都提到的,一段时间的循环以可变的速度运行,这是因为它运行得尽可能快,但这取决于你的计算机上随时发生的事情。

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

    以下是一些使用库执行此操作的python代码 pygame 您必须安装它才能运行此演示。请随意 re-implement 这使用了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 with controllable paddle")
    
    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
    
    paddle_width = 100
    paddle_height = 20
    paddle_x = (screen_width - paddle_width) // 2
    paddle_y = screen_height - 50
    paddle_speed = 500
    
    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()
    
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            paddle_x -= paddle_speed * dt
        if keys[pygame.K_RIGHT]:
            paddle_x += paddle_speed * dt
    
        if paddle_x < 0:
            paddle_x = 0
        elif paddle_x > screen_width - paddle_width:
            paddle_x = screen_width - paddle_width
    
        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
    
        if ball_y + ball_radius >= paddle_y and ball_x >= paddle_x and ball_x <= paddle_x + paddle_width:
            ball_speed_y *= -1
    
        screen.fill(BLACK)
    
        pygame.draw.circle(screen, WHITE, (int(ball_x), int(ball_y)), ball_radius)
        pygame.draw.rect(screen, WHITE, (paddle_x, paddle_y, paddle_width, paddle_height))
        pygame.display.flip()
    
        pygame.time.Clock().tick(60)
    
    

    看看你是否可以重新制作你的例子,并以此为例添加第二个球拍。