为什么我的 pygame 程序在移动鼠标时变慢?

Why does my pygame program slowdown, when I move my mouse?

所以我正在尝试在 pygame、python 中编写乒乓球游戏。每当我快速移动鼠标时,球就会减速。通过我的鼠标位置,我移动了桨,所以我需要鼠标移动。我该如何解决这个问题或者可能是什么问题?难道我做错了什么?感谢任何帮助或反馈(我是初学者)

import pygame, random

WHITE = (255, 255, 255)
width = 1000
height = 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pong Game")

Directions = ["negative", "positive"]
DirectionX = random.choice(Directions)
DirectionY = random.choice(Directions)

class classBall():
    def __init__(self):
        self.BallIMG = pygame.image.load("Ball.png")
        self.X = width/2
        self.Y = height/2
        BallDisplay = window.blit(self.BallIMG, (self.X, self.Y))
        self.Vel = 0.25
        pass

    def display(self):
        global DirectionX, DirectionY

        if 25 < self.X < 35 and PaddlePlayer.PosY-15 < self.Y < PaddlePlayer.PosY+115:
            DirectionX = "positive"
        if width-65 < self.X < width-55 and PaddleComp.PosY-15 < self.Y < PaddleComp.PosY+115:
            DirectionX = "negative"
        if 10 > self.Y:
            DirectionY = "positive"
        if height-10 < self.Y:
            DirectionY = "negative"

        if DirectionY == "positive":
            self.Y += self.Vel
        else:
            self.Y -= self.Vel
        if DirectionX == "positive":
            self.X += self.Vel
        else:
            self.X -= self.Vel
        BallDisplay = window.blit(self.BallIMG, (self.X, self.Y))

Ball = classBall()

class Paddle1Player():
    def __init__(self):
        self.PosY = height/2
        self.PosX = 30
        pygame.draw.rect(window, WHITE, [self.PosX, self.PosY, 5, 100])

    def display(self):
        x, MausPositionY = pygame.mouse.get_pos()
        if not MausPositionY > height-100:
            self.PosY = MausPositionY
        pygame.draw.rect(window, WHITE, [30, self.PosY, 5, 100])

class Paddle2Computer():
    def __init__(self):
        self.PosY = height/2
        self.PosX = width-35
        pygame.draw.rect(window, WHITE, [self.PosX, self.PosY, 5, 100])

    def display(self):

        if Ball.X > width/2 and DirectionX == "positive":
            if self.PosY < Ball.Y and DirectionY == "positive":
                self.PosY+= Ball.Vel
            elif self.PosY > Ball.Y and DirectionY == "negative":
                self.PosY-= Ball.Vel
        else:
            if not height/2 -1 < self.PosY+25 < height/2:
                if self.PosY+25 < height/2:
                    self.PosY+= Ball.Vel-Ball.Vel*3/10
                else:
                    self.PosY-= Ball.Vel-Ball.Vel*3/10
            
        pygame.draw.rect(window, WHITE, [width-35, self.PosY, 5, 100])


PaddlePlayer = Paddle1Player()
PaddleComp = Paddle2Computer()



running = True
pygame.display.flip()
while running:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    window.fill((0,0,0))
    
    Ball.display()
    PaddlePlayer.display()
    PaddleComp.display()

    pygame.display.flip()

您想设置 pygame.time.Clock() 并在您的事件循环中调用 clock.tick(60)(60fps)。

现在你的循环是输入绑定而不是 FPS 绑定。

  • In Pygame, normalizing game-speed across different fps values