Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

So I am trying to program a pong game in pygame, python. Whenever I move my mouse fastly, the ball is slowing down. With my mouseposition, I move my paddle, so I need mouse motion. How can I fix this issue or what might be the problem? Am I doing something wrong? Appreciate any help, or feedback (I am beginner)

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()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You want to be setting up a pygame.time.Clock() and in your event loop be calling clock.tick(60) (for 60fps).

Right now your loop is input-bound and not FPS-bound.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading