-1

I just want to improve my skills in pygame, and i decided to make a Pong - like game where you first need to destroy blocks. However, i'm doing some tests, and i have a problem : The block is indeed displayed, but the window freezes, and says that Python isn't responding.

Here's my code for Pong :

import sys
import pygame
from pongsettings import Settings
from Block import Block

class Pong:
    """Overall class to manage game assets and behavior."""
    def __init__(self):
        """Initialize the game, and create game resources."""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode((700, 300))
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        pygame.display.set_caption("Pong")

        self.block = Block(self)


    def _update_screen(self):
        """Update images on the screen, and flip to the new screen."""
        while True :
            self.screen.fill(self.settings.bg_color)
            self.block.draw_block()
            
            pygame.display.flip()

    def run_game(self):
        """Start the main loop for the game."""
        while True :
            self._check_events()
            self._update_screen()

if __name__ == '__main__':
    # Make a game instance, and run the game.
    pg = Pong()
    pg.run_game()

And here's the one for Block :

import pygame
 
class Block:
    """A class to represent a single block"""

    def __init__(self, pg_game):
        """Initialize the block and set its starting position."""
        self.screen = pg_game.screen
        self.settings = pg_game.settings
        self.color = self.settings.block_color

        # Load the block and set its rect attribute.
        self.rect = pygame.Rect(500, 150, self.settings.block_width, self.settings.block_height)

    def draw_block(self):
        """Draw the block to the screen."""
        pygame.draw.rect(self.screen, self.color, self.rect)

I also have the code for the game settings, but it's really simple and i don't think the problem comes from here, but if you need it i'll post it.

Could it be caused by an infinite While loop ?

Please let me know if you got the solution !

1 Answers1

1

If there is nothing happening in the program windows might persume the program has stopped running.

Add something that happens every loop like input checking to fix it

def _check_events(self):
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      self.running = False