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.
#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


Messages In This Thread
RE: My game's code works but Python still freezes at random intervals. - by metulburr - Nov-25-2021, 02:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame freezes after certain time GJG 4 5,651 May-21-2024, 04:02 AM
Last Post: deanhystad
  From python game in terminal to a website game using bottle Njanez 0 3,921 Aug-13-2021, 01:11 PM
Last Post: Njanez
  Game “Pong” I have problems with the code BenBach18 2 3,618 Jan-10-2021, 05:16 PM
Last Post: michael1789
  Bringing My Game Code Into Class Format ZQ12 1 2,278 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