xodat51
xodat51

Reputation: 23

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

Upvotes: 2

Views: 253

Answers (1)

Fredrick Brennan
Fredrick Brennan

Reputation: 7357

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.

Upvotes: 2

Related Questions