0

I got a really weird bug maybe you guys can inform me on. When creating the start for this program, i just wanted to check if the screen was working, so I created the code here like this first, and the program just froze and i was so confused for like an hour.

import sys
import pygame

class Rain:
    """class that controls the rain"""
    def __init__(self):
        self.screen_width = 800
        self.screen_height = 600
        self.screen = pygame.display.set_mode((800, 600))
        self.bg_color = (28,20,97)
        self.screen.fill(self.bg_color)
        pygame.display.set_caption('raindrops')


    def _run(self):
        while True:
            self._update_screen()

    

    def _update_screen(self):
        self.screen.fill(self.bg_color)
        pygame.display.flip()


r = Rain()
r._run()

but then i added in this keydown and suddenly everything worked, it made like 0 sense to me, hoping somebody can explain it so i don't do something this stupid again. Thanks!

import sys
import pygame

class Rain:
    """class that controls the rain"""
    def __init__(self):
        self.screen_width = 800
        self.screen_height = 600
        self.screen = pygame.display.set_mode((800, 600))
        self.bg_color = (28,20,97)
        self.screen.fill(self.bg_color)
        pygame.display.set_caption('raindrops')


    def _run(self):
        while True:
            self._check_keydown()
            self._update_screen()

    
    def _check_keydown(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    print('you hit q')
                    sys.exit()
                    pygame.QUIT()



    def _update_screen(self):
        self.screen.fill(self.bg_color)
        pygame.display.flip()


r = Rain()
r._run()
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • Just read the documentation: [`pygame.event.get()`](https://www.pygame.org/docs/ref/event.html#pygame.event.get) respectively [`pygame.event.pump()`](https://www.pygame.org/docs/ref/event.html#pygame.event.pump): *"For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system."* – Rabbid76 Dec 31 '21 at 12:38

0 Answers0