Python Forum
My game's code works but Python still freezes at random intervals.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My game's code works but Python still freezes at random intervals.
#1
I've been working on my own version of Pong and was able to get everything working properly. I get no error messages in PyCharm prior to running it and am able to play it for a short time before Python suddenly stops responding. This happens at random intervals. Sometimes it'll crash in under a minute. Sometimes it crashes after ten minutes. I have no idea why this is happening. Does anyone have any insight as to what the issue could be?

Edit: https://github.com/GameSlayer99/PepePong
Reply
#2
It sounds like you're running it within PyCharm... does this also happen if you run it from the terminal?

Do you have any while loops? Not responding is different from crashes. If it's not responding, that means it's busy doing something.

Is the code somewhere we can look? (github, etc)
Reply
#3
(Nov-25-2021, 03:11 AM)nilamo Wrote: It sounds like you're running it within PyCharm... does this also happen if you run it from the terminal?

Do you have any while loops? Not responding is different from crashes. If it's not responding, that means it's busy doing something.

Is the code somewhere we can look? (github, etc)

I've attached the main files used.

Attached Files

.py   pepe_pong.py (Size: 1.39 KB / Downloads: 129)
.py   settings.py (Size: 879 bytes / Downloads: 116)
.py   game_functions.py (Size: 4.21 KB / Downloads: 120)
.py   startup.py (Size: 1.05 KB / Downloads: 115)
Reply
#4
Here are the rest of the files.

Attached Files

.py   ball.py (Size: 3.46 KB / Downloads: 133)
.py   enemy.py (Size: 2.16 KB / Downloads: 119)
.py   player.py (Size: 1.4 KB / Downloads: 126)
Reply
#5
(Nov-25-2021, 03:11 AM)nilamo Wrote: It sounds like you're running it within PyCharm... does this also happen if you run it from the terminal?

Do you have any while loops? Not responding is different from crashes. If it's not responding, that means it's busy doing something.

Is the code somewhere we can look? (github, etc)

While I wrote the code in PyCharm, when I run the code, it opens the game up in Python. I haven't run it from the terminal, but I have converted it into an exe file and I get the same issue. I have one while loop. When I say it doesn't respond, I mean the game abruptly freezes and when I try to close it out, I am told that the application is "not responding."
Reply
#6
Quote:
    while True:
        gf.check_events(player, pp_settings)
        clock.tick(60)
        if pp_settings.game_active:
            enemy.update()
            player.update()
            ball.update()
            gf.update_screen(pp_settings, screen, player, enemy, ball)
        else:
            gf.update_screen_startup(screen, pp_settings, player, enemy, startup)

I didn't see how pp_settings.game_active can change, but it isn't in gf.update_screen_startup(). So if it was ever False for any reason, the game would no longer update any entities.

*Edit: Nope, nope, nevermind, I see it now. game_functions.check_events() handles that. Hmm...
Reply
#7
(Nov-25-2021, 03:30 AM)nilamo Wrote:
Quote:
    while True:
        gf.check_events(player, pp_settings)
        clock.tick(60)
        if pp_settings.game_active:
            enemy.update()
            player.update()
            ball.update()
            gf.update_screen(pp_settings, screen, player, enemy, ball)
        else:
            gf.update_screen_startup(screen, pp_settings, player, enemy, startup)

I didn't see how pp_settings.game_active can change, but it isn't in gf.update_screen_startup(). So if it was ever False for any reason, the game would no longer update any entities.
If you look under check_events in game_functions.py, which is always running whether game_active is True or False, I inserted an option to turn game_active from False to True by pressing the space bar.
Reply
#8
You really should use github so people can run the code without having to downloaded every single file. as well as have access to the images.

metulburr@metulburr:~/testing$ python3 pepe_pong.py
pygame 2.0.0 (SDL 2.0.12, python 3.8.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "pepe_pong.py", line 44, in <module>
    run_game()
  File "pepe_pong.py", line 17, in run_game
    pp_settings = Settings()
  File "/home/metulburr/testing/settings.py", line 13, in __init__
    self.background = pygame.image.load('images/background2.jpg')
FileNotFoundError: No such file or directory.
(Nov-25-2021, 03:24 AM)game_slayer_99 Wrote: When I say it doesn't respond, I mean the game abruptly freezes and when I try to close it out, I am told that the application is "not responding."
Probably a bottleneck somewhere in your code.

To diagnose the issue i am just going to get it runnable. Then throw some print statements around to find out what ones are not showing, or showing too much, etc. As well as ran from a terminal to see if there is an error when it crashes. Repeat crashes a few times to see if one print statement stands out of the location of where it crashes each time. Then start looking near there on why is that section causing the crash. But without a runnable example i am just waiting on you. Which leads me back to use github.

I dont know if this is related to your issue or not. But this is called state management. And there is a better way to do this using class inheritance for different scenes. I would definitely look into switching to this method.
        if pp_settings.game_active:
            enemy.update()
            player.update()
            ball.update()
            gf.update_screen(pp_settings, screen, player, enemy, ball)
        else:
            gf.update_screen_startup(screen, pp_settings, player, enemy, startup)
            sleep(2.5)
Using time.sleep in any GUI program is a big no no. Use ticks to use seconds in game. This does not freeze the GUI. This would make a lot of sense of the random intervals of freezing as the game ends based on user input.

    def update(self):
        """Update ball's information."""
        # Update the position.
        self.rect.centerx += self.pp_settings.ball_speed_factor_x
        self.rect.centery += self.pp_settings.ball_speed_factor_y
        if self.rect.top < 0 or self.rect.bottom > self.pp_settings.screen_height:
            self.pp_settings.ball_speed_factor_y *= -1
            wall_hit = mixer.Sound('sounds/wall_hit.wav')
            wall_hit.play()

        # Update the score.
        if self.rect.right >= self.pp_settings.screen_width:
            self.pp_settings.enemy_score += 1
            if self.pp_settings.game_active:
                enemy_score = mixer.Sound('sounds/good_ending.wav')
                enemy_score.play()
            self.ball_restart()
            self.pp_settings.rally = 0
        if self.rect.left <= 0:
            self.pp_settings.player_score += 1
            if self.pp_settings.game_active:
                player_score = mixer.Sound('sounds/good_ending.wav')
                player_score.play()
            self.ball_restart()
            self.pp_settings.rally = 0
This is "a" bottleneck, but maybe not "the" bottleneck. You are loading the wav files every single time you play them instead of saving it as an object. Every single time the ball hits the wall it has to load the file. One of these times it may fail. If not it is still bottlenecking your code.
Recommended Tutorials:
Reply
#9
(Nov-25-2021, 02:33 PM)metulburr Wrote: You really should use github so people can run the code without having to downloaded every single file. as well as have access to the images.

metulburr@metulburr:~/testing$ python3 pepe_pong.py
pygame 2.0.0 (SDL 2.0.12, python 3.8.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "pepe_pong.py", line 44, in <module>
    run_game()
  File "pepe_pong.py", line 17, in run_game
    pp_settings = Settings()
  File "/home/metulburr/testing/settings.py", line 13, in __init__
    self.background = pygame.image.load('images/background2.jpg')
FileNotFoundError: No such file or directory.
(Nov-25-2021, 03:24 AM)game_slayer_99 Wrote: When I say it doesn't respond, I mean the game abruptly freezes and when I try to close it out, I am told that the application is "not responding."
Probably a bottleneck somewhere in your code.

To diagnose the issue i am just going to get it runnable. Then throw some print statements around to find out what ones are not showing, or showing too much, etc. As well as ran from a terminal to see if there is an error when it crashes. Repeat crashes a few times to see if one print statement stands out of the location of where it crashes each time. Then start looking near there on why is that section causing the crash. But without a runnable example i am just waiting on you. Which leads me back to use github.

I dont know if this is related to your issue or not. But this is called state management. And there is a better way to do this using class inheritance for different scenes. I would definitely look into switching to this method.
        if pp_settings.game_active:
            enemy.update()
            player.update()
            ball.update()
            gf.update_screen(pp_settings, screen, player, enemy, ball)
        else:
            gf.update_screen_startup(screen, pp_settings, player, enemy, startup)
            sleep(2.5)
Using time.sleep in any GUI program is a big no no. Use ticks to use seconds in game. This does not freeze the GUI. This would make a lot of sense of the random intervals of freezing as the game ends based on user input.

    def update(self):
        """Update ball's information."""
        # Update the position.
        self.rect.centerx += self.pp_settings.ball_speed_factor_x
        self.rect.centery += self.pp_settings.ball_speed_factor_y
        if self.rect.top < 0 or self.rect.bottom > self.pp_settings.screen_height:
            self.pp_settings.ball_speed_factor_y *= -1
            wall_hit = mixer.Sound('sounds/wall_hit.wav')
            wall_hit.play()

        # Update the score.
        if self.rect.right >= self.pp_settings.screen_width:
            self.pp_settings.enemy_score += 1
            if self.pp_settings.game_active:
                enemy_score = mixer.Sound('sounds/good_ending.wav')
                enemy_score.play()
            self.ball_restart()
            self.pp_settings.rally = 0
        if self.rect.left <= 0:
            self.pp_settings.player_score += 1
            if self.pp_settings.game_active:
                player_score = mixer.Sound('sounds/good_ending.wav')
                player_score.play()
            self.ball_restart()
            self.pp_settings.rally = 0
This is "a" bottleneck, but maybe not "the" bottleneck. You are loading the wav files every single time you play them instead of saving it as an object. Every single time the ball hits the wall it has to load the file. One of these times it may fail. If not it is still bottlenecking your code.

Ok. Thanks for the input. I'll upload my code to github and make some of the changes to the code that you suggested.
Reply
#10
"This is "a" bottleneck, but maybe not "the" bottleneck. You are loading the wav files every single time you play them instead of saving it as an object. Every single time the ball hits the wall it has to load the file. One of these times it may fail. If not it is still bottlenecking your code."

That may explain why the game consistently crashes only immediately after impacting either a wall or one of the paddles. I'll test it out.

Edit: I removed the sound files entirely and have yet to experience a game crash. I feel confident at the moment that this was at the very least a major contributing issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  From python game in terminal to a website game using bottle Njanez 0 3,833 Aug-13-2021, 01:11 PM
Last Post: Njanez
  Pygame freezes after certain time GJG 2 5,233 May-06-2021, 10:52 AM
Last Post: GJG
  Game “Pong” I have problems with the code BenBach18 2 3,452 Jan-10-2021, 05:16 PM
Last Post: michael1789
  Bringing My Game Code Into Class Format ZQ12 1 2,165 Dec-22-2019, 05:32 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020