1

I'm currently programming a game using Pygame, once I implemented the Main Menu, the FPS of my game were divided by two, What could possibly cause that lag? (I'm talking about the framerate while playing, when the Main menu is not visible)

There is the code for the Main Menu (point list is the list of point for the polygon to display the current selection):

import sys,os
import pygame
from Hud_implem import *
class MainMenu(object):
    def main(self,screen):
        self.buttonlist = [[0]]
        self.buttonlist.append(self.pointlist((58.104,32.148),(69.396,23.915),(69.369,30.296),(58.104,38.630)))
        self.buttonlist.append(self.pointlist((58.104,41.407),(77.229,27.296),(77.229,33.777),(58.104,47.888)))
        self.buttonlist.append(self.pointlist((58.104,50.037),(74.187,38.185),(74.189,44.666),(58.104,56.519)))
        self.buttonlist.append(self.pointlist((58.104,58.704),(66.396,52.593),(66.396,59.074),(58.104,65.185)))
        clock = pygame.time.Clock()
        screen_image = pygame.image.load(os.path.join("Images","Title Screen.png")).convert_alpha()
        screen_image.set_colorkey((193,56,57))
        screen_image = pygame.transform.smoothscale(screen_image,(SCREEN_W,SCREEN_H))
        selection_layer = pygame.Surface((SCREEN_W,SCREEN_H))
        selection_layer.fill((255,255,255))
        selection_layer.set_colorkey((255,255,255))
        selection_layer.set_alpha(100)
        self.selectioned = 1
        self.movecountdown = 0
        while  not started:
            dt = clock.tick(60)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == 13:
                    self.get_pressed()
            screen.fill((49,103,124),((0,0),screen.get_size()))
            self.get_selection(selection_layer,screen,dt/1000.)
            screen.blit(screen_image,(0,0))
            pygame.display.flip()

    def get_pressed(self):
        if self.selectioned == 1 or self.selectioned == 2:
            #started = True
            Game().main(screen)
        elif self.selectioned == 4:
            pygame.quit()
    def get_selection(self,layer,screen,dt):
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_UP] and not self.movecountdown:
            self.selectioned += -1
            self.movecountdown = 0.2 #NOT 2 DISPLACEMENT AT A TIME
        if keys_pressed[pygame.K_DOWN] and not self.movecountdown:
            self.selectioned += 1
            self.movecountdown = 0.2 #NOT 2 DISPLACEMENT AT A TIME

        if self.selectioned > 4:
            self.selectioned = 1
        if self.selectioned < 1:
            self.selectioned = 4
        self.movecountdown -= dt
        if self.movecountdown < 0:
            self.movecountdown = 0
        layer.fill((255,255,255))
        pygame.draw.polygon(layer,(0,0,0),self.buttonlist[self.selectioned])
        screen.blit(layer,(0,0))


    def percent(self,percx,percy):
        return (float(percx/100*SCREEN_W),float(percy/100*SCREEN_H))
    def pointlist(self,topleft,topright,bottomright,bottomleft):
        topleft = self.percent(topleft[0],topleft[1])
        topright = self.percent(topright[0],topright[1])
        bottomright = self.percent(bottomright[0],bottomright[1])
        bottomleft = self.percent(bottomleft[0],bottomleft[1])
        return [topleft,topright,bottomright,bottomleft]


if __name__ == '__main__':
    global started
    started = False
    pygame.init()
    pygame.display.set_caption("Project Swordman","Icon.png")
    pygame.display.set_icon(pygame.image.load(os.path.join("Images","Icon.png")))
    SCREEN_W,SCREEN_H = 1366,768

    #screen = pygame.display.set_mode((SCREEN_W, SCREEN_H),pygame.FULLSCREEN)
    screen = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    MainMenu().main(screen)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Guhogu
  • 81
  • 1
  • 3
  • Do you mean the framerate when the menu is open, or the framerate even when the menu is not visible? – theodox Dec 03 '13 at 19:32
  • If you have problem with FPS when playing you should show `Game` code. In most games computer has to make more computations, blitings etc. during play - so FPS can be smaller in game than in menu. `clock.tick(60)` means only "no more than 60 FPS" but it can be less. – furas Dec 03 '13 at 21:15
  • I know for the clock.tick() The FPS are significantly lower when using the main menu and I noticed by printing the values returned by the clock.tick() I used exactly same version of the actual game for both of my tests – Guhogu Dec 04 '13 at 02:22
  • I am voting to close this question as the problem is not reproducible. Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Rabbid76 Dec 22 '20 at 14:48

0 Answers0