@@ -57,9 +57,9 @@ def drawScore(self, surface):
5757 surface .blit (scoreSurface , (self .scorePosX , 0 ))
5858
5959 def drawSplashScreen (self , surface , text , color ):
60- ''' Surface, String --> void
60+ """ Surface, String --> void
6161 Draws text centered at the center of the given Surface.
62- '''
62+ """
6363 splashFont = pygame .font .Font (None , 100 )
6464 splashSurface = splashFont .render (text , True , color )
6565 textXPos = (SCREEN_SIZE [0 ] // 2 ) - (splashFont .size (text )[0 ] // 2 )
@@ -84,22 +84,24 @@ def play(self):
8484 if event .type == KEYDOWN :
8585 break
8686
87- while True :#self.playing: # main loop for running the game
87+ while True :# main loop for running the game
88+ pygame .event .pump ()
8889 SCREEN .blit (BG , (0 , 0 )) # Makes clean background
8990 self .apple .drawApple (SCREEN , self .snake .getSize ())
9091 self .snake .drawSnake (SCREEN )
9192 self .drawScore (SCREEN )
9293 pygame .display .flip ()
9394
94- # Move the snake
95- self .snake .moveSnake ()
96-
9795 # Handle Key events
96+ didChangeDir = False
9897 for event in pygame .event .get ():
9998 self .handleQuit (event )
100- pressedKeys = pygame . key . get_pressed ()
101- self .snake .changeDir (pressedKeys )
99+ if event . type == KEYDOWN and not didChangeDir :
100+ didChangeDir = self .snake .changeDir (event )
102101
102+ # Move the snake
103+ self .snake .moveSnake ()
104+
103105 # Handle Collisions
104106 if self .isSnakeCollision ():
105107 self .snake .drawSnake (SCREEN ) # update the snake's position to when it crashed
@@ -121,6 +123,8 @@ def play(self):
121123
122124def main ():
123125 pygame .init ()
126+ pygame .event .set_allowed (None ) # No event types are allowed on queue
127+ pygame .event .set_allowed ([QUIT , KEYDOWN ])
124128 while True :
125129 game = SnakeGame ()
126130 game .play ()
0 commit comments