0

I am trying to make a simple brick breaker game but pretty quickly ran into the issue of lag. I assume this is due to the game having to redraw each brick (I intend to have about 50 at a time) every time it runs the main loop. Is there a way I can solve this? Also another factor might be the way I have done collisions. Its coded to check each block individually each loop. Here is some of the code, If you want it all just ask.

 #This is the main loop, lag seems to come from the for loop that draws the blocks, as it runs about 50 times
while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
            break
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                menu()
                gameExit = True
                break
        if event.type == pygame.MOUSEMOTION:
            playerBoard.move()

    ball.col_check()        
    ball.move()

    gameDisplay.fill(white)#Clear Screen

    #FPS counter
    fps = font.render(str(int(clock.get_fps())), True, pygame.Color('grey'))
    gameDisplay.blit(fps, (50, 50))

    #Draw Player
    pygame.draw.rect(gameDisplay, grey, (playerBoard.x, playerBoard.y, playerBoard.width, playerBoard.height))
    #Draw Blocks

    for item in blocks:
        pygame.draw.rect(gameDisplay, black, (item.x, item.y, item.width, item.height))

    #Draw ball
    pygame.draw.rect(gameDisplay, black, (ball.x, ball.y , ball.width, ball.width))

    pygame.display.flip()

    clock.tick(FPS)

The brick-ball collision code is here: (Does not work properly but I think I might be able to fix it myself. Only registers hits randomly, it would seem.)

def col_check(self):
    #Check Board collision.
    if self.y + self.radius >= playerBoard.y and self.y <= playerBoard.y + blockSize:
        if (self.x + self.radius <= playerBoard.x + blockSize * 5 and self.x + self.radius >= playerBoard.x) or (self.x - self.radius <= playerBoard.x + blockSize * 5 and self.x - self.radius >= playerBoard.x):
            self.yChange *= -1
            x = self.x
            x -= playerBoard.x + (blockSize * 5) / 2 #Minus the (paddle Y cor + paddle center)
            x /= 4 #Lower the angle a bit
            x = int(x)
            self.xChange = x
    #Check border collisions         
    elif self.x - self.radius + borderS <= 0 or self.x + self.radius >= displayWidth - borderS:
        self.xChange *= -1
    elif self.y - self.radius <= 0:
        self.yChange *= -1
    elif self.y >= displayHeight - self.width - borderB:
        self.__init__()     
    #Finaly check block collisions
    else:
        for item in blocks:
            if (self.y + self.radius >= item.y and self.y + self.radius <= item.y + blockSize) or (self.y - self.radius >= item.y and self.y - self.radius <= item.y + blockSize):
                if (self.x + self.radius <= item.x + blockSize * 2 and self.x + self.radius >= item.x) or (self.x - self.radius <= item.x + blockSize * 2 and self.x - self.radius >= item.x):
                    self.yChange *= -1  

Commenting out the collision check however does not increase the FPS at all, so it is not the main issue. Thanks in advance!

~ Moses

Mose
  • 49
  • 7
  • 2
    You should check up the sprite class. It even does the collision tests by itself. [Pygame Sprite](https://www.pygame.org/docs/tut/SpriteIntro.html). It has methods so it only draws elements that needed to be redrawn. – Cowtard Apr 19 '18 at 09:04
  • Please post a [minimal, complete and verfiable example](https://stackoverflow.com/help/mcve) that we can copy, run and test. – skrx Apr 19 '18 at 12:47
  • Ok, next time ill try to make a simple snippet :) – Mose Apr 19 '18 at 23:31

0 Answers0