Ethan
Ethan

Reputation: 21

My Pygame game is running really slow?

Alright for starters I would like yall to know that it has been over a year since I have used python so if the answer is obvious and I am just stupid then please forgive my ignorance haha.

Moving on, I am creating a Space Invaders type game and everything is going smoothly except for when I am changing from the main screen to the game screen. It might just be the method I am using to change screens, but I have used this method in other programs before and have never had any issues. Or might just be trying to run too many processes at the same time. Either way, I cannot find anything similar to the problem so I have resorted to asking for help.

(While we are on the topic of methods for changing screens in pygame, if anyone knows of a better method to use please let me know).

Below is the main code, let me know if you need to see the import files and I will try to get back to you as soon as possible with whatever files you are asking for.

import time
from SpaceInvaders.Scroller import *
from SpaceInvaders.TextDisplay import *
from SpaceInvaders.Alien import *
from SpaceInvaders.Shooter import *
from SpaceInvaders.Colors import *

background = Scroller("starfield.png", "starfield.png", 0, 0, 0, 1000)
screen_width = 800
screen_height = 1000
screen = pygame.display.set_mode((screen_width, screen_height))
mouse_pos = pygame.mouse.get_pos()


def title_screen():
    pygame.init()

    done = False

    clock = pygame.time.Clock()

    intro = Text("Welcome to Space Invaders", indian_red, 30, (screen_width * .5), (screen_height * .5 - 100))

    play_button_text = Text("Click to Play", white, 15, (screen_width * .5), (screen_height * .5))

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            elif event.type == pygame.MOUSEBUTTONDOWN:
                game_screen()

        background.draw(screen)
        background.update()

        intro.draw(screen)

        play_button_text.draw(screen)

        clock.tick(30)

        pygame.display.flip()

    pygame.quit()


def game_screen():
    pygame.init()

    alien = Alien("Alien.png")

    shooter = Shooter("Shooter.png", mouse_pos[0], 650)

    done = False

    clock = pygame.time.Clock()

    level_text = Text("Level 1", silver, 50, (screen_width * .5), (screen_height * .5))

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

        screen.fill(black)

        level_text.draw(screen)

        time.sleep(2)

        background.draw(screen)
        background.update()

        clock.tick(30)

        pygame.display.flip()

    pygame.quit()

title_screen()

The game runs perfectly until you try to switch from the title screen to the game screen, then the framerate drops and my entire laptop slows down. It is actually quite annoying.

Please and thank you for all the help I hope to receive with this problem. Again just let me know if you would like to see the import files and I will get back to you as soon as possible. Also if you would like to download the files so you can look at them for yourself then I will be leaving a link to them at the end of this.

I should also add that this is the first time I am making a game with a scrolling background in it so that might be the issue (I hope it isn't). If it is I would appreciate any ideas on how to be able to still use it without the framerate drop.

Google Drive Link for those who want to download the files: https://drive.google.com/drive/folders/0B69VPNkZEzsmTjhaYzRQRVEwdTA?usp=sharing

===================================EDIT===================================

I also realize that the Alien and Shooter objects are not being rendered. That is due to the fact that I have not adjusted the sizes yet. They are still too big for the game.

Upvotes: 0

Views: 822

Answers (1)

Ethan
Ethan

Reputation: 21

The answer was quite simple, I had the time.sleep(2) inside of the while loop and I guess you cannot wait while having an infinite loop going. Kind of defeats the purpose haha.

Upvotes: 1

Related Questions